Oracle composite primary key / foreign key question

后端 未结 4 1779
余生分开走
余生分开走 2021-02-13 22:37

I have a composite primary key in 1 table in oracle. I want to create a foreign key for one table entry in my second table that references the composite primary key in the firs

4条回答
  •  萌比男神i
    2021-02-13 23:10

    Whenever you want to create a composite primary key or unique constraint on a column, you can't give reference in another table.

    for ex.

    sql>create table t1( a number,b number,c number ,primary key(a,b,c));
    
    table created.
    
    sql>create table g1(a number constraint con_fg references t1(a));
    
    ERROR at line 1:
    ORA-02270: no matching unique or primary key for this column-list
    

    Here t1 is parent table and g1 is child table. The child table can contains duplicate values in one column. So oracle will not allow that table of column.

    See also

    SQL>select constraint_name,constraint_type from user_constraints where table_name='T1';
    
    CONSTRAINT_NAME                C
    ------------------------------ -
    SYS_C005822                    P
    

    So, here also the only constraint for all three columns i.e a,b,c in t1 table.

    That's why you can't create a foreign on composite primary key or composite unique constraint

提交回复
热议问题