簡體   English   中英

從MySQL表的3個表中刪除行

[英]Deleting rows from 3 tables in MySQL table

我有3個表(我僅提及所需的屬性):

進口 iddate

import_head idimport_id

import_body idimport_head_id

import_headimport_body創建一個導入項目,導入是由數十個導入項目創建的。關系: import 1 <==> N * import_head * 1 <==> 1 * import_body *(每個關系上的DELETE CASCADE)

從導入表中刪除行時如何刪除所有導入項?

使用DELETE FROM import WHERE id = ?刪除導入時 引發錯誤: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails -在import_head_id

在此先多謝

修改子表上的外鍵並在刪除時進行(CASCADE)
這應該為你工作

您需要先刪除關聯表的記錄,然后再從主鍵表中刪除該記錄。 由於性能影響,我個人將避免cascade刪除,但這是您的決定。 這是一個討論cascade問題的線程

我已經更改了數據庫中的實體,所以現在只有兩個表:

import
import_item

我沒有發現更好的方法,但是它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM