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.
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
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
I found this to drop all tables in a database:
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:
works for me --edit, fixing a typo in the last command
I know this isn't exactly one command, but the desired result can be achieved from within phpMyAdmin by following these steps:
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.
Soln 1)
Soln 2)
-- edit ----
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.
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:
Once you have this stored procedure in your util database, you can call it like
which is now exactly one statement :-)