Delete data from all tables in MYSQL

前端 未结 16 1766
自闭症患者
自闭症患者 2020-12-13 09:17

I have 100 tables, 40,000 rows in each table. I want to go into MySQL and delete all rows from all tables.

...in 1 statement, if p

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 09:56

    I had to restore a backup which was missing the create statements. So I restored an older backup but I had to get rid of the data. This oneliner helped me truncate everything.

    mysql -u user -ppass -NBe "SELECT 'SET foreign_key_checks = 0;' UNION SELECT CONCAT('TRUNCATE TABLE myschema.', table_name, ';') FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'myschema' AND table_type = 'BASE TABLE'" | mysql -u user -ppass
    

    For better readability again with line breaks:

    mysql -u user -ppass -NBe "SELECT 'SET foreign_key_checks = 0;' 
       UNION SELECT CONCAT('TRUNCATE TABLE myschema.', table_name, ';') 
       FROM INFORMATION_SCHEMA.tables 
       WHERE table_schema = 'myschema' AND table_type = 'BASE TABLE'"
          | mysql -u user -ppass
    

    be sure to use the string without line breaks and replace myschema with your table schema and set the correct login credentials or host for mysql process.

提交回复
热议问题