MySQL: how to drop multiple tables using single query?
Asked Answered
A

8

18

I want to drop multiple tables with ease without actually listing the table names in the drop query and the tables to be deleted have prefix say 'wp_'

Areca answered 20/7, 2011 at 7:48 Comment(1)
Similar question - #4958924Commutate
A
10

Just sharing one of the solutions:

mysql> SELECT CONCAT( "DROP TABLE ",
GROUP_CONCAT(TABLE_NAME) ) AS stmt

FROM information_schema.TABLES

WHERE TABLE_SCHEMA = "your_db_name" AND TABLE_NAME LIKE "ur condition" into outfile '/tmp/a.txt';

mysql> source /tmp/a.txt;

Areca answered 20/7, 2011 at 7:48 Comment(0)
S
17

I've used a query very similar to Angelin's. In case you have more than a few tables, one has to increase the max length of group_concat. Otherwise the query will barf on the truncated string that group_concat returns.

This is my 10 cents:

-- Increase memory to avoid truncating string, adjust according to your needs
SET group_concat_max_len = 1024 * 1024 * 10;
-- Generate drop command and assign to variable
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @dropcmd FROM information_schema.tables WHERE table_schema='databasename' AND table_name LIKE 'my_table%';
-- Drop tables
PREPARE str FROM @dropcmd; EXECUTE str; DEALLOCATE PREPARE str;
Sputum answered 16/10, 2012 at 5:9 Comment(1)
Thank you for this - without the group_concat_max_len increase, Angelin's answer would never have worked for my scenario.Lozano
A
10

Just sharing one of the solutions:

mysql> SELECT CONCAT( "DROP TABLE ",
GROUP_CONCAT(TABLE_NAME) ) AS stmt

FROM information_schema.TABLES

WHERE TABLE_SCHEMA = "your_db_name" AND TABLE_NAME LIKE "ur condition" into outfile '/tmp/a.txt';

mysql> source /tmp/a.txt;

Areca answered 20/7, 2011 at 7:48 Comment(0)
D
4

Simple solution without risk of error:

mysqldump create a file that contains DROP command like

DROP TABLE IF EXISTS `wp_matable`;

a 'grep' with "DROP TABLE wp_" give us the commands to execute

so drop is made by theses trhee lines (you can edit drop.sql to check which tables would be dropped before)

mysqldump -u user -p database > dump.sql 
grep "DROP TABLE `wp_" dump.sql > drop.sql
mysql -u user -p database < drop.sql
Damiondamita answered 13/1, 2014 at 8:57 Comment(1)
I am not sure if there is an error in your answer but to make the grep command work, I had to write DROP TABLE IF EXISTS instead of just DROP TABLE and I had to escape the backtick with the "\" character. For example: grep "DROP TABLE IF EXISTS \`table_prefix_" dump.sql > drop.sql. I did the export simply with sudo mysqldump > dump.sql and then logged in as root through Unix socket with sudo mysql before selecting the database with use db_name and executed the SQL file with \. drop.sql.Willms
H
2

Be careful with "_", need to be written with "\" before in Mysql like:

SELECT CONCAT('DROP TABLE',GROUP_CONCAT(CONCAT(table_schema,'.',table_name)),';') INTO @dropcmd FROM information_schema.tables WHERE table_schema='databasename' AND table_name LIKE '**my\\_table**%';
Helmer answered 7/3, 2016 at 17:2 Comment(0)
L
2

A less complicated solution when a large number of tables are needed to be deleted -

SELECT GROUP_CONCAT(table_name SEPARATOR ", ")
    -> AS tables
    -> FROM information_schema.tables
    -> WHERE table_schema = "my_database_name"
    -> AND table_name LIKE "wp_%";
+-------------------------------------------------------------------------
| tables              
+-------------------------------------------------------------------------
| wp_t1, wp_t2, wp_t3, wp_t4, wp_t5, wp_t6, wp_t7, wp_t7, wp_ .......... 
+-------------------------------------------------------------------------

Copy the table names. Then use -

DROP TABLE 
    -> wp_t1, wp_t2, wp_t3, wp_t4, wp_t5, wp_t6, wp_t7, wp_t7, wp_ ..........;
Loaf answered 4/10, 2021 at 19:14 Comment(0)
L
0

For the great mysqldump solution it's better to use the option --skip-quote-names

mysqldump --skip-quote-names -u user -p database > dump.sql 
grep "DROP TABLE wp_" dump.sql > drop.sql
mysql -u user -p database < drop.sql

You get rid of backticks in table names. The grep part won't work in some enviroments with the backticks.

Liner answered 12/10, 2015 at 15:52 Comment(1)
You can make the backtick work with grep if you escape it with the character "\": #6759152Willms
C
-3

Go to c:\xampp\mysql\data\your folder

Select multiple tables that you want remove and then press delete button

Thanks

Carmeliacarmelina answered 18/7, 2016 at 13:18 Comment(1)
This makes the assumption that the original poster is using XAMPP and has it installed to that directory.Faulty
R
-4

Dropping single table in mysql:

DROP TABLE TABLE_NAME;

Refugia answered 2/9, 2015 at 3:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.