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;
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='<TABLE SCHEMA>'
UNION
SELECT CONCAT('TRUNCATE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('OPTIMIZE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' ADD CONSTRAINT ',CONSTRAINT_NAME,' FOREIGN KEY(',COLUMN_NAME,')',' REFERENCES ',REFERENCED_TABLE_NAME,'(',REFERENCED_COLUMN_NAME,');') FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'FK%' AND TABLE_SCHEMA='<TABLE SCHEMA>'
INTO OUTFILE "C:/DB Truncate.sql" LINES TERMINATED BY '\n';
Now, run the Db Truncate.sql script generated
Benefits.
1) Reclaim disk space
2) Not needed to drop and recreate the DB/Schema with the same structure
Drawbacks.
1) FK constraints should be names in the table with the name containing 'FK' in the constraint name.
if using sql server 2005, there is a hidden stored procedure that allows you to execute a command or a set of commands against all tables inside a database. Here is how you would call TRUNCATE TABLE with this stored procedure:
EXEC [sp_MSforeachtable] #command1="TRUNCATE TABLE ?"
Here is a good article that elaborates further.
For MySql, however, you could use mysqldump and specify the --add-drop-tables and --no-data options to drop and create all tables ignoring the data. like this:
mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE]
mysqldump usage guide from dev.mysql
Here is my variant to have 'one statement to truncate 'em all'.
First, I am using a separate database named 'util' for my helper stored procedures. The code of my stored procedure to truncate all tables is:
DROP PROCEDURE IF EXISTS trunctables;
DELIMITER ;;
CREATE PROCEDURE trunctables(theDb varchar(64))
BEGIN
declare tname varchar(64);
declare tcursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type <> 'VIEW' AND table_schema = theDb;
SET FOREIGN_KEY_CHECKS = 0;
OPEN tcursor;
l1: LOOP
FETCH tcursor INTO tname;
if tname = NULL then leave l1; end if;
set #sql = CONCAT('truncate `', theDB, '`.`', tname, '`');
PREPARE stmt from #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP l1;
CLOSE tcursor;
SET FOREIGN_KEY_CHECKS = 1;
END ;;
DELIMITER ;
Once you have this stored procedure in your util database, you can call it like
call util.trunctables('nameofdatabase');
which is now exactly one statement :-)
Use this and form the query
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in (db1,db2)
INTO OUTFILE '/path/to/file.sql';
Now use this to use this query
mysql -u username -p </path/to/file.sql
if you get an error like this
ERROR 1701 (42000) at line 3: Cannot truncate a table referenced in a foreign key constraint
the easiest way to go through is at the top of your file add this line
SET FOREIGN_KEY_CHECKS=0;
which says that we don't want to check the foreign key constraints while going through this file.
It will truncate all tables in databases db1 and bd2.
here for i know here
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename1','databasename2');
If cannot delete or update a parent row: a foreign key constraint fails
That happens if there are tables with foreign keys references to the table you are trying to drop/truncate.
Before truncating tables All you need to do is:
SET FOREIGN_KEY_CHECKS=0;
Truncate your tables and change it back to
SET FOREIGN_KEY_CHECKS=1;
user this php code
$truncate = mysql_query("SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';') as tables_query FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename')");
while($truncateRow=mysql_fetch_assoc($truncate)){
mysql_query($truncateRow['tables_query']);
}
?>
check detail here
link
No. There is no single command to truncate all mysql tables at once. You will have to create a small script to truncate the tables one by one.
ref: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
This worked for me. Change database, username and password accordingly.
mysql -Nse 'show tables' -D DATABASE -uUSER -pPWD | while read table; do echo "SET FOREIGN_KEY_CHECKS = 0;drop table \`$table\`;SET FOREIGN_KEY_CHECKS = 1;"; done | mysql DATABASE -uUSER -pPWD
Ans by battousaix is perfect!
I just used his answer and created the final working command for truncate database tables.
mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -Nse 'show tables' DATABASE_NAME | while read table; do mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -e "SET FOREIGN_KEY_CHECKS = 0; truncate table $table" DATABASE_NAME; done
The above command will work perfectly for the MySQL server.
Also, It's included.
SET FOREIGN_KEY_CHECKS = 0
Here is a procedure that should truncate all tables in the local database.
Let me know if it doesn't work and I'll delete this answer.
Untested
CREATE PROCEDURE truncate_all_tables()
BEGIN
-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmd VARCHAR(2000);
-- Declare the cursor
DECLARE cmds CURSOR
FOR
SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES;
-- Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
-- Open the cursor
OPEN cmds;
-- Loop through all rows
REPEAT
-- Get order number
FETCH cmds INTO cmd;
-- Execute the command
PREPARE stmt FROM cmd;
EXECUTE stmt;
DROP PREPARE stmt;
-- End of loop
UNTIL done END REPEAT;
-- Close the cursor
CLOSE cmds;
END;
I find that TRUNCATE TABLE .. has trouble with foreign key constraints, even after a NOCHECK CONSTRAINT ALL, so I use a DELETE FROM statement instead. This does mean that identity seeds are not reset, you could always add a DBCC CHECKIDENT to achieve this.
I Use the code below to print out to the message window the sql for truncating all the tables in the database, before running it. It just makes it a bit harder to make a mistake.
EXEC sp_MSforeachtable 'PRINT ''ALTER TABLE ? NOCHECK CONSTRAINT ALL'''
EXEC sp_MSforeachtable 'print ''DELETE FROM ?'''
EXEC sp_MSforeachtable 'print ''ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'''
I know this isn't exactly one command, but the desired result can be achieved from within phpMyAdmin by following these steps:
Select (all) tables to be removed (Check All)
Select "Drop" / "Truncate" from the "With selected:" list
On the confirmation page ("Do you really want to:") copy the query (everything with the red background)
Go at the top and click on SQL and write: "SET FOREIGN_KEY_CHECKS=0;" then paste the previously copied query
Click "Go"
The idea is to quickly get all the tables from the database (which you do in 5 seconds and 2 clicks) but disable foreign key checks first. No CLI and no dropping the database and adding it again.
PHP single command:
php -r '$d="PUT_YOUR_DB_NAME_HERE"; $q="show tables"; $dt="drop table"; exec("mysql -Nse \"$q\" $d", $o); foreach($o as $e) `mysql -e "$dt $e" $d`;'
Executed PHP script:
$d="PUT_YOUR_DB_NAME_HERE";
$q="show tables";
$dt="drop table";
exec("mysql -Nse \"$q\" $d", $o);
foreach($o as $e)
`mysql -e "$dt $e" $d`;
I find the top answer to be amazing. However it fails when you have an authenticated MySQL Database user.
Here is a solution built on top of the top answer I linked. This solution securely handles authentication without having to:
Type the password for each table
Worry about your password leaking somewhere
Side-effects on other MySQL cnf files (~/.my.cnf) For more details on what these files do, check out resources section at the bottom of this answer.
1. Create local .my.cnf file
vi .temp.my.cnf
[client]
user=<admin_user_goes_here>
password=<admin_password_goes_here>
2. Truncate or drop all tables
2.A Truncate All Tables (empty only)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "truncate table $table" <db_name_goes_here>; done
2.B Drop All Tables (remove entirely)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "drop table $table" <db_name_goes_here>; done
3. Cleanup
Delete your user-password file
rm -rf .temp.my.cnf
Resources:
https://rtcamp.com/tutorials/mysql/mycnf-preference/
https://dev.mysql.com/doc/refman/8.0/en/option-files.html
https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html#option_general_defaults-extra-file
I am not sure but I think there is one command using which you can copy the schema of database into new database, once you have done this you can delete the old database and after this you can again copy the database schema to the old name.
TB=$( mysql -Bse "show tables from DATABASE" );
for i in ${TB};
do echo "Truncating table ${i}";
mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table DATABASE.${i}; set foreign_key_checks=1; set unique_checks=1";
sleep 1;
done
--
David,
Thank you for taking the time to format the code, but this is how it is supposed to be applied.
-Kurt
On a UNIX or Linux box:
Make sure you are in a bash shell. These commands are to be run, from the command line as follows.
Note:
I store my credentials in my ~/.my.cnf file, so I don't need to supply them on the command line.
Note:
cpm is the database name
I am only showing a small sample of the results, from each command.
Find your foreign key constraints:
klarsen#Chaos:~$ mysql -Bse "select concat(table_name, ' depends on ', referenced_table_name)
from information_schema.referential_constraints
where constraint_schema = 'cpm'
order by referenced_table_name"
approval_external_system depends on approval_request
address depends on customer
customer_identification depends on customer
external_id depends on customer
credential depends on customer
email_address depends on customer
approval_request depends on customer
customer_status depends on customer
customer_image depends on customer
List the tables and row counts:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 297
2 approval_external_system 0
3 approval_request 0
4 country 189
5 credential 468
6 customer 6776
7 customer_identification 5631
8 customer_image 2
9 customer_status 13639
Truncate your tables:
klarsen#Chaos:~$ TB=$( mysql -Bse "show tables from cpm" ); for i in ${TB}; do echo "Truncating table ${i}"; mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table cpm.${i}; set foreign_key_checks=1; set unique_checks=1"; sleep 1; done
Truncating table address
Truncating table approval_external_system
Truncating table approval_request
Truncating table country
Truncating table credential
Truncating table customer
Truncating table customer_identification
Truncating table customer_image
Truncating table customer_status
Verify that it worked:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 0
2 approval_external_system 0
3 approval_request 0
4 country 0
5 credential 0
6 customer 0
7 customer_identification 0
8 customer_image 0
9 customer_status 0
10 email_address 0
On a Windows box:
NOTE:
cpm is the database name
C:\>for /F "tokens=*" %a IN ('mysql -Bse "show tables" cpm') do mysql -e "set foreign_key_checks=0; set unique_checks=0; truncate table %a; foreign_key_checks=1; set unique_checks=1" cpm
The following MySQL query will itself produce a single query that will truncate all tables in a given database. It bypasses FOREIGN keys:
SELECT CONCAT(
'SET FOREIGN_KEY_CHECKS=0; ',
GROUP_CONCAT(dropTableSql SEPARATOR '; '), '; ',
'SET FOREIGN_KEY_CHECKS=1;'
) as dropAllTablesSql
FROM ( SELECT Concat('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME) AS dropTableSql
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'DATABASE_NAME' ) as queries
mysqldump -u root -p --no-data dbname > schema.sql
mysqldump -u root -p drop dbname
mysqldump -u root -p < schema.sql
Soln 1)
mysql> select group_concat('truncate',' ',table_name,';') from information_schema.tables where table_schema="db_name" into outfile '/tmp/a.txt';
mysql> /tmp/a.txt;
Soln 2)
- Export only structure of a db
- drop the database
- import the .sql of structure
-- edit ----
earlier in solution 1, i had mentioned concat() instead of group_concat() which would have not returned the desired result
We can write a bash script like below
truncate_tables_in_mysql() {
type mysql >/dev/null 2>&1 && echo "MySQL present." || sudo apt-get install -y mysql-client
tables=$(mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "USE $BACKEND_DATABASE;
SHOW TABLES;")
tables_list=($tables)
query_string="USE $BACKEND_DATABASE; SET FOREIGN_KEY_CHECKS = 0;"
for table in "${tables_list[#]:1}"
do
query_string="$query_string TRUNCATE TABLE \`$table\`; "
done
query_string="$query_string SET FOREIGN_KEY_CHECKS = 1;"
mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "$query_string"
}
You can replace env variables with your MySQL details. Using one command you can truncate all the tables in a DB.
Small addition to #Mathias Bynens's answer. When I run this I got an error because foreign key check
mysql -Nse 'SHOW TABLES' <database_name> | while read table; do mysql -e "SET FOREIGN_KEY_CHECKS=0; DROP TABLE $table" <database_name>; done
If there are views in the database it returns an error. I had to clear views manually by drop view <view_name>;
An idea could be to just drop and recreate the tables?
EDIT:
#Jonathan Leffler: True
Other Suggestion (or case you dont need to truncate ALL tables):
Why not just create a basic stored procedure to truncate specific tables
CREATE PROCEDURE [dbo].[proc_TruncateTables]
AS
TRUNCATE TABLE Table1
TRUNCATE TABLE Table2
TRUNCATE TABLE Table3
GO
<?php
// connect to database
$conn=mysqli_connect("localhost","user","password","database");
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// sql query
$sql =mysqli_query($conn,"TRUNCATE " . TABLE_NAME);
// Print message
if ($sql === TRUE) {
echo 'data delete successfully';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
?>
Here is code snippet which I use to clear a table. Just change $conn info and TABLE_NAME.
Related
I have a db with 100 tables. I want to delete data from all tables using mysql command or in phpmyadmin
Backup your database structure (use mysqldump with --no-data command line option).
Drop database.
Restore database from the dump.
This method have no problems with FOREIGN KEY relations. Rather than DELETE/TRUNCATE usage where you must clear the tables content in definite order (if you'd clear master table before slave one then the deletion will fail due to referential constraint violation).
Use information_schema.TABLES make dynamic query and exeute.
select concat('delete from ',TABLE_NAME,';') from information_schema.TABLES where TABLE_SCHEMA='databasename';
or try this one
SET FOREIGN_KEY_CHECKS = 0;
SET #TABLES = NULL;
SELECT GROUP_CONCAT('delete from ', table_name,';') INTO #TABLES FROM information_schema.tables
WHERE table_schema = 'databasename' and table_name in ('tbl_audit_trail','tbl_celery');
SET #TABLES= replace( #TABLES,',','');
select #TABLES;
copy the result and execute
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;
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='<TABLE SCHEMA>'
UNION
SELECT CONCAT('TRUNCATE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('OPTIMIZE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' ADD CONSTRAINT ',CONSTRAINT_NAME,' FOREIGN KEY(',COLUMN_NAME,')',' REFERENCES ',REFERENCED_TABLE_NAME,'(',REFERENCED_COLUMN_NAME,');') FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'FK%' AND TABLE_SCHEMA='<TABLE SCHEMA>'
INTO OUTFILE "C:/DB Truncate.sql" LINES TERMINATED BY '\n';
Now, run the Db Truncate.sql script generated
Benefits.
1) Reclaim disk space
2) Not needed to drop and recreate the DB/Schema with the same structure
Drawbacks.
1) FK constraints should be names in the table with the name containing 'FK' in the constraint name.
if using sql server 2005, there is a hidden stored procedure that allows you to execute a command or a set of commands against all tables inside a database. Here is how you would call TRUNCATE TABLE with this stored procedure:
EXEC [sp_MSforeachtable] #command1="TRUNCATE TABLE ?"
Here is a good article that elaborates further.
For MySql, however, you could use mysqldump and specify the --add-drop-tables and --no-data options to drop and create all tables ignoring the data. like this:
mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE]
mysqldump usage guide from dev.mysql
Here is my variant to have 'one statement to truncate 'em all'.
First, I am using a separate database named 'util' for my helper stored procedures. The code of my stored procedure to truncate all tables is:
DROP PROCEDURE IF EXISTS trunctables;
DELIMITER ;;
CREATE PROCEDURE trunctables(theDb varchar(64))
BEGIN
declare tname varchar(64);
declare tcursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type <> 'VIEW' AND table_schema = theDb;
SET FOREIGN_KEY_CHECKS = 0;
OPEN tcursor;
l1: LOOP
FETCH tcursor INTO tname;
if tname = NULL then leave l1; end if;
set #sql = CONCAT('truncate `', theDB, '`.`', tname, '`');
PREPARE stmt from #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP l1;
CLOSE tcursor;
SET FOREIGN_KEY_CHECKS = 1;
END ;;
DELIMITER ;
Once you have this stored procedure in your util database, you can call it like
call util.trunctables('nameofdatabase');
which is now exactly one statement :-)
Use this and form the query
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in (db1,db2)
INTO OUTFILE '/path/to/file.sql';
Now use this to use this query
mysql -u username -p </path/to/file.sql
if you get an error like this
ERROR 1701 (42000) at line 3: Cannot truncate a table referenced in a foreign key constraint
the easiest way to go through is at the top of your file add this line
SET FOREIGN_KEY_CHECKS=0;
which says that we don't want to check the foreign key constraints while going through this file.
It will truncate all tables in databases db1 and bd2.
here for i know here
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename1','databasename2');
If cannot delete or update a parent row: a foreign key constraint fails
That happens if there are tables with foreign keys references to the table you are trying to drop/truncate.
Before truncating tables All you need to do is:
SET FOREIGN_KEY_CHECKS=0;
Truncate your tables and change it back to
SET FOREIGN_KEY_CHECKS=1;
user this php code
$truncate = mysql_query("SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';') as tables_query FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename')");
while($truncateRow=mysql_fetch_assoc($truncate)){
mysql_query($truncateRow['tables_query']);
}
?>
check detail here
link
No. There is no single command to truncate all mysql tables at once. You will have to create a small script to truncate the tables one by one.
ref: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
This worked for me. Change database, username and password accordingly.
mysql -Nse 'show tables' -D DATABASE -uUSER -pPWD | while read table; do echo "SET FOREIGN_KEY_CHECKS = 0;drop table \`$table\`;SET FOREIGN_KEY_CHECKS = 1;"; done | mysql DATABASE -uUSER -pPWD
Ans by battousaix is perfect!
I just used his answer and created the final working command for truncate database tables.
mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -Nse 'show tables' DATABASE_NAME | while read table; do mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -e "SET FOREIGN_KEY_CHECKS = 0; truncate table $table" DATABASE_NAME; done
The above command will work perfectly for the MySQL server.
Also, It's included.
SET FOREIGN_KEY_CHECKS = 0
Here is a procedure that should truncate all tables in the local database.
Let me know if it doesn't work and I'll delete this answer.
Untested
CREATE PROCEDURE truncate_all_tables()
BEGIN
-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmd VARCHAR(2000);
-- Declare the cursor
DECLARE cmds CURSOR
FOR
SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES;
-- Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
-- Open the cursor
OPEN cmds;
-- Loop through all rows
REPEAT
-- Get order number
FETCH cmds INTO cmd;
-- Execute the command
PREPARE stmt FROM cmd;
EXECUTE stmt;
DROP PREPARE stmt;
-- End of loop
UNTIL done END REPEAT;
-- Close the cursor
CLOSE cmds;
END;
I find that TRUNCATE TABLE .. has trouble with foreign key constraints, even after a NOCHECK CONSTRAINT ALL, so I use a DELETE FROM statement instead. This does mean that identity seeds are not reset, you could always add a DBCC CHECKIDENT to achieve this.
I Use the code below to print out to the message window the sql for truncating all the tables in the database, before running it. It just makes it a bit harder to make a mistake.
EXEC sp_MSforeachtable 'PRINT ''ALTER TABLE ? NOCHECK CONSTRAINT ALL'''
EXEC sp_MSforeachtable 'print ''DELETE FROM ?'''
EXEC sp_MSforeachtable 'print ''ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'''
I know this isn't exactly one command, but the desired result can be achieved from within phpMyAdmin by following these steps:
Select (all) tables to be removed (Check All)
Select "Drop" / "Truncate" from the "With selected:" list
On the confirmation page ("Do you really want to:") copy the query (everything with the red background)
Go at the top and click on SQL and write: "SET FOREIGN_KEY_CHECKS=0;" then paste the previously copied query
Click "Go"
The idea is to quickly get all the tables from the database (which you do in 5 seconds and 2 clicks) but disable foreign key checks first. No CLI and no dropping the database and adding it again.
PHP single command:
php -r '$d="PUT_YOUR_DB_NAME_HERE"; $q="show tables"; $dt="drop table"; exec("mysql -Nse \"$q\" $d", $o); foreach($o as $e) `mysql -e "$dt $e" $d`;'
Executed PHP script:
$d="PUT_YOUR_DB_NAME_HERE";
$q="show tables";
$dt="drop table";
exec("mysql -Nse \"$q\" $d", $o);
foreach($o as $e)
`mysql -e "$dt $e" $d`;
I find the top answer to be amazing. However it fails when you have an authenticated MySQL Database user.
Here is a solution built on top of the top answer I linked. This solution securely handles authentication without having to:
Type the password for each table
Worry about your password leaking somewhere
Side-effects on other MySQL cnf files (~/.my.cnf) For more details on what these files do, check out resources section at the bottom of this answer.
1. Create local .my.cnf file
vi .temp.my.cnf
[client]
user=<admin_user_goes_here>
password=<admin_password_goes_here>
2. Truncate or drop all tables
2.A Truncate All Tables (empty only)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "truncate table $table" <db_name_goes_here>; done
2.B Drop All Tables (remove entirely)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "drop table $table" <db_name_goes_here>; done
3. Cleanup
Delete your user-password file
rm -rf .temp.my.cnf
Resources:
https://rtcamp.com/tutorials/mysql/mycnf-preference/
https://dev.mysql.com/doc/refman/8.0/en/option-files.html
https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html#option_general_defaults-extra-file
I am not sure but I think there is one command using which you can copy the schema of database into new database, once you have done this you can delete the old database and after this you can again copy the database schema to the old name.
TB=$( mysql -Bse "show tables from DATABASE" );
for i in ${TB};
do echo "Truncating table ${i}";
mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table DATABASE.${i}; set foreign_key_checks=1; set unique_checks=1";
sleep 1;
done
--
David,
Thank you for taking the time to format the code, but this is how it is supposed to be applied.
-Kurt
On a UNIX or Linux box:
Make sure you are in a bash shell. These commands are to be run, from the command line as follows.
Note:
I store my credentials in my ~/.my.cnf file, so I don't need to supply them on the command line.
Note:
cpm is the database name
I am only showing a small sample of the results, from each command.
Find your foreign key constraints:
klarsen#Chaos:~$ mysql -Bse "select concat(table_name, ' depends on ', referenced_table_name)
from information_schema.referential_constraints
where constraint_schema = 'cpm'
order by referenced_table_name"
approval_external_system depends on approval_request
address depends on customer
customer_identification depends on customer
external_id depends on customer
credential depends on customer
email_address depends on customer
approval_request depends on customer
customer_status depends on customer
customer_image depends on customer
List the tables and row counts:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 297
2 approval_external_system 0
3 approval_request 0
4 country 189
5 credential 468
6 customer 6776
7 customer_identification 5631
8 customer_image 2
9 customer_status 13639
Truncate your tables:
klarsen#Chaos:~$ TB=$( mysql -Bse "show tables from cpm" ); for i in ${TB}; do echo "Truncating table ${i}"; mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table cpm.${i}; set foreign_key_checks=1; set unique_checks=1"; sleep 1; done
Truncating table address
Truncating table approval_external_system
Truncating table approval_request
Truncating table country
Truncating table credential
Truncating table customer
Truncating table customer_identification
Truncating table customer_image
Truncating table customer_status
Verify that it worked:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 0
2 approval_external_system 0
3 approval_request 0
4 country 0
5 credential 0
6 customer 0
7 customer_identification 0
8 customer_image 0
9 customer_status 0
10 email_address 0
On a Windows box:
NOTE:
cpm is the database name
C:\>for /F "tokens=*" %a IN ('mysql -Bse "show tables" cpm') do mysql -e "set foreign_key_checks=0; set unique_checks=0; truncate table %a; foreign_key_checks=1; set unique_checks=1" cpm
The following MySQL query will itself produce a single query that will truncate all tables in a given database. It bypasses FOREIGN keys:
SELECT CONCAT(
'SET FOREIGN_KEY_CHECKS=0; ',
GROUP_CONCAT(dropTableSql SEPARATOR '; '), '; ',
'SET FOREIGN_KEY_CHECKS=1;'
) as dropAllTablesSql
FROM ( SELECT Concat('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME) AS dropTableSql
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'DATABASE_NAME' ) as queries
mysqldump -u root -p --no-data dbname > schema.sql
mysqldump -u root -p drop dbname
mysqldump -u root -p < schema.sql
Soln 1)
mysql> select group_concat('truncate',' ',table_name,';') from information_schema.tables where table_schema="db_name" into outfile '/tmp/a.txt';
mysql> /tmp/a.txt;
Soln 2)
- Export only structure of a db
- drop the database
- import the .sql of structure
-- edit ----
earlier in solution 1, i had mentioned concat() instead of group_concat() which would have not returned the desired result
We can write a bash script like below
truncate_tables_in_mysql() {
type mysql >/dev/null 2>&1 && echo "MySQL present." || sudo apt-get install -y mysql-client
tables=$(mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "USE $BACKEND_DATABASE;
SHOW TABLES;")
tables_list=($tables)
query_string="USE $BACKEND_DATABASE; SET FOREIGN_KEY_CHECKS = 0;"
for table in "${tables_list[#]:1}"
do
query_string="$query_string TRUNCATE TABLE \`$table\`; "
done
query_string="$query_string SET FOREIGN_KEY_CHECKS = 1;"
mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "$query_string"
}
You can replace env variables with your MySQL details. Using one command you can truncate all the tables in a DB.
Small addition to #Mathias Bynens's answer. When I run this I got an error because foreign key check
mysql -Nse 'SHOW TABLES' <database_name> | while read table; do mysql -e "SET FOREIGN_KEY_CHECKS=0; DROP TABLE $table" <database_name>; done
If there are views in the database it returns an error. I had to clear views manually by drop view <view_name>;
An idea could be to just drop and recreate the tables?
EDIT:
#Jonathan Leffler: True
Other Suggestion (or case you dont need to truncate ALL tables):
Why not just create a basic stored procedure to truncate specific tables
CREATE PROCEDURE [dbo].[proc_TruncateTables]
AS
TRUNCATE TABLE Table1
TRUNCATE TABLE Table2
TRUNCATE TABLE Table3
GO
<?php
// connect to database
$conn=mysqli_connect("localhost","user","password","database");
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// sql query
$sql =mysqli_query($conn,"TRUNCATE " . TABLE_NAME);
// Print message
if ($sql === TRUE) {
echo 'data delete successfully';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
?>
Here is code snippet which I use to clear a table. Just change $conn info and TABLE_NAME.
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;
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='<TABLE SCHEMA>'
UNION
SELECT CONCAT('TRUNCATE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('OPTIMIZE TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='<TABLE SCHEMA>' AND TABLE_TYPE='BASE TABLE'
UNION
SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' ADD CONSTRAINT ',CONSTRAINT_NAME,' FOREIGN KEY(',COLUMN_NAME,')',' REFERENCES ',REFERENCED_TABLE_NAME,'(',REFERENCED_COLUMN_NAME,');') FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE CONSTRAINT_NAME LIKE 'FK%' AND TABLE_SCHEMA='<TABLE SCHEMA>'
INTO OUTFILE "C:/DB Truncate.sql" LINES TERMINATED BY '\n';
Now, run the Db Truncate.sql script generated
Benefits.
1) Reclaim disk space
2) Not needed to drop and recreate the DB/Schema with the same structure
Drawbacks.
1) FK constraints should be names in the table with the name containing 'FK' in the constraint name.
if using sql server 2005, there is a hidden stored procedure that allows you to execute a command or a set of commands against all tables inside a database. Here is how you would call TRUNCATE TABLE with this stored procedure:
EXEC [sp_MSforeachtable] #command1="TRUNCATE TABLE ?"
Here is a good article that elaborates further.
For MySql, however, you could use mysqldump and specify the --add-drop-tables and --no-data options to drop and create all tables ignoring the data. like this:
mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE]
mysqldump usage guide from dev.mysql
Here is my variant to have 'one statement to truncate 'em all'.
First, I am using a separate database named 'util' for my helper stored procedures. The code of my stored procedure to truncate all tables is:
DROP PROCEDURE IF EXISTS trunctables;
DELIMITER ;;
CREATE PROCEDURE trunctables(theDb varchar(64))
BEGIN
declare tname varchar(64);
declare tcursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type <> 'VIEW' AND table_schema = theDb;
SET FOREIGN_KEY_CHECKS = 0;
OPEN tcursor;
l1: LOOP
FETCH tcursor INTO tname;
if tname = NULL then leave l1; end if;
set #sql = CONCAT('truncate `', theDB, '`.`', tname, '`');
PREPARE stmt from #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP l1;
CLOSE tcursor;
SET FOREIGN_KEY_CHECKS = 1;
END ;;
DELIMITER ;
Once you have this stored procedure in your util database, you can call it like
call util.trunctables('nameofdatabase');
which is now exactly one statement :-)
Use this and form the query
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in (db1,db2)
INTO OUTFILE '/path/to/file.sql';
Now use this to use this query
mysql -u username -p </path/to/file.sql
if you get an error like this
ERROR 1701 (42000) at line 3: Cannot truncate a table referenced in a foreign key constraint
the easiest way to go through is at the top of your file add this line
SET FOREIGN_KEY_CHECKS=0;
which says that we don't want to check the foreign key constraints while going through this file.
It will truncate all tables in databases db1 and bd2.
here for i know here
SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename1','databasename2');
If cannot delete or update a parent row: a foreign key constraint fails
That happens if there are tables with foreign keys references to the table you are trying to drop/truncate.
Before truncating tables All you need to do is:
SET FOREIGN_KEY_CHECKS=0;
Truncate your tables and change it back to
SET FOREIGN_KEY_CHECKS=1;
user this php code
$truncate = mysql_query("SELECT Concat('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';') as tables_query FROM INFORMATION_SCHEMA.TABLES where table_schema in ('databasename')");
while($truncateRow=mysql_fetch_assoc($truncate)){
mysql_query($truncateRow['tables_query']);
}
?>
check detail here
link
No. There is no single command to truncate all mysql tables at once. You will have to create a small script to truncate the tables one by one.
ref: http://dev.mysql.com/doc/refman/5.0/en/truncate-table.html
This worked for me. Change database, username and password accordingly.
mysql -Nse 'show tables' -D DATABASE -uUSER -pPWD | while read table; do echo "SET FOREIGN_KEY_CHECKS = 0;drop table \`$table\`;SET FOREIGN_KEY_CHECKS = 1;"; done | mysql DATABASE -uUSER -pPWD
Ans by battousaix is perfect!
I just used his answer and created the final working command for truncate database tables.
mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -Nse 'show tables' DATABASE_NAME | while read table; do mysql -P 3306 -h YOUR_HOST_HERE -u YOUR_USERNAME_HERE -pYOUR_PASSWORD_HERE -e "SET FOREIGN_KEY_CHECKS = 0; truncate table $table" DATABASE_NAME; done
The above command will work perfectly for the MySQL server.
Also, It's included.
SET FOREIGN_KEY_CHECKS = 0
Here is a procedure that should truncate all tables in the local database.
Let me know if it doesn't work and I'll delete this answer.
Untested
CREATE PROCEDURE truncate_all_tables()
BEGIN
-- Declare local variables
DECLARE done BOOLEAN DEFAULT 0;
DECLARE cmd VARCHAR(2000);
-- Declare the cursor
DECLARE cmds CURSOR
FOR
SELECT CONCAT('TRUNCATE TABLE ', TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES;
-- Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
-- Open the cursor
OPEN cmds;
-- Loop through all rows
REPEAT
-- Get order number
FETCH cmds INTO cmd;
-- Execute the command
PREPARE stmt FROM cmd;
EXECUTE stmt;
DROP PREPARE stmt;
-- End of loop
UNTIL done END REPEAT;
-- Close the cursor
CLOSE cmds;
END;
I find that TRUNCATE TABLE .. has trouble with foreign key constraints, even after a NOCHECK CONSTRAINT ALL, so I use a DELETE FROM statement instead. This does mean that identity seeds are not reset, you could always add a DBCC CHECKIDENT to achieve this.
I Use the code below to print out to the message window the sql for truncating all the tables in the database, before running it. It just makes it a bit harder to make a mistake.
EXEC sp_MSforeachtable 'PRINT ''ALTER TABLE ? NOCHECK CONSTRAINT ALL'''
EXEC sp_MSforeachtable 'print ''DELETE FROM ?'''
EXEC sp_MSforeachtable 'print ''ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'''
I know this isn't exactly one command, but the desired result can be achieved from within phpMyAdmin by following these steps:
Select (all) tables to be removed (Check All)
Select "Drop" / "Truncate" from the "With selected:" list
On the confirmation page ("Do you really want to:") copy the query (everything with the red background)
Go at the top and click on SQL and write: "SET FOREIGN_KEY_CHECKS=0;" then paste the previously copied query
Click "Go"
The idea is to quickly get all the tables from the database (which you do in 5 seconds and 2 clicks) but disable foreign key checks first. No CLI and no dropping the database and adding it again.
PHP single command:
php -r '$d="PUT_YOUR_DB_NAME_HERE"; $q="show tables"; $dt="drop table"; exec("mysql -Nse \"$q\" $d", $o); foreach($o as $e) `mysql -e "$dt $e" $d`;'
Executed PHP script:
$d="PUT_YOUR_DB_NAME_HERE";
$q="show tables";
$dt="drop table";
exec("mysql -Nse \"$q\" $d", $o);
foreach($o as $e)
`mysql -e "$dt $e" $d`;
I find the top answer to be amazing. However it fails when you have an authenticated MySQL Database user.
Here is a solution built on top of the top answer I linked. This solution securely handles authentication without having to:
Type the password for each table
Worry about your password leaking somewhere
Side-effects on other MySQL cnf files (~/.my.cnf) For more details on what these files do, check out resources section at the bottom of this answer.
1. Create local .my.cnf file
vi .temp.my.cnf
[client]
user=<admin_user_goes_here>
password=<admin_password_goes_here>
2. Truncate or drop all tables
2.A Truncate All Tables (empty only)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "truncate table $table" <db_name_goes_here>; done
2.B Drop All Tables (remove entirely)
mysql --defaults-extra-file=.temp.my.cnf -Nse 'show tables' <db_name_goes_here> | while read table; do mysql --defaults-extra-file=.temp.my.cnf -e "drop table $table" <db_name_goes_here>; done
3. Cleanup
Delete your user-password file
rm -rf .temp.my.cnf
Resources:
https://rtcamp.com/tutorials/mysql/mycnf-preference/
https://dev.mysql.com/doc/refman/8.0/en/option-files.html
https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html#option_general_defaults-extra-file
I am not sure but I think there is one command using which you can copy the schema of database into new database, once you have done this you can delete the old database and after this you can again copy the database schema to the old name.
TB=$( mysql -Bse "show tables from DATABASE" );
for i in ${TB};
do echo "Truncating table ${i}";
mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table DATABASE.${i}; set foreign_key_checks=1; set unique_checks=1";
sleep 1;
done
--
David,
Thank you for taking the time to format the code, but this is how it is supposed to be applied.
-Kurt
On a UNIX or Linux box:
Make sure you are in a bash shell. These commands are to be run, from the command line as follows.
Note:
I store my credentials in my ~/.my.cnf file, so I don't need to supply them on the command line.
Note:
cpm is the database name
I am only showing a small sample of the results, from each command.
Find your foreign key constraints:
klarsen#Chaos:~$ mysql -Bse "select concat(table_name, ' depends on ', referenced_table_name)
from information_schema.referential_constraints
where constraint_schema = 'cpm'
order by referenced_table_name"
approval_external_system depends on approval_request
address depends on customer
customer_identification depends on customer
external_id depends on customer
credential depends on customer
email_address depends on customer
approval_request depends on customer
customer_status depends on customer
customer_image depends on customer
List the tables and row counts:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 297
2 approval_external_system 0
3 approval_request 0
4 country 189
5 credential 468
6 customer 6776
7 customer_identification 5631
8 customer_image 2
9 customer_status 13639
Truncate your tables:
klarsen#Chaos:~$ TB=$( mysql -Bse "show tables from cpm" ); for i in ${TB}; do echo "Truncating table ${i}"; mysql -e "set foreign_key_checks=0; set unique_checks=0;truncate table cpm.${i}; set foreign_key_checks=1; set unique_checks=1"; sleep 1; done
Truncating table address
Truncating table approval_external_system
Truncating table approval_request
Truncating table country
Truncating table credential
Truncating table customer
Truncating table customer_identification
Truncating table customer_image
Truncating table customer_status
Verify that it worked:
klarsen#Chaos:~$ mysql -Bse "SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cpm'" | cat -n
1 address 0
2 approval_external_system 0
3 approval_request 0
4 country 0
5 credential 0
6 customer 0
7 customer_identification 0
8 customer_image 0
9 customer_status 0
10 email_address 0
On a Windows box:
NOTE:
cpm is the database name
C:\>for /F "tokens=*" %a IN ('mysql -Bse "show tables" cpm') do mysql -e "set foreign_key_checks=0; set unique_checks=0; truncate table %a; foreign_key_checks=1; set unique_checks=1" cpm
The following MySQL query will itself produce a single query that will truncate all tables in a given database. It bypasses FOREIGN keys:
SELECT CONCAT(
'SET FOREIGN_KEY_CHECKS=0; ',
GROUP_CONCAT(dropTableSql SEPARATOR '; '), '; ',
'SET FOREIGN_KEY_CHECKS=1;'
) as dropAllTablesSql
FROM ( SELECT Concat('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME) AS dropTableSql
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'DATABASE_NAME' ) as queries
mysqldump -u root -p --no-data dbname > schema.sql
mysqldump -u root -p drop dbname
mysqldump -u root -p < schema.sql
Soln 1)
mysql> select group_concat('truncate',' ',table_name,';') from information_schema.tables where table_schema="db_name" into outfile '/tmp/a.txt';
mysql> /tmp/a.txt;
Soln 2)
- Export only structure of a db
- drop the database
- import the .sql of structure
-- edit ----
earlier in solution 1, i had mentioned concat() instead of group_concat() which would have not returned the desired result
We can write a bash script like below
truncate_tables_in_mysql() {
type mysql >/dev/null 2>&1 && echo "MySQL present." || sudo apt-get install -y mysql-client
tables=$(mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "USE $BACKEND_DATABASE;
SHOW TABLES;")
tables_list=($tables)
query_string="USE $BACKEND_DATABASE; SET FOREIGN_KEY_CHECKS = 0;"
for table in "${tables_list[#]:1}"
do
query_string="$query_string TRUNCATE TABLE \`$table\`; "
done
query_string="$query_string SET FOREIGN_KEY_CHECKS = 1;"
mysql -h 127.0.0.1 -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD -e "$query_string"
}
You can replace env variables with your MySQL details. Using one command you can truncate all the tables in a DB.
Small addition to #Mathias Bynens's answer. When I run this I got an error because foreign key check
mysql -Nse 'SHOW TABLES' <database_name> | while read table; do mysql -e "SET FOREIGN_KEY_CHECKS=0; DROP TABLE $table" <database_name>; done
If there are views in the database it returns an error. I had to clear views manually by drop view <view_name>;
An idea could be to just drop and recreate the tables?
EDIT:
#Jonathan Leffler: True
Other Suggestion (or case you dont need to truncate ALL tables):
Why not just create a basic stored procedure to truncate specific tables
CREATE PROCEDURE [dbo].[proc_TruncateTables]
AS
TRUNCATE TABLE Table1
TRUNCATE TABLE Table2
TRUNCATE TABLE Table3
GO
<?php
// connect to database
$conn=mysqli_connect("localhost","user","password","database");
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// sql query
$sql =mysqli_query($conn,"TRUNCATE " . TABLE_NAME);
// Print message
if ($sql === TRUE) {
echo 'data delete successfully';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
?>
Here is code snippet which I use to clear a table. Just change $conn info and TABLE_NAME.
I want to drop multiple tables with ease without actually listing the table names in the drop query and the tables to be deleted have prefix say 'wp_'
I've used a query very similar to Angelin's. In case you have more than a few tables, one has to increase the max length of group_concat. Otherwise the query will barf on the truncated string that group_concat returns.
This is my 10 cents:
-- Increase memory to avoid truncating string, adjust according to your needs
SET group_concat_max_len = 1024 * 1024 * 10;
-- Generate drop command and assign to variable
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO #dropcmd FROM information_schema.tables WHERE table_schema='databasename' AND table_name LIKE 'my_table%';
-- Drop tables
PREPARE str FROM #dropcmd; EXECUTE str; DEALLOCATE PREPARE str;
Just sharing one of the solutions:
mysql> SELECT CONCAT(
"DROP TABLE ",
GROUP_CONCAT(TABLE_NAME)
) AS stmt
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "your_db_name" AND TABLE_NAME LIKE "ur
condition" into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;
Simple solution without risk of error:
mysqldump create a file that contains DROP command like
DROP TABLE IF EXISTS `wp_matable`;
a 'grep' with "DROP TABLE wp_" give us the commands to execute
so drop is made by theses trhee lines (you can edit drop.sql to check which tables would be dropped before)
mysqldump -u user -p database > dump.sql
grep "DROP TABLE `wp_" dump.sql > drop.sql
mysql -u user -p database < drop.sql
Be careful with "_", need to be written with "\" before in Mysql like:
SELECT CONCAT('DROP TABLE',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO #dropcmd FROM information_schema.tables WHERE table_schema='databasename' AND table_name LIKE '**my\\_table**%';
A less complicated solution when a large number of tables are needed to be deleted -
SELECT GROUP_CONCAT(table_name SEPARATOR ", ")
-> AS tables
-> FROM information_schema.tables
-> WHERE table_schema = "my_database_name"
-> AND table_name LIKE "wp_%";
+-------------------------------------------------------------------------
| tables
+-------------------------------------------------------------------------
| wp_t1, wp_t2, wp_t3, wp_t4, wp_t5, wp_t6, wp_t7, wp_t7, wp_ ..........
+-------------------------------------------------------------------------
Copy the table names. Then use -
DROP TABLE
-> wp_t1, wp_t2, wp_t3, wp_t4, wp_t5, wp_t6, wp_t7, wp_t7, wp_ ..........;
For the great mysqldump solution it's better to use the option --skip-quote-names
mysqldump --skip-quote-names -u user -p database > dump.sql
grep "DROP TABLE wp_" dump.sql > drop.sql
mysql -u user -p database < drop.sql
You get rid of backticks in table names. The grep part won't work in some enviroments with the backticks.
Go to c:\xampp\mysql\data\your folder
Select multiple tables that you want remove and then press delete button
Thanks
Dropping single table in mysql:
DROP TABLE TABLE_NAME;
It is possbile set/reset the AUTO_INCREMENT value of a MySQL table via
ALTER TABLE some_table AUTO_INCREMENT = 1000
However I need to set the AUTO_INCREMENTupon its existing value (to fix M-M replication), something like:
ALTER TABLE some_table SET AUTO_INCREMENT = AUTO_INCREMENT + 1 which is not working
Well actually, I would like to run this query for all tables within a database. But actually this is not very crucial.
I could not find out a way to deal with this problem, except running the queries manually. Will you please suggest something or point me out to some ideas.
Thanks
Using:
ALTER TABLE some_table AUTO_INCREMENT = 0
...will reset the auto_increment value to be the next value based on the highest existing value in the auto_increment column.
To run this over all the tables, you'll need to use MySQL's dynamic SQL syntax called PreparedStatements because you can't supply the table name for an ALTER TABLE statement as a variable. You'll have to loop over the output from:
SELECT t.table_name
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_schema = 'your_database_name'
...running the ALTER TABLE statement above for each table.
set #db = 'your_db_name';
SELECT concat('ALTER TABLE ', #db, '.', TABLE_NAME, ' AUTO_INCREMENT = 0;')
FROM information_schema.TABLES WHERE TABLE_SCHEMA = #db AND TABLE_TYPE = 'BASE TABLE'
Then copy-paste and run the output you get.
In the below instructions you will need to replace everything that is in [brackets] with your correct value. BACKUP BEFORE ATTEMPTING.
If you can login to mysql as root through the command line then you could do the following to reset the auto_increment on all tables, first we will construct our queries which we want to run:
Make a database backup:
mysqldump -u [uname] -p [dbname] | gzip -9 > [backupfile.sql.gz]
Login:
mysql -u root -p
Set the group_concat_max_length to a higher value so our list of queries doesn't get truncated:
SET group_concat_max_len=100000;
Create our list of queries by using the following:
SELECT GROUP_CONCAT(CONCAT("ALTER TABLE ", table_name, " AUTO_INCREMENT = 0") SEPARATOR ";") FROM information_schema.tables WHERE table_schema = "[DATABASENAME]";
Then you will receive a long string of mysql queries followed by a bunch of dashes. Copy the string of queries to your clipboard, it will look something similar to:
ALTER table1 AUTO_INCREMENT = 0;ALTER table2 AUTO_INCREMENT = 0;...continued...
Change to the database you would like to run the command on:
USE [DATABASENAME];
Then paste the string that is on your clipboard and hit enter to run it. This should run the alter on every table in your database.
Messed up? Restore from your backup, be sure to logout of mysql before running the following (just type exit; to do so)
gzip -d < [backupfile.sql.gz] | mysql -u [uname] -p [dbname]
I will not take responsibility for any damage cause by your use of any of these commands, use at your own risk.
I found this gist on github and it worked like a charm for me: https://gist.github.com/abhinavlal/4571478
The command:
mysql -Nsr -e "SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t WHERE t.table_schema = 'DB_NAME'" | xargs -I {} mysql DB_NAME -e "ALTER TABLE {} AUTO_INCREMENT = 1;"
If your DB requires a password, you unfortunately have to put that in the command for it to work. One work-around (still not great but works) is to put the password in a secure file. You can always delete the file after so the password doesn't stay in your command history:
... | xargs -I {} mysql -u root -p`cat /path/to/pw.txt` DB_NAME -e...
Assuming that you must fix this by amending the auto-increment column rather than the foreign keys in the table decomposing the N:M relationship, and that you can predict what the right values are, try using a temporary table where the relevant column is not auto-increment, then map this back in place of the original table and change the column type to auto-increment afterwards, or truncate the original table and load the data from the temp table.
I have written below procedure change the database name and execute the procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `setAutoIncrement`()
BEGIN
DECLARE done int default false;
DECLARE table_name CHAR(255);
DECLARE cur1 cursor for SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_schema = "buzzer_verifone";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
myloop: loop
fetch cur1 into table_name;
if done then
leave myloop;
end if;
set #sql = CONCAT('ALTER TABLE ',table_name, ' AUTO_INCREMENT = 1');
prepare stmt from #sql;
execute stmt;
drop prepare stmt;
end loop;
close cur1;
END
Execute the procedure above using below line
Call setAutoIncrement();
The Quickest solution to Update/Reset AUTO_INCREMENT in MySQL Database
Ensure that the AUTO_INCREMENT column has not been used as a FOREIGN_KEY on another table.
Firstly, Drop the AUTO_INCREMENT COLUMN as:
ALTER TABLE table_name DROP column_name
Example: ALTER TABLE payments DROP payment_id
Then afterward re-add the column, and move it as the first column in the table
ALTER TABLE table_name ADD column_name DATATYPE AUTO_INCREMENT PRIMARY KEY FIRST
Example: ALTER TABLE payments ADD payment_id INT AUTO_INCREMENT PRIMARY KEY FIRST
Reset mysql table auto increment was very easy, we can do it with single query, please see this http://webobserve.blogspot.com/2011/02/reset-mysql-table-autoincrement.html.