Skip to content

Commit

Permalink
Delete empty tables from an given database.
Browse files Browse the repository at this point in the history
  • Loading branch information
FernandoKGA committed Jul 21, 2019
1 parent 7a97314 commit 7390574
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Delete_Empty_Tables_Procedure/delete_empty_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- https://stackoverflow.com/questions/190776/how-to-have-dynamic-sql-in-mysql-stored-procedure
-- https://dba.stackexchange.com/questions/99298/how-do-i-perform-a-query-on-a-dynamic-list-of-tables
-- https://stackoverflow.com/questions/8393550/mysql-drop-tables-with-wildcard-using-only-sql-statement
-- https://www.w3schools.com/sql/func_mysql_replace.asp
-- https://stackoverflow.com/questions/1524858/create-table-variable-in-mysql

DROP PROCEDURE IF EXISTS deleting_empty_tables;
DELIMITER ;;

CREATE PROCEDURE deleting_empty_tables(IN db_name varchar(255))

BEGIN

DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 1;

DROP TABLE IF EXISTS empty_tables;

CREATE TEMPORARY TABLE empty_tables(
table_name varchar(255)
);

INSERT INTO empty_tables
SELECT t.table_name
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.table_schema = db_name AND t.table_rows = 0;

SELECT COUNT(*) FROM empty_tables INTO n;

WHILE i<=n DO
SET @table_name = (SELECT et.table_name FROM empty_tables et LIMIT 1);
SET @qr = (SELECT 'DROP TABLE db.tb');
SET @qr = REPLACE(@qr,'db',db_name);
SET @qr = REPLACE(@qr,'tb',@table_name);
PREPARE statement FROM @qr;
EXECUTE statement;
DEALLOCATE PREPARE statement;
DELETE FROM empty_tables WHERE table_name = @table_name;
END WHILE;

DROP TABLE empty_tables;

END;;

DELIMITER ;

0 comments on commit 7390574

Please sign in to comment.