Oracle composite primary key / foreign key question

故事扮演 提交于 2019-12-21 03:44:07

问题


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 first table. I am getting the error ORA-02256. Any thoughts on how I can enter this?

CREATE TABLE groupspersonx ( 
  personid number, 
  groupid number, 
  CONSTRAINT pk_persongroupid PRIMARY KEY(personid, groupid) 
); 

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

回答1:


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.




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/6602099/oracle-composite-primary-key-foreign-key-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!