Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: deleting tables with prefix

How to delete my tables who all have the prefix myprefix_?

Note: need to execute it in phpMyAdmin

like image 575
Deniz Zoeteman Avatar asked Oct 19 '09 15:10

Deniz Zoeteman


People also ask

How do you delete multiple tables at a time in SQL?

Alternatively, you can also hit keyboard option F7 and it will open up Object Explorer Details. In Object Explorer Details, select the tables which you want to delete and either hit the keyboard button DELETE or just go right click on the tables and select the option DELETE.


1 Answers

You cannot do it with just a single MySQL command, however you can use MySQL to construct the statement for you:

In the MySQL shell or through PHPMyAdmin, use the following query

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )      AS statement FROM information_schema.tables      WHERE table_name LIKE 'myprefix_%'; 

This will generate a DROP statement which you can than copy and execute to drop the tables.

EDIT: A disclaimer here - the statement generated above will drop all tables in all databases with that prefix. If you want to limit it to a specific database, modify the query to look like this and replace database_name with your own database_name:

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )      AS statement FROM information_schema.tables      WHERE table_schema = 'database_name' AND table_name LIKE 'myprefix_%'; 
like image 194
Andre Miller Avatar answered Sep 28 '22 14:09

Andre Miller