【发布时间】:2016-03-30 14:20:46
【问题描述】:
我有 emp1 和 emp2 的表
emp1:
emp_1 | emp_2
1 | 2
3 | 4
5 | 6
emp2:
emp
1
2
3
6
我尝试将主键设置为表 emp1,将外键设置为表 emp2。
我的代码:
对于主键:
alter table emp1 add primary key(emp_1,emp_2);
对于外键:
alter table emp2
add foreign key (emp)
references a_t1(emp_1,emp_2);
错误:
Error report -
SQL Error: ORA-02256: number of referencing columns must match referenced columns
02256. 00000 - "number of referencing columns must match referenced columns"
*Cause: The number of columns in the foreign-key referencing list is not
equal to the number of columns in the referenced list.
*Action: Make sure that the referencing columns match the referenced
columns.
请帮我解决这个错误并设置主键。
【问题讨论】:
-
您正在尝试将一个外键从一个字段添加到两个字段;您必须决定表 EMP2 中的值是否必须与表 EMP1 中的 emp_1 或 emp_2 列中的记录匹配。
-
您不能让 FK 引用复合 PK 的一部分,或者两个单独的密钥(PK 或 UK)中的任何一个。例如,如果您在 emp1 中有具有值 (7,8) 和值 (8,9) 的行,并且希望在 emp2 中有具有值 (8) 的行,您会发生什么?那是指父项中的哪一行?
-
是的,我明白了。我试图创建一个复合主键。
标签: oracle oracle11g foreign-keys primary-key composite-primary-key