ORA-02292: integrity constraint (xxxx) violated - child record found

简介: 在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外键关系,下面借助一个小列子来描述一下这个错误: SQL> create table studen...

在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外键关系,下面借助一个小列子来描述一下这个错误:

SQL> create table student
  2  (
  3    id  number,
  4    name nvarchar2(12),
  5    constraint pk_student primary key(id) 
  6  );
 
Table created.
QL> create table grades
  2  (  id  number ,
  3     subject nvarchar2(12),
  4     scores number,
  5     constraint pk_grades primary key(id ,subject),
  6     constraint fk_student_id foreign key(id) references student(id)
  7  );
 
Table created.
 
SQL> insert into student
  2  values(1001,'kerry');
 
1 row created.
 
SQL> insert into student
  2  values(1002,'jimmy');
 
1 row created.
 
SQL> commit;
 
Commit complete.
 
SQL> insert into grades
  2  values(1001, 'math', 120);
 
1 row created.
 
SQL> insert into grades
  2  values(1001, 'english', 106);
 
1 row created.
 
SQL> commit;
 
Commit complete.
 
SQL> update student set id=1004 where name='kerry';
update student set id=1004 where name='kerry'
*
ERROR at line 1:
ORA-02292: integrity constraint (TEST.FK_STUDENT_ID) violated - child record
found
 
 
SQL> 

 

 

 

遇到这种情况,首先找到外键约束和相关表,禁用外键约束,处理数据,然后启用外键约束。

SELECT OWNER, CONSTRAINT_NAME, TABLE_NAME 
FROM DBA_CONSTRAINTS 
WHERE CONSTRAINT_NAME=&CONSTRAINT_NAME;
 
SELECT OWNER, CONSTRAINT_NAME, TABLE_NAME 
FROM USER_CONSTRAINTS 
WHERE CONSTRAINT_NAME=&CONSTRAINT_NAME;
 
 
SQL> ALTER TABLE TEST.GRADES DISABLE CONSTRAINT FK_STUDENT_ID;
 
Table altered.
 
SQL> update student set id=1004 where name='kerry';
 
1 row updated.
 
 
SQL> update grades set id=1004 where id =1001;
 
2 rows updated.
 
SQL> commit;
 
Commit complete.
 
SQL> ALTER TABLE TEST.GRADES ENABLE CONSTRAINT FK_STUDENT_ID;
 
Table altered.
 
SQL> 

 

 

如果是删除数据遇到这种情况,可以先删除子表数据,然后删除父表数据。

SQL> delete from student where id=1004;
delete from student where id=1004
*
ERROR at line 1:
ORA-02292: integrity constraint (TEST.FK_STUDENT_ID) violated - child record
found
 
 
SQL> delete from grades
  2  where id in
  3  ( select id from student
  4    where id=1004);
 
2 rows deleted.
 
SQL> delete from student where id=1004;
 
1 row deleted.
 
SQL> commit;
 
Commit complete.
 
SQL> 
 

相关文章
|
6月前
|
关系型数据库 MySQL 数据库
Error: Table ‘bWAPP.users‘ doesn‘t exist
Error: Table ‘bWAPP.users‘ doesn‘t exist
|
7月前
|
Oracle 关系型数据库 数据库
【已解决】ORA-00942: table or view does not exist
【已解决】ORA-00942: table or view does not exist
598 0
|
存储 关系型数据库 MySQL
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
|
Linux
WARNING: Re-reading the partition table failed with error 22: Invalid argument
在划分磁盘分区时,遇到错误“WARNING: Re-reading the partition table failed with error 22: Invalid argument” 如下所示: [root@DB-Server u02]# fdisk -l   Disk /dev/sda: 500.
2547 0
|
数据安全/隐私保护 索引
报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin'
报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin'
505 0
|
SQL 关系型数据库 Oracle
ORA-01466: unable to read data - table definition has changed
1. Oracle建议我们等待大约5分钟之后再进行flashback query新创建的表,否则可能会碰到这个错误ORA-01466: unable to read data - table definition has changed.
1813 0
|
SQL 数据库管理 关系型数据库
SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
测试环境:OEL6.5+Oracle 11g R2在进行执行计划测试的时候,遇到一个小问题。在用普通用户执行下面这条命令的时候,普通用户名为hhu,已经赋予了create session和resource权限。
1132 1
|
SQL 关系型数据库
ORA-1652: unable to extend temp segment by 128 in tablespace xxx Troubleshootin
当收到告警信息ORA-01652: unable to extend temp segment by 128 in tablespace xxxx 时,如何Troubleshooting ORA-1652这样的问题呢? 当然一般xxx是临时表空间,也有可能是用户表空间。
2112 0
|
Oracle 关系型数据库
Mandatory Patching Requirement for Database Versions 11.2.0.3 or Earlier, Using DB Links (DOC ID 2335265.1)
Mandatory Patching Requirement for Database Versions 11.2.0.3 or Earlier, Using DB Links (DOC ID 2335265.
2813 0