Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?
+2
A:
From http://www.devdaily.com/blog/post/mysql/drop-mysql-tables-in-any-order-foreign-keys:
SET FOREIGN_KEY_CHECKS = 0;
drop table if exists customers;
drop table if exists orders;
drop table if exists order_details;
SET FOREIGN_KEY_CHECKS = 1;
chiccodoro
2010-08-13 12:33:30
+2
A:
You can do:
select concat('drop table if exists ', table_name, ' cascade;')
from information_schema.tables;
Then run the generated queries. They will drop every single table on the current database.
Here is some help on drop table
command.
Pablo Santa Cruz
2010-08-13 12:33:40
The above answer assumes that `||` is set to be the concatenation operator. More specifically, MySQL SQL mode contains `PIPES_AS_CONCAT`. Reference: http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_pipes_as_concat
Ionuț G. Stan
2010-08-13 12:37:52
@Ionut: cool! thanks for pointing that out. Fixed the code sample to use `concat` instead of `||`
Pablo Santa Cruz
2010-08-13 12:40:49