Last active
April 18, 2024 14:07
Drop all tables in mysql database
This file contains hidden or 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