ORA-02292: 违反完整约束条件 (***.FK_****) - 已找到子记录

执行DELETE FROM dept WHERE deptno = dept_no;时报错

1 行出现错误:
ORA-02292: 违反完整约束条件 (SCOTT.FK_DEPTNO) - 已找到子记录
ORA-06512: 在 "SCOTT.PTESTFOUND", line 5
ORA-06512: 在 line 3

查询约束具体状况

select * from user_constraints  ; #用SCOTT用户执行,否则找不到
OWNERCONSTRAINT_NAMECONSTRAINT_TYPETABLE_NAMESEARCH_CONDITIONR_OWNERR_CONSTRAINT_NAMEDELETE_RULESTATUSDEFERRABLEDEFERREDVALIDATEDGENERATED
SCOTTPK_DEPTPDEPTENABLEDNOT DEFERRABLEIMMEDIATEVALIDATEDUSER NAME
SCOTTPK_EMPPEMPENABLEDNOT DEFERRABLEIMMEDIATEVALIDATEDUSER NAME
SCOTTFK_DEPTNOREMPSCOTTPK_DEPTNO ACTIONENABLEDNOT DEFERRABLEIMMEDIATEVALIDATEDUSER NAME

CONSTRAINT_TYPE:
P --primary key unique and not null,可以是多个column的联合
R --forgien key parent table 中的primary key中的values必须包含child table中所有的values. share column的parent-child 关系

或者直接通过约束名称查找,更加直观显示:

select a.constraint_name, a.table_name, b.constraint_name ,b.table_name
 from user_constraints a, user_constraints b
 where a.constraint_type = 'R' and b.constraint_type = 'P' 
 and a.r_constraint_name = b.constraint_name
 and a.constraint_name = 'FK_DEPTNO'
CONSTRAINT_NAMETABLE_NAMECONSTRAINT_NAMETABLE_NAME
FK_DEPTNOEMPPK_DEPTDEPT

EMP表中有外键FK_DEPTNO中关联的字段DEPTNO与表DEPT的主键PK_DEPT中关联字段DEPTNO
所以删除EMP表某一行时需要先把表DEPT中对应的一行先删除;或者删除/禁用EMP表中外键FK_DEPTNO约束。
参考《oracle 添加、修改、删除、约束-语法》

--删除所有外键约束 的Sql代码  
select 'alter table '||table_name||' drop constraint '||constraint_name||';' from user_constraints where constraint_type='R';
alter table EMP drop constraint FK_DEPTNO;
--禁用所有外键约束的Sql代码 
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='R'`
alter table EMP disable constraint FK_DEPTNO;
--启用所有外键约束的Sql代码 
select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='R'   
alter table EMP enable constraint FK_DEPTNO;

我直接执行禁用EMP表的外键关联

SQL> alter table EMP disable constraint FK_DEPTNO;

删除语句执行成功。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值