删除所有MySQL外键约束不失败的地方
我试图删除一些记录,但收到以下错误:
无法删除或更新父行:外键约束失败
问题是,对于我希望删除的 100 条记录中的 1 条或 2 条,外键约束失败。我希望编写一个删除这 98-99 条记录的查询,跳过失败的 1 条或 2 条,我可以稍后手动检查并删除/修改这些记录。不会因为某个有问题的记录而停止,而是继续处理其他记录,忽略它。
有没有一种巧妙的方法来做到这一点?
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. 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须
LEFT JOIN
引用表并添加一个条件,说明该表中缺少该行。例如:
You have to
LEFT JOIN
the referencing table and add a condition saying that the row is missing in that table.For example:
使用忽略:
http://dev.mysql.com/doc/refman/ 5.0/en/delete.html
Use ignore:
http://dev.mysql.com/doc/refman/5.0/en/delete.html