tags:

views:

133

answers:

6

I have 100 tables, 40,000 rows in each table. I want to go into MySQL and delete all rows from all tables.

...in 1 statement, if possible?

I want to keep the database and tables.

+2  A: 

It's possible, but it has side-effects you might not like:

drop database <dbname>;

It means the tables' structures are deleted, as well as the indexes, stored procedures, etc., etc.

The only other way would be to write a stored procedure which loops somehow with

truncate table <tablename>;
wallyk
I want to keep the database and tables.
TIMEX
+1  A: 

Just a guess...try it

TRUNCATE TABLE *;

Or

TRUNCATE TABLE table1, table2, table3;

Or

DELETE FROM table1, table2, table3 WHERE 1=1;

Daniel
I've never seen that advertised nor implemented anywhere. Does it work?
wallyk
No idea, but it would be nice if it did!
Daniel
The "WHERE 1=1" is excessive, and thus not needed at all.
it doesn't work :)
TIMEX
+2  A: 

I don't think so (but I've been wrong before). What I tend to do is those cases is a two-step process.

If your DBMS has a command line interface, you can use it to create a script to do the bulk of the work, something like:

db2 "select 'db2 delete from ' | tblname from sysibm.systables
    where owner = 'pax'" >p2.sh
p2.sh

The first bit simply creates a p2.sh file (or a p2.cmd file under Windows) containing a delete from statement for every table owned by pax. Then you just run that command file to do the dirty work. You may want to check it first, of course :-)

Not the one-step process you were looking for but still very simple. I'm assuming here that mysql also has a command line interface.

Update:

The MySQL version of the above looks like it should be:

echo "select 'mysql truncate table ' | table_name
              from information_schema.tables" | mysql >p2.sh
bash p2.sh

This uses the truncate command which is usually more efficient than delete from for deleting all rows. It also uses the proper MySQL system tables to get the table names.

One point though - you may want to put a where clause on that select to limit the tables to those you want deleted. The query as it stands will try to delete every table. One possibility is to limit it with specific table_schema and/or table_type values.

paxdiablo
+2  A: 

This will require a stored procedure or script that loops through each table and does:

truncate table <tablename>

To get the list of tables to truncate you can do something like:

SELECT table_name 
FROM INFORMATION_SCHEMA.tables 
WHERE table_schema = 'db_name'
Taylor Leese
+1: You'd be looking at using a Prepared Statement to be able to iterate through the tables: http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
OMG Ponies
+1  A: 

Easiest method to truncate all tables while retaining schema.

mysqldump -d -uuser -ppass --add-drop-table databasename > databasename.sql

mysql -uuser -ppass databasename < databasename.sql

Not sure if it will retain stored procedures as they are not in use where I work, but I use this regularly to reset databases.

The -d switch on mysqldump means "don't dump data."

The --add-drop-table prepends a DROP TABLE statement to every CREATE TABLE in the dump.

zen
+2  A: 

Export the SQL script, delete the database, recreate the database against the script. :)

F.Aquino