MySQL - Drop Table

In this tutorial, we will show you how to remove existing tables using the MySQL DROP TABLE statement.
 
MySQL DROP TABLE statement syntax
 
In order to remove existing tables, you use the MySQL DROP TABLE statement. The syntax of the DROP TABLE is as follows:
 
DROP [TEMPORARY] TABLE [IF EXISTS] table_name [, table_name] ...
[RESTRICT | CASCADE]
 
The DROP TABLE statement removes a table and its data permanently from the database. In MySQL, you can also remove multiple tables using a single DROP TABLE statement, each table is separated by a comma (,).
 
The TEMPORARY flag allows you to remove temporary tables only. It is very convenient to ensure that you do not accidentally remove non-temporary tables.
 
The IF EXISTS addition allows you to hide the error message in case one or more tables in the list do not exist. When you use IF EXISTS addition, MySQL generates a NOTE, which can be retrieved by using the SHOW WARNING statement. It is important to notice that the DROP TABLE statement removes all existing tables and issues an error message or a NOTE when you have a non-existent table in the list.
 
As mentioned above, the DROP TABLE statement only removes table and its data. However, it does not remove specific user privileges associated with the table. Therefore if a table with the same name is re-created after that, the existing privileges will apply to the new table, which may pose a security risk.
 
The RESTRICT and CASCADE flags are reserved for the future versions of MySQL.
 
Last but not least, you must have DROP privileges for the table that you want to remove.
 
MySQL DROP TABLE example
 
We are going to remove the tasks table that we created in the previous tutorial on creating tables using CREATE TABLE statement. In addition, we also remove a non-existent table to practice with the SHOW WARNING statement. The statement to remove the tasks table and a non-existent table is as follows:
 
DROP TABLE IF EXISTS tasks, nonexistent_table;
 
If you check the database, you will see that the tasks table was removed. You can check the NOTE, which is generated by MySQL because of non-existent table, by using the SHOW WARNING statement as follows:
 
SHOW WARNINGS;
 
MySQL DROP TABLE LIKE
 
Image you have a lot of tables whose names start with test in your database and you want to save time by removing all of them using a single DROP TABLE statement. Unfortunately, MySQL does not provide the DROP TABLE LIKE statement that can remove tables based on pattern matching like the following:
 
DROP TABLE LIKE '%pattern%'
 
However, there are some workarounds. We will discuss one of them here for your reference.
 
Let’s start creating test* tables for the sake of demonstration.
 
CREATE TABLE IF NOT EXISTS test1(
  id int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY(id)
);
 
CREATE TABLE IF NOT EXISTS test2 LIKE test1;
CREATE TABLE IF NOT EXISTS test3 LIKE test1;
CREATE TABLE IF NOT EXISTS test4 LIKE test1;
 
We’ve created four tables named test1, test2, test3 and test4 with the same table structure.
 
Suppose you want to remove all test* tables at a time, you can follow the steps below:
 
First, you declare two variables that accept database schema and a pattern that you want to the tables to match:
 
-- set table schema and pattern matching for tables
SET @schema = 'classicmodels';
SET @pattern = 'test%';
 
Next, you need to build a dynamic DROP TABLE statement:
 
-- build dynamic sql (DROP TABLE tbl1, tbl2...;)
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(@schema,'.',table_name)),';')
INTO @droplike
FROM information_schema.tables
WHERE @schema = database()
AND table_name LIKE @pattern;
 
Basically, the query instructs MySQL to go to the information_schema table , which contains data on all tables in all databases, and to concatenate all tables in the database @schema (classicmodels ) that matches the pattern @pattern ( test%) with the prefix DROP TABLE. TheGROUP_CONCAT function creates a comma-separated list of tables.
 
Then, we can display the dynamic SQL to verify if it works correctly:
 
-- display the dynamic sql statement
SELECT @droplike;
 
You can see that it works as expected.
 
After that, you can execute the statement using prepared statement in MySQL as follows:
 
-- execute dynamic sql
PREPARE stmt FROM @dropcmd;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
 
For more information on MySQL prepared statement, check it out the MySQL prepared statement tutorial.
Putting it all together.
 
-- set table schema and pattern matching for tables
SET @schema = 'classicmodels';
SET @pattern = 'test%';
 
-- build dynamic sql (DROP TABLE tbl1, tbl2...;)
SELECT CONCAT('DROP TABLE ',GROUP_CONCAT(CONCAT(@schema,'.',table_name)),';')
INTO @droplike
FROM information_schema.tables
WHERE @schema = database()
AND table_name LIKE @pattern;
 
-- display the dynamic sql statement
SELECT @droplike;
 
-- execute dynamic sql
PREPARE stmt FROM @dropcmd;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
 
So if you want to drop multiple tables that have a specific pattern in a database, you just use the script above to save time. All you need to do is replacing the pattern and the database schema in @patternand @schema variables. If you often have to deal with this task, you can always develop a stored procedure based on the script and reuse the stored procedure in the future.
 
In this tutorial, we’ve shown you how to use the DROP TABLE statement to remove existing tables in a particular database. We also discussed about a workaround that allows you to use the DROP TABLE statement to remove tables based on pattern matching.