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
MS SQL Server 2005+ (Remove PRINT for actual execution...)
If your database platform supports INFORMATION_SCHEMA views, take the results of the following query and execute them.
Try this for MySQL:
Adding a semicolon to the Concat makes it easier to use e.g. from within mysql workbench.
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.
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.
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 will print the command to truncate all tables:
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: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 usage guide from dev.mysql
The following MySQL query will itself produce a single query that will truncate all tables in a given database. It bypasses FOREIGN keys: