MySQL DROP all tables, ignoring foreign keys

前端 未结 24 2705
醉梦人生
醉梦人生 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 03:47

    This solution is based on @SkyLeach answer but with the support of dropping tables with foreign keys.

    echo "SET FOREIGN_KEY_CHECKS = 0;" > ./drop_all_tables.sql
    mysqldump --add-drop-table --no-data -u user -p dbname | grep 'DROP TABLE' >> ./drop_all_tables.sql
    echo "SET FOREIGN_KEY_CHECKS = 1;" >> ./drop_all_tables.sql
    mysql -u user -p dbname < ./drop_all_tables.sql
    

提交回复
热议问题