添加以联合主键之一作为外键的约束时,出错:此列列表的唯一关键字或主键不匹配

来源:互联网 发布:360全球实时网络攻击 编辑:程序博客网 时间:2024/06/08 04:37

创建了两个表:t_test1 t_test2

create table t_test1(
id_1 varchar2(4),
id_2 varchar2(4),
other varchar2(50));

create table t_test2(
id_1 varchar2(4),
other varchar2(50));

 

设置t_test1的主键:

alter table t_test1 add constraint pk_1 primary key(id_1,id_2);

 

设置t_test2的外键 t_test2.id_1 参照 t_test2.id_1 出错:

alter table t_test2 add constraint fk_1 foreign key(id_1) references t_test1(id_1);

出错:此列列表的唯一关键字或主键不匹配

 

原来是id_1不惟一的原因,添加惟一性约束就能解决问题

alter table t_test1 add constraint uqn_1 unique(id_1);

alter table t_test2 add constraint fk_1 foreign key(id_1) references t_test1(id_1);

原创粉丝点击