引用作为主键的外键与父表上的另一个键

时间:2022-12-25 10:23:06

i have those tables :

我有那些表:

create table t_table(t1 int, constraint tprim primary key(t1));

create table parent_table(t1 int, p1 int, p2 int, constraint pprim primary key(t1, p1));

create table child_table(t1 int, c1 int, p1 int, constraint cprim primary key(t1, c1), constraint fchild foreign key (p1) references parent_table(p1));

i get this error when creating the second table :

我在创建第二个表时遇到此错误:

ORA-02270: no matching unique or primary key for this column-list

p1 in parent_table is not unique so i can't make it unique to resolve the problem.

parent_table中的p1不是唯一的,因此我无法使其独一无二来解决问题。

in this case how to say that p1 is a foreign key in child_table that references parent_table(p1) ?

在这种情况下,如何说p1是child_table中引用parent_table(p1)的外键?

thanks in advance.

提前致谢。

2 个解决方案

#1


2  

You can't. A Foreign Key references a Primary Key. Perhaps what you want to do is to include t1 in the foreign key to child_table? Or else, if you really need something like p1 to be FK in child_table, perhaps you need one more table, just for p1, with unique values? Then p1 in both parent_table and child_table is FK pointing to this additional table.

你不能。外键引用主键。也许你想要做的是在child_table的外键中包含t1?或者,如果你真的需要像p1这样的东西在child_table中成为FK,那么你可能还需要一个表,仅用于p1,具有唯一值?然后,parent_table和child_table中的p1都是指向此附加表的FK。

#2


0  

this is not possible you must reference a primary key

这是不可能的,您必须引用主键

#1


2  

You can't. A Foreign Key references a Primary Key. Perhaps what you want to do is to include t1 in the foreign key to child_table? Or else, if you really need something like p1 to be FK in child_table, perhaps you need one more table, just for p1, with unique values? Then p1 in both parent_table and child_table is FK pointing to this additional table.

你不能。外键引用主键。也许你想要做的是在child_table的外键中包含t1?或者,如果你真的需要像p1这样的东西在child_table中成为FK,那么你可能还需要一个表,仅用于p1,具有唯一值?然后,parent_table和child_table中的p1都是指向此附加表的FK。

#2


0  

this is not possible you must reference a primary key

这是不可能的,您必须引用主键