Cannot delete or update a parent row: a foreign key constraint fails解決方法

我在使用delete from table語句清空一個表數據時,報錯 Cannot delete or update a parent row: a foreign key constraint fails。

先把我的數據庫貼出來。

tb_device表





tb_device_status_now表



正題:


最開始我直接清空tb_device,報錯。看數據庫發現tb_device_status_now中的字段與其外鍵關聯所以不能清空。所以改爲先清空tb_device_status_now表,再清空tb_device。結果發現還是報錯!再看數據庫,發現tb_device表中dev_master字段與dev_id字段也是外鍵關聯。。。。

所以要先清空dev_master字段,才能清空tb_device整張表。

清空某字段數據的sql語句:update table set column=null


最後我的刪除順序:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章