Skip to content

Instantly share code, notes, and snippets.

@andsens
Created July 16, 2012 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andsens/3122930 to your computer and use it in GitHub Desktop.
Save andsens/3122930 to your computer and use it in GitHub Desktop.
Creates ordered statements to drop all tables in mysql db avoiding most foreign key conflicts
SELECT CONCAT('DROP TABLE `', `t`.`TABLE_SCHEMA`, '`.`', `t`.`TABLE_NAME`, '`;')
FROM `information_schema`.`TABLES` t
LEFT JOIN `information_schema`.`REFERENTIAL_CONSTRAINTS` rc
ON `rc`.`CONSTRAINT_SCHEMA` = `t`.`TABLE_SCHEMA`
AND `rc`.`REFERENCED_TABLE_NAME` = `t`.`TABLE_NAME`
WHERE `TABLE_SCHEMA` = 'dbname'
GROUP BY `t`.`TABLE_NAME`
ORDER BY COUNT(`t`.`TABLE_NAME`) ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment