错误码:-6607
错误内容:违反引用约束[%s]
创建外键时子表里存在父表不存在的数据
--创建父表
create table test1(id int,name varchar(20),constraint pk_test1_id primary key(id));
insert into test1 values(1,'A');
insert into test1 values(2,'B');
commit;
--创建子表
create table test2(id int,name varchar(20));
insert into test2 values(1,'A');
insert into test2 values(3,'C');
commit;
--执行添加外键
alter table test2 add constraint fk_test2_id foreign key(id) references test1(id);
-6607: 违反引用约束[FK_TEST2_ID]
1、查询子表中存在,且父表中不存在的数据;
select * from test2 where not exists (select 1 from test1 where test1.id=test2.id);
2、删除子表数据,或在父表中新增数据;
文章
阅读量
获赞