DELETE all where MySQL foreign key constraint does not fail

后端 未结 2 1187
独厮守ぢ
独厮守ぢ 2021-02-19 11:16

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

相关标签:
2条回答
  • 2021-02-19 11:35

    Use ignore:

    DELETE IGNORE ...
    

    http://dev.mysql.com/doc/refman/5.0/en/delete.html

    0 讨论(0)
  • 2021-02-19 11:50

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

    For example:

    DELETE a FROM a
    LEFT JOIN b ON b.a_id = a.id
    WHERE b.a_id IS NULL;
    
    0 讨论(0)
提交回复
热议问题