Last active
April 18, 2024 14:07
-
-
Save Voronenko/f83dc8e80fc4dd788514b09fb90cbde6 to your computer and use it in GitHub Desktop.
Drop all tables in mysql database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET @tables = NULL; | |
SET GROUP_CONCAT_MAX_LEN=1048576; | |
SELECT GROUP_CONCAT('`', table_schema, '`.`', table_name, '`' SEPARATOR ',') INTO @tables | |
FROM information_schema.tables | |
WHERE table_schema = (SELECT DATABASE()); | |
SELECT IF(@tables IS NULL, | |
'SELECT NULL FROM (SELECT NULL) AS `empty` WHERE 0=1', | |
CONCAT('DROP TABLE IF EXISTS ', @tables)) INTO @tables; | |
PREPARE dropTablesStmt FROM @tables; | |
SELECT @@FOREIGN_KEY_CHECKS INTO @SAVED_FOREIGN_KEY_CHECKS; | |
SET FOREIGN_KEY_CHECKS = 0; | |
EXECUTE dropTablesStmt; | |
SET FOREIGN_KEY_CHECKS = @SAVED_FOREIGN_KEY_CHECKS; | |
DEALLOCATE PREPARE dropTablesStmt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment