MySQL DROP all tables, ignoring foreign keys

前端 未结 24 2747
醉梦人生
醉梦人生 2020-12-02 03:44

Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?

24条回答
  •  萌比男神i
    2020-12-02 04:03

    From http://www.devdaily.com/blog/post/mysql/drop-mysql-tables-in-any-order-foreign-keys:

    SET FOREIGN_KEY_CHECKS = 0;
    drop table if exists customers;
    drop table if exists orders;
    drop table if exists order_details;
    SET FOREIGN_KEY_CHECKS = 1;
    

    (Note that this answers how to disable foreign key checks in order to be able to drop the tables in arbitrary order. It does not answer how to automatically generate drop-table statements for all existing tables and execute them in a single script. Jean's answer does.)

提交回复
热议问题