- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL DROP TABLE
SQL DROP TABLE Command
The DROP TABLE command in SQL is used to delete an entire table from the database permanently. This command removes both the table structure and all the data stored in it.
Once a table is dropped, it cannot be recovered unless a backup exists. So, use this command with caution.
Syntax
Following is the basic syntax to drop a table in SQL:
DROP TABLE table_name;
Example
Assume we have created a table named CUSTOMERS using the CREATE TABLE statement as shown below:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
First, we will verify the structure of the CUSTOMERS table using the DESC command, and then proceed to delete it from the database:
DESC CUSTOMERS;
If the table is created successfully, the above command displays the structure of the table as shown below:
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| ID | int | NO | PRI | NULL | |
| NAME | varchar(20) | NO | NULL | ||
| AGE | int | NO | NULL | ||
| ADDRESS | char(25) | YES | NULL | ||
| SALARY | decimal(18,2) | YES | NULL |
This means that the CUSTOMERS table is available in our database, so let us now drop it as shown below:
DROP TABLE CUSTOMERS;
We get the output as shown below:
Query OK, 0 rows affected (0.04 sec)
Verifying the Table Deletion
Now, to verify if the table is actually dropped, you can use the DESC CUSTOMERS command as shown below:
DESC CUSTOMERS;
It displays the following error:
ERROR 1146 (42S02): Table 'testdb.customers' doesn't exist
When you drop a MySQL table using the DROP TABLE command, any privileges that were granted specifically to that table are not removed automatically, they must be revoked manually.
Dropping Multiple Tables
Sometimes you may want to delete more than one table at once. In SQL (supported in MySQL and some other databases), you can drop multiple tables by separating their names with commas in a single DROP TABLE statement.
Syntax
Following is the basic syntax to drop multiple tables at once in MySQL database:
DROP TABLE table1, table2, table3;
Example
The following command drops two tables named salary and CUSTOMERS in MySQL:
DROP TABLE salary, CUSTOMERS;
We get the output as shown below:
Query OK, 0 rows affected (0.05 sec)
DROP TABLE Using IF EXISTS Clause
When you try to drop a table that does not exist, SQL throws an error. To avoid this, you can use the IF EXISTS clause with the DROP TABLE statement. This allows the command to run safely by checking whether the table exists before attempting to drop it.
Syntax
Following is the basic syntax to drop a table using IF EXISTS clause:
DROP TABLE [IF EXISTS] table_name;
Example Without IF EXISTS
In the following example, we drop a table that does not exist, without using the IF EXISTS clause:
DROP TABLE CUSTOMERS;
This will result in the following error:
ERROR 1051 (42S02): Unknown table 'testdb.customers'
Example With IF EXISTS
Now, we drop a table that does not exist, this time using the IF EXISTS clause:
DROP TABLE IF EXISTS CUSTOMERS;
If the table exists, it will be dropped. If it doesn't exist, the command is simply ignored with a warning:
Query OK, 0 rows affected, 1 warning (0.02 sec)
Dropping a TEMPORARY Table
In SQL, you can drop a temporary table by using the TEMPORARY keyword with the DROP TABLE statement. This helps to prevent accidental deletion of regular (non-temporary) tables.
Syntax
Following is the basic syntax to drop a temporary table in MySQL:
DROP TEMPORARY TABLE table_name;
Example
Following is an example to delete a temporary table CUSTOMERS in MySQL:
DROP TEMPORARY TABLE CUSTOMERS;
We get the output as shown below:
Query OK, 0 rows affected (0.05 sec)
Important Points for Dropping a Table
Following are the important points to remember when dropping a table in SQL:
- Once a table is dropped, both the table and its data are permanently deleted and cannot be recovered.
- Always back up your data before dropping a table if there's any chance you might need it later.
- If you only want to remove the data but keep the table structure, consider using the DELETE or TRUNCATE commands instead.
- To drop a table, the user must have ALTER permission on the table and CONTROL permission on the schema containing the table.
Conclusion
The DROP TABLE statement is used to permanently delete a table and all of its data from the database. If you only want to remove the data and keep the structure, use the TRUNCATE command.