Oracle composite primary key / foreign key question

后端 未结 4 1696
余生分开走
余生分开走 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: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.

提交回复
热议问题