Summary: In this chapter, we will show you how to remove existing tables using the MySQL DROP TABLE statement.
Using MySQL DROP TABLE To Remove Existing Tables
The MySQL DROP TABLE statement allows you to remove or delete a table from the MySQL database.
MySQL DROP TABLE statement syntax
In its simplest form, the syntax for the DROP TABLE statement in MySQL is:
1
| DROP TABLE table_name; |
However, the full syntax for the MySQL DROP TABLE statement is:
1
2
3
| DROP [ TEMPORARY ] TABLE [IF EXISTS] table_name [, table_name] ... [ RESTRICT | CASCADE ] |
Parameters or Arguments
- TEMPORARY: It specifies that only temporary tables should be dropped by the DROP TABLE statement.
- table_name: The name of the table to remove from the database.
- table_name1, table_name2: The tables to remove from the database, if removing more than one table in the DROP TABLE statement.
- IF EXISTS: If specified, the DROP TABLE statement will not raise an error if one of the tables does not exist.
- RESTRICT: It has no impact or effect on the DROP TABLE statement but is included in the syntax to make porting the tables to different databases easier.
- CASCADE : It has no impact or effect on the DROP TABLE statement but is included in the syntax to make porting the tables to different databases easier.
Note: If you use the MySQL DROP TABLE statement to drop one or more tables that do not exist, the database will raise an error unless you specify the IF EXISTS parameter in the DROP TABLE statement.
MySQL DROP TABLE example
Let’s look at an example that shows how to drop a table using the MySQL DROP TABLE statement.
Drop One Table
First, let’s look at a simple DROP TABLE example that shows how to use the DROP TABLE statement to drop one table in MySQL.
1
| DROP TABLE contacts; |
This DROP TABLE example would delete the table called contacts.
Drop Multiple Tables
Let’s look at an example where we want to drop more than one table using the DROP TABLE statement:
1
| DROP TABLE customers, contacts; |
This DROP TABLE statement example would delete two tables – customers and contacts. If we were worried that one of the tables doesn’t exist and we don’t want to raise an error, we could modify our DROP TABLE statement as follows:
1
| DROP TABLE IF EXISTS customers, contacts; |
This example would delete the customers and contacts tables and would not raise an error if one of the tables didn’t exist.
Drop Temporary Table
Finally, let’s look at an example that shows how to use the DROP TABLE statement to drop a temporary table.
1
| DROP TEMPORARY TABLE IF EXISTS customers; |
This DROP TABLE example will only delete the temporary table called customers. If there was also a permanent table called customers, this DROP TABLE statement would not delete it because TEMPORARY is specified.
In this chapter, 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.
MySQL ALTER TABLE
MySQL ALTER TABLE