Oracle composite primary key / foreign key question

后端 未结 4 1697
余生分开走
余生分开走 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条回答
  • 2021-02-13 23:06

    You can't use:

    CONSTRAINT fk_persongroup FOREIGN KEY(groupid) REFERENCES groupspersonx(personid, groupid)
    

    Change that too:

    CONSTRAINT fk_persongroup FOREIGN KEY(groupid) REFERENCES groupspersonx(groupid) 
    

    That should work.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-13 23:26

    The error is because the FOREIGN KEY is one column, but you're trying to supply two columns as the parent. There's no need to tie to the composite key, because the restrictedgroups doesn't have a personid column...

    You also have the relationship backwards - use:

    CREATE TABLE restrictedgroups ( 
      groupid number, 
      name varchar2(50), 
      dateadded date, 
      since date, 
      notes varchar2(1024), 
      CONSTRAINT pk_groupid PRIMARY KEY(groupid)
    );
    
    CREATE TABLE groupspersonx ( 
      personid number, 
      groupid number, 
      CONSTRAINT pk_persongroupid PRIMARY KEY(personid, groupid),
      CONSTRAINT fk_persongroup FOREIGN KEY(groupid) REFERENCES restrictedgroups(groupid) 
    ); 
    

    I would add a foreign key constraint for whatever table the personid would be coming from.

    0 讨论(0)
  • 2021-02-13 23:26
    CREATE TABLE groupspersonx( 
      personid number, groupid number, 
    CONSTRAINT pk_persongroupid PRIMARY KEY(personid, groupid));
    
    CREATE TABLE restrictedgroups ( 
      pid number, 
      groupid number,
      name varchar2(50), 
      dateadded date, 
      since date, 
      notes varchar2(1024), 
      CONSTRAINT pk_groupid PRIMARY KEY(groupid), 
      CONSTRAINT fk_persongroup FOREIGN KEY(pid,groupid) REFERENCES groupspersonx(personid, groupid));
    

    * number of references columns is equals with foreign key columns

    0 讨论(0)
提交回复
热议问题