Is there a query (command) to truncate all the tables in a database in one operation? I want to know if I can do this with one single query.
Drop (i.e. remove tables)
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done
Truncate (i.e. empty tables)
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "truncate table $table" DATABASE_NAME; done
The following query will generate a list of individual truncate commands for all database tables in a Mysql schema(s). (Replace dbSchemaName1
with name of your Db schema.)
SELECT CONCAT('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES WHERE table_schema IN ('dbSchemaName1','dbSchemaName2');
Copy the query results (which might look like the following) and paste the list of truncate commands into a SQL query tab in MySQL Worbench or your query command tool of choice:
TRUNCATE TABLE dbSchemaName1.table1;
TRUNCATE TABLE dbSchemaName1.table2;
TRUNCATE TABLE dbSchemaName1.table3;
Note: you may receive the following error:
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
This occurs if there are tables with foreign keys references to the table you are trying to drop/truncate.
To resolve this turn off foreign key checks before running the truncate commands:
SET FOREIGN_KEY_CHECKS=0; -- turn off foreign key checks
TRUNCATE TABLE dbSchemaName1.table1; -- truncate tables
TRUNCATE TABLE dbSchemaName1.table2;
TRUNCATE TABLE dbSchemaName1.table3;
SET FOREIGN_KEY_CHECKS=1; -- turn on foreign key checks
Use phpMyAdmin in this way:
Database View => Check All (tables) => Empty
If you want to ignore foreign key checks, you can uncheck the box that says:
[ ] Enable foreign key checks
You'll need to be running atleast version 4.5.0 or higher to get this checkbox.
Its not MySQL CLI-fu, but hey, it works!
MS SQL Server 2005+ (Remove PRINT for actual execution...)
EXEC sp_MSforeachtable 'PRINT ''TRUNCATE TABLE ?'''
If your database platform supports INFORMATION_SCHEMA views, take the results of the following query and execute them.
SELECT 'TRUNCATE TABLE ' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
Try this for MySQL:
SELECT Concat('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES
Adding a semicolon to the Concat makes it easier to use e.g. from within mysql workbench.
SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES
I found this to drop all tables in a database:
mysqldump -uUSERNAME -pPASSWORD --add-drop-table --no-data DATABASENAME | grep ^DROP | mysql -uUSERNAME -pPASSWORD DATABASENAME
Usefull if you are limited by hosting solution (not able to drop a whole database).
I modified it to truncate the tables. There is no "--add-truncate-table" for mysqldump, so i did:
mysqldump -uUSERNAME -pPASSWORD --add-drop-table --no-data DATABASENAME | grep ^DROP | sed -e 's/DROP TABLE IF EXISTS/TRUNCATE TABLE/g' | mysql -uUSERNAME -pPASSWORD DATABASENAME
works for me --edit, fixing a typo in the last command
SET FOREIGN_KEY_CHECKS = 0;
SELECT @str := CONCAT('TRUNCATE TABLE ', table_schema, '.', table_name, ';')
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema IN ('db1_name','db2_name');
PREPARE stmt FROM @str;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
SELECT
to this: AND table_schema = DATABASE();
SELECT
query. @str
doesn't seem to concatenate all tables together.
SELECT @str := GROUP_CONCAT(CONCAT('TRUNCATE TABLE ', table_schema, '.', table_name, '') SEPARATOR ';\n')
I found it most simple to just do something like the code below, just replace the table names with your own. important make sure the last line is always SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=0;
TRUNCATE `table1`;
TRUNCATE `table2`;
TRUNCATE `table3`;
TRUNCATE `table4`;
TRUNCATE `table5`;
TRUNCATE `table6`;
TRUNCATE `table7`;
SET FOREIGN_KEY_CHECKS=1;
This will print the command to truncate all tables:
SELECT GROUP_CONCAT(Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME) SEPARATOR ';') FROM INFORMATION_SCHEMA.TABLES where table_schema in ('my_db');
To truncate a table, one must drop the foreign key constraints mapped to the columns in this table from other tables (in fact on all tables in the specific DB/Schema). So, all foreign key constraints must be dropped initially followed by table truncation. Optionally, use the optimize table (in mysql, innodb engine esp) to reclaim the used data space/size to OS after data truncation. Once data truncation is carried out, create the same foreign key constraints again on the same table. See below a script that would generate the script to carry out the above operations. SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,';') FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY' AND TABLE_SCHEMA='
mysql -e "SET FOREIGN_KEY_CHECKS = 0; drop table $table" DATABASE_NAME
mysql -Nse 'show tables' DATABASE_NAME | while read table; do echo "drop table $table;"; done | mysql DATABASE_NAME