您好,登錄后才能下訂單哦!
本篇內容介紹了“數據庫更新表數據時出現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錯誤怎么解決”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。