温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

数据库更新表数据时出现ORA-02292错误怎么解决

发布时间:2021-12-22 09:44:23 来源:亿速云 阅读:1186 作者:iii 栏目:关系型数据库

本篇内容介绍了“数据库更新表数据时出现ORA-02292错误怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

实验:
--建立主表
SQL> create table student(id  number,name nvarchar2(12),constraint pk_student primary key(id));

Table created.

--建立外键约束表
SQL> create table grades(id  number ,subject nvarchar2(12),scores number,constraint pk_grades primary key(id ,subject),constraint fk_student_id foreign key(id) references student(id));

Table created.

SQL> insert into student values(1001,'kerry');

1 row created.

SQL> insert into student values(1002,'jimmy');

1 row created.

SQL> commit;

Commit complete.

SQL> insert into grades values(1001, 'math', 120);

1 row created.

SQL> insert into grades values(1001, 'english', 106);

1 row created.

SQL> commit;

Commit complete.

--查看:
SQL> select * from student;

        ID NAME
---------- ------------------------
      1001 kerry
      1002 jimmy

SQL> select * from grades;

        ID SUBJECT                      SCORES
---------- ------------------------ ----------
      1001 math                            120
      1001 english                         106

--更新主表列
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 (SYS.FK_STUDENT_ID) violated - child record
found


--报错,解决:首先找到外键约束和相关表,禁用外键约束,处理数据,然后启用外键约束:查询dba_constraints
SQL> col OWNER for a10
SQL> select owner,CONSTRAINT_NAME,TABLE_NAME,SEARCH_CONDITION,VALIDATED from dba_constraints where constraint_name='FK_STUDENT_ID';

OWNER      CONSTRAINT_NAME                TABLE_NAME                     SEARCH_CONDITION               VALIDATED
---------- ------------------------------ ------------------------------ ------------------------------ -------------
SYS        FK_STUDENT_ID                  GRADES                                                         VALIDATED

SQL>

--disable约束:
SQL> alter table sys.grades disable constraint FK_STUDENT_ID;

Table altered.

SQL>
SQL> update student set id=1004 where name='kerry';

1 row updated.

SQL> commit;

Commit complete.

SQL> update grades set id=1004 where id =1001;

2 rows updated.

SQL> commit;

Commit complete.

--修改完后,再enable约束:
SQL> alter table sys.grades enable constraint FK_STUDENT_ID;

Table altered.

--查看:
SQL>  select * from student;

        ID NAME
---------- ------------------------
      1004 kerry
      1002 jimmy

SQL> select * from grades;

        ID SUBJECT                      SCORES
---------- ------------------------ ----------
      1004 math                            120
      1004 english                         106

SQL>


---如果是删除数据遇到这种情况,可以先删除子表数据,然后删除父表数据,如下:
SQL> delete from student where id=1004;
delete from student where id=1004
*
ERROR at line 1:
ORA-02292: integrity constraint (SYS.FK_STUDENT_ID) violated - child record found

--先删除子表中相关列数据,
SQL>  delete from grades where id in ( select id from student where id=1004);

2 rows deleted.

--再次删除
SQL>  delete from student where id=1004;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from student;

        ID NAME
---------- ------------------------
      1002 jimmy

SQL> select * from grades;

no rows selected

SQL>

“数据库更新表数据时出现ORA-02292错误怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI