Truncate all tables (most of which have constraints). How to temporarily drop them

后端 未结 4 1473
清酒与你
清酒与你 2021-02-02 17:59

I have a development database (MYSQL) which I would like to load with fresh data at some point. I would like to delete the content of all tables. What is the best way, as autom

4条回答
  •  悲哀的现实
    2021-02-02 18:07

    Here's what I do from the command prompt:

     mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE table $table" DATABASE_NAME; done
    

    Problem is, if you require authentication, you will need to add a -u YOUR_USERNAME -pYOURPASSWORD after both mysql commands.

    If you don't want to enter your password at the command prompt (I don't ever), then you'll need to copy and paste the password after every prompt, which may be several times depending on the number of tables.

提交回复
热议问题