mysql刪除有外鏈索引數據,Cannot delete or update a parent row: a foreign key constraint fails 問題的解決辦法

mysql刪除有外鏈索引數據
Cannot delete or update a parent row: a foreign key constraint fails 問題的解決辦法
查詢:DELETE FROM `goods` WHERE `goods_id` = '11'
錯誤代碼: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`webDB`.`goods_properties_detail`, CONSTRAINT `FK_rip_43` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`goods_id`))
以上是由於,該條記錄的某個字段做爲innodb的另一個表的外鏈。而在刪除操做時會自動檢查外鏈。
解決辦法一:先判斷刪除關聯數據,而後再刪除(這樣比較符合業務邏輯比較安全)。mysql

解決辦法二:不檢查外鏈,設置FOREIGN_KEY_CHECKS變量:
SET FOREIGN_KEY_CHECKS = 0;
DELETE FROM `goods` WHERE `goods_id` = '11'
刪除完成後設置
SET FOREIGN_KEY_CHECKS = 1; web

ps:關閉惟一性校驗
set unique_checks=0;
set unique_checks=1;sql

相關文章
相關標籤/搜索