Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

truncate all table in mysql database

Tags:

mysql

Is it possible to truncate all table in mysql database ? what is query for that .

like image 357
chetan Avatar asked Jul 01 '10 12:07

chetan


People also ask

How do I TRUNCATE all tables in a database?

To remove all data from an existing table, use the SQL TRUNCATE TABLE order. You can also use the DROP TABLE command to delete an entire table. But Truncate will remove the entire table structure from the database, and you will need to recreate the table if you want to store any data.

Can we TRUNCATE multiple tables in MySQL?

No, you can only truncate a single table with TRUNCATE command. To truncate multiple tables you can use T-SQL and iterate through table names to truncate each at a time.


1 Answers

Continuing from @Pablo pipes weren't concatenating for me - and I wanted to restrict it to a single database and then only tables

SELECT CONCAT('truncate table ',table_name,';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<<YOUR-DB-NAME>>'
AND TABLE_TYPE = 'BASE TABLE';
like image 174
cloakedninjas Avatar answered Oct 18 '22 11:10

cloakedninjas