Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Last active April 18, 2024 14:07
Show Gist options
  • Save Voronenko/f83dc8e80fc4dd788514b09fb90cbde6 to your computer and use it in GitHub Desktop.
Save Voronenko/f83dc8e80fc4dd788514b09fb90cbde6 to your computer and use it in GitHub Desktop.
Drop all tables in mysql database
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