Angelin Nadar
Angelin Nadar

Reputation: 9300

MySQL: how to drop multiple tables using single query?

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_'

Upvotes: 18

Views: 33044

Answers (8)

Payel Senapati
Payel Senapati

Reputation: 1356

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_ ..........;

Upvotes: 2

Sohail Iqbal
Sohail Iqbal

Reputation: 15

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

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

Thanks

Upvotes: -3

Funkyhead
Funkyhead

Reputation: 21

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**%';

Upvotes: 2

darkone_vt777
darkone_vt777

Reputation: 1

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.

Upvotes: 0

krishnachaitanya
krishnachaitanya

Reputation: 1

Dropping single table in mysql:

DROP TABLE TABLE_NAME;

Upvotes: -4

John P
John P

Reputation: 15245

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;

Upvotes: 17

marlboro51
marlboro51

Reputation: 41

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

Upvotes: 4

Angelin Nadar
Angelin Nadar

Reputation: 9300

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;

Upvotes: 10

Related Questions