Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL DROP all tables, ignoring foreign keys

Is there a nice easy way to drop all tables from a MySQL database, ignoring any foreign key constraints that may be in there?

like image 210
bcmcfc Avatar asked Aug 13 '10 12:08

bcmcfc


People also ask

How do I Drop all tables in MySQL schema?

MySQL drop all tables from MySQL Workbench Navigate to the Schemas tab. Click on your database name, then click the Tables menu. Click on the first table on the list, then hold SHIFT and click on the last table. With all tables highlighted, right-click on one of the tables and select the drop tables option from the ...

Does DROP TABLE remove foreign keys?

If you drop the "child" table first, the foreign key will be dropped as well.

How do I Drop all tables?

Select all of the tables in your database in the Schema Browser clicking on the first table, holding Shift, and clicking on the last table. Right-click on the selected tables and select “Drop (n) Tables…”

How do I force a table to Drop in MySQL?

To remove a table in MySQL, use the DROP TABLE statement. The basic syntax of the command is as follows: DROP [TEMPORARY] TABLE [IF EXISTS] table_name [, table_name] [RESTRICT | CASCADE];


2 Answers

I found the generated set of drop statements useful, and recommend these tweaks:

  1. Limit the generated drops to your database like this:
SELECT concat('DROP TABLE IF EXISTS `', table_name, '`;') FROM information_schema.tables WHERE table_schema = 'MyDatabaseName'; 

Note 1: This does not execute the DROP statements, it just gives you a list of them. You will need to cut and paste the output into your SQL engine to execute them.

Note 2: If you have VIEWs, you'll have to correct each DROP TABLE `VIEW_NAME` statement to DROP VIEW `VIEW_NAME` manually.

  1. Note, per http://dev.mysql.com/doc/refman/5.5/en/drop-table.html, dropping with cascade is pointless / misleading:

"RESTRICT and CASCADE are permitted to make porting easier. In MySQL 5.5, they do nothing."

Therefore, in order for the drop statements to work if you need:

SET FOREIGN_KEY_CHECKS = 0 

This will disable referential integrity checks - so when you are done performing the drops you need, you will want to reset key checking with

SET FOREIGN_KEY_CHECKS = 1 
  1. The final execution should look like:
SET FOREIGN_KEY_CHECKS = 0; -- Your semicolon separated list of DROP statements here SET FOREIGN_KEY_CHECKS = 1; 

NB: to use output of SELECT easier, mysql -B option can help.

like image 101
Dion Truter Avatar answered Sep 20 '22 11:09

Dion Truter


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; 

(Note that this answers how to disable foreign key checks in order to be able to drop the tables in arbitrary order. It does not answer how to automatically generate drop-table statements for all existing tables and execute them in a single script. Jean's answer does.)

like image 20
chiccodoro Avatar answered Sep 18 '22 11:09

chiccodoro