删除所有MySQL外键约束没有失败的地方 [英] DELETE all where MySQL foreign key constraint does not fail

查看:137
本文介绍了删除所有MySQL外键约束没有失败的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除一些记录,但是出现以下错误:

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

问题是,外键约束仅对我要删除的100条记录中的1条或2条失败.我希望编写一个查询来删除这98-99条记录, 跳过失败的1或2条记录 ,以后可以手动检查和删除/修改.不是因为某个有问题的记录而停止,而是继续其他问题,而忽略了这一点.

The thing is, the foreign key constraint is failing for only 1 or 2 of my 100 records I wish to delete. 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. Not stopping because of some single problematic record, but continuing with the others, ignoring that.

是否有一种整齐的方法来做到这一点?

Is there a neat way to do this ?

推荐答案

您必须LEFT JOIN引用表并添加一个条件,表明该表中缺少该行.

You have to LEFT JOIN the referencing table and add a condition saying that the row is missing in that table.

例如:

DELETE a FROM a
LEFT JOIN b ON b.a_id = a.id
WHERE b.a_id IS NULL;

这篇关于删除所有MySQL外键约束没有失败的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆