[英]DELETE all where MySQL foreign key constraint does not fail
I am trying to delete a few records but am getting the following error: 我试图删除一些记录,但我收到以下错误:
Cannot delete or update a parent row: a foreign key constraint fails
无法删除或更新父行:外键约束失败
The thing is, the foreign key constraint is failing for only 1 or 2 of my 100 records I wish to delete. 问题是,我希望删除的100条记录中只有1或2条中的外键约束失败。 I wish to write a query which deletes these 98-99 records, skipping the 1 or 2 which failed , which I can later manually inspect and delete/modify.
我希望编写一个删除这些98-99记录的查询, 跳过失败的1或2记录,稍后我可以手动检查和删除/修改。 Not stopping because of some single problematic record, but continuing with the others, ignoring that.
不是因为一些有问题的记录而停止,而是继续其他人,忽略了这一点。
Is there a neat way to do this ? 有一个巧妙的方法来做到这一点?
You have to LEFT JOIN
the referencing table and add a condition saying that the row is missing in that table. 您必须
LEFT JOIN
引用表并添加一个条件,表示该表中缺少该行。
For example: 例如:
DELETE a FROM a
LEFT JOIN b ON b.a_id = a.id
WHERE b.a_id IS NULL;
Use ignore: 使用ignore:
DELETE IGNORE ...
http://dev.mysql.com/doc/refman/5.0/en/delete.html http://dev.mysql.com/doc/refman/5.0/en/delete.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.