Force drop mysql bypassing foreign key constraint

后端 未结 6 912
别跟我提以往
别跟我提以往 2021-01-29 18:08

I\'m trying to delete all tables from a database except one, and I end up having the following error:

Cannot delete or update a parent row: a foreign key

6条回答
  •  执笔经年
    2021-01-29 18:58

    This might be useful to someone ending up here from a search. Make sure you're trying to drop a table and not a view.

    SET foreign_key_checks = 0;
    -- Drop tables
    drop table ...
    -- Drop views
    drop view ...
    SET foreign_key_checks = 1;
    

    SET foreign_key_checks = 0 is to set foreign key checks to off and then SET foreign_key_checks = 1 is to set foreign key checks back on. While the checks are off the tables can be dropped, the checks are then turned back on to keep the integrity of the table structure.

提交回复
热议问题