Delete data from all tables in MYSQL

前端 未结 16 1684
自闭症患者
自闭症患者 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 10:08

    This one works for me in MySQL 5, and it is specific to tables:

    echo 'show tables' | mysql --skip-column-names -u root YOUR_DB | awk '{print "truncate table " $0 ";"}' | mysql -u root YOUR_DB

    Replace YOUR_DB by the name of your database. You have to provide your password twice, so you have a chance to think it again ... ;-)

    0 讨论(0)
  • 2020-12-13 10:10

    This will require a stored procedure or script that loops through each table and does:

    truncate table <tablename>

    To get the list of tables to truncate you can do something like:

    SELECT table_name 
    FROM INFORMATION_SCHEMA.tables 
    WHERE table_schema = 'db_name' 
    
    0 讨论(0)
  • 2020-12-13 10:11

    Easiest method to truncate all tables while retaining schema.

    mysqldump -d -uuser -ppass --add-drop-table databasename > databasename.sql
    
    mysql -uuser -ppass databasename < databasename.sql
    

    Not sure if it will retain stored procedures as they are not in use where I work, but I use this regularly to reset databases.

    The -d switch on mysqldump means "don't dump data."

    The --add-drop-table prepends a DROP TABLE statement to every CREATE TABLE in the dump.

    0 讨论(0)
  • 2020-12-13 10:11

    In command line...

    USE dbname
    
    SET foreign_key_checks = 0;
    TRUNCATE tablename;  //do this for each table you want emptied
    SET foreign_key_checks = 1;
    
    0 讨论(0)
提交回复
热议问题