MySQL DROP all tables, ignoring foreign keys

前端 未结 24 2761
醉梦人生
醉梦人生 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:51

    A one liner to drop all tables from a given database:

    echo "DATABASE_NAME"| xargs -I{} sh -c "mysql -Nse 'show tables' {}| xargs -I[] mysql -e 'SET FOREIGN_KEY_CHECKS=0; drop table []' {}"
    

    Running this will drop all tables from database DATABASE_NAME.

    And a nice thing about this is that the database name is only written explicitly once.

提交回复
热议问题