In my database there is a lot of tables starting with _elgg
, now I want to drop all tables with this prefix. Can anyone give me a solution ?
Thanks in advance!
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityFirst do a
SELECT CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '\_elgg%'
AND TABLE_SCHEMA = 'your_database_name';
and check if this returns the correct statements for every table. If you want to execute those statements, do
SELECT CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '\_elgg%'
AND TABLE_SCHEMA = 'your_database_name'
INTO OUTFILE '/tmp/whatever_filename';
SOURCE /tmp/whatever_filename;
Another way: On the command line, to do it in one rush, you can do
mysql -B databasename -uroot -prootpassword --disable-column-names -e `mysql -B databasename -uroot -prootpassword --disable-column-names -e "SELECT CONCAT('DROP TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '\_elgg%' AND TABLE_SCHEMA = 'your_database_name';"`
views
instead by any chance?
SELECT * FROM (
SELECT CONCAT('DROP TABLE ', GROUP_CONCAT(table_name) , ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE table_name LIKE 'prefix_%'
) a INTO @mystmt;
PREPARE mystatement FROM @mystmt;
EXECUTE mystatement;
Use the following query:
-- Set up variable to delete ALL tables starting with 'temp_'
SET GROUP_CONCAT_MAX_LEN=10000;
SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'my_database'
AND TABLE_NAME LIKE 'temp_%');
SET @delStmt = CONCAT('DROP TABLE ', @tbls);
-- SELECT @delStmt;
PREPARE stmt FROM @delStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You need to change at TABLE_SCHEMA
and TABLE_NAME
appropriately.