MySQL ERROR 1451 23000 外來鍵異常處理
1、執行delete報錯
mysql> delete from JBPM4_EXECUTION;
ERROR 1451 (23000): Cannot delete or updatea parent row: a foreign key constraint fails (`jbpm_db`.`JBPM4_EXECUTION`,CONSTRAINT `FK_EXEC_INSTANCE` FOREIGN KEY (`INSTANCE_`) REFERENCES`JBPM4_EXECUTION` (`DBID_`))
mysql>
表有外來鍵所以delete報錯了,這裡有2種辦法處理:
(1) 臨時設定外來鍵失效
(2) 刪除表涉及到的外來鍵的表的資料
2、外來鍵失效的處理方案
mysql> SET FOREIGN_KEY_CHECKS = 0; # 臨時設定外來鍵失效 Query OK, 0 rows affected (0.00 sec) mysql> mysql> delete from JBPM4_EXECUTION; #執行刪除操作 Query OK, 110 rows affected (0.00 sec) mysql> mysql> SET FOREIGN_KEY_CHECKS = 1; # 操作結束後恢復外來鍵 Query OK, 0 rows affected (0.00 sec) mysql> |
3、刪除外來鍵表的資料的出來方案
先查詢表鎖涉及到的所有外來鍵情況,查詢sql如下:
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = |
執行查詢會看到有如下外來鍵:
E:\u\mysql\problem\pic\02.jpg
然後看到涉及到外來鍵的表的有 JBPM4_VARIABLE、JBPM4_EXECUTION、JBPM4_SWIMLANE三張表,接下來清除這三張表的資料即可。
mysql> delete from JBPM4_VARIABLE; Query OK, 1404 rows affected (0.03 sec) mysql> delete fromJBPM4_SWIMLANE; Query OK, 13 rows affected (0.03 sec) mysql> delete from JBPM4_EXECUTION; # 這裡報錯是因為表自己給自己設定了外來鍵關聯,所以清除外來鍵欄位的資料,就可以了 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`jbpm_db`.`JBPM4_EXECUTION`, CONSTRAINT `FK_EXEC_INSTANCE` FOREIGN KEY (`INSTANCE_`) REFERENCES `JBPM4_EXECUTION` (`DBID_`)) mysql> mysql> update JBPM4_EXECUTION set INSTANCE_=null,PARENT_=null,SUBPROCINST_=null,SUPEREXEC_=null; Query OK, 203 rows affected (0.02 sec) Rows matched: 203 Changed: 203 Warnings: 0 mysql> delete from JBPM4_EXECUTION; # 所有外來鍵關聯資料都清除了,現在刪除資料可以了。 Query OK, 203 rows affected (0.02 sec) mysql> |