MySQL — How to drop multiple tables when foreign keys are preventing that

This is a nice solution I found on StackOverFlow.com when I was looking for a way to remove some tables that had a lot of foreign keys  –  these keys were not allowing me to do the drop easily.

I have found it very useful, here it is with some minor improvements:

SET FOREIGN_KEY_CHECKS = 0; 
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
 FROM information_schema.tables 
 WHERE table_schema = 'your_db_name_here'; -- specify DB name here.
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
If you like this and find it useful please consider sharing it! Cheers!Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
 

C