DELETE all where MySQL foreign key constraint does not fail

后端 未结 2 1180
独厮守ぢ
独厮守ぢ 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: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;
    

提交回复
热议问题