DROP TABLE statement allows a table to be removed from a MySQL database. This statement deletes the entire structure as well as the content of the table.
The tutorial explores
DROP statement commands and shows options to drop tables from MySQL databases.
- Access to a terminal window/command line
- A system running MySQL
- A working or test database
- A MySQL user with the necessary privileges (DROP privileges required)
DROP TABLE MySQL Command Syntax
To remove a table in MySQL, use the
DROP TABLE statement. The basic syntax of the command is as follows:
DROP [TEMPORARY] TABLE [IF EXISTS] table_name [, table_name] [RESTRICT | CASCADE];
Let’s break down the syntax:
DROP TABLEstatement deletes a table and its rows permanently.
[TEMPORARY]option ensures you remove temporary tables only.
[IF EXISTS]option drops a table only if it exists.
[RESTRICT]option will be available in future iterations of MySQL. It ensures that a parent row is not deleted if a child row is referencing a value in said parent row.
[CASCADE]option ensures that when you delete a row, all rows in related tables that reference that row are deleted as well. This option will be available in future iterations of MySQL.
Note: When deleting a MySQL table, all user privileges associated with that table remain. If you create a new table with the same name as the deleted one, the existing user privileges will be assigned. Consider whether that impacts your security.
Use the DROP Statement to Remove a Table
To permanently remove a table, enter the following statement within the MySQL shell:
DROP TABLE table1;
table1 with the name of the table you want to delete. The output confirms that the table has been removed.
Use DROP to Remove Only Existing Tables
MySQL generates an error if you try to drop a table that does not exist. This can be an issue when including the
DROP statement in a predefined script.
To check MySQL if a table exists first:
DROP TABLE IF EXISTS table1;
IF EXISTS option generates a warning if
table1 does not exist.
Using this option prevents a script from getting stuck on an error. Display the warning by entering:
How to DROP Multiple Tables
To drop multiple tables with a single
DROP TABLE IF EXISTS table1, table2, table3;
IF EXISTS option shows one warning as
table1 does not exist.
Note: Consider identifying duplicate values in MySQL databases and then deleting them to improve data efficiency.
How to DROP a Temporary Table
Temporary tables are used to generate and store a data set shortly before using it.
For example, you might decide to store the results of a
SELECT statement with multiple JOIN statements in a temporary table. Next, query the temporary table with ease.
To remove temporary tables without risking losing regular tables, use the
DROP TEMPORARY TABLE IF EXISTS table4;
How to DROP Tables Based on Character Strings
MySQL does not have a built-in command to drop tables that match a string of characters. Instead, use a script to help perform this task.
1. Define the database and string of characters you want to filter:
set @schema = 'tableselection'; set @string = 'table%';
tableselection with the name of your database. Replace
table% with the string of characters you want to select and delete. Make sure to leave the
% wildcard at the end.
2. Create a MySQL statement that selects all of the tables that match the string of characters:
SELECT CONCAT ('DROP TABLE ',GROUP_CONCAT(CONCAT(@schema,'.',table_name)),';') INTO @droptool FROM information_schema.tables WHERE @schema = database() AND table_name LIKE @string;
This code selects all tables with the
table% characters specified from the
information_schema table. It then concatenates them and executes a
DROP statement against the group.
3. Create a selection from the results of this code:
4. Display the contents of the command:
PREPARE stmt FROM @droptool; EXECUTE stmt; DEALLOCATE PREPARE stmt;
The code in the section below is presented in its entirety for ease of use:
set @schema = 'tableselection'; set @string = 'table%'; SELECT CONCAT ('DROP TABLE ',GROUP_CONCAT(CONCAT(@schema,'.',table_name)),';') INTO @droptool FROM information_schema.tables WHERE @schema = database() AND table_name LIKE @string; SELECT @droptool; PREPARE stmt FROM @droptool; EXECUTE stmt; DEALLOCATE PREPARE stmt;
Note: If you are interested in how to create index in new table, read our guide How to Create or Add an Index in MySQL With Examples.
You have learned how to use Drop Table to remove tables from a MySQL database. The options presented in this tutorial provide you with full control.
Additionally, you are now able to create code that helps find and delete tables based on character strings. Learn about the most important MySQL commands and how to use them in our MySQL commands cheat sheet article with a downloadable PDF.