SQL DROP TABLE
The DROP TABLE statement in SQL is used to delete an existing table and all its data permanently from the database. This operation cannot be undone, so it should be used with caution.
Once a table is dropped, all relationships and constraints associated with the table are also removed.
Syntax
DROP TABLE table_name;
Example
Let us drop the Employees table that we created earlier:
Code Example
-- Drop the Employees Table
DROP TABLE Employees;
-- Verify if the Table Exists
SHOW TABLES;
Output
Tables_in_db_name |
---|
Employees |
-- After DROP:
Tables_in_db_name |
---|
(empty set) |
Explanation
- The DROP TABLE statement permanently removes the Employees table from the database.
- The SHOW TABLES command is used to confirm that the table has been deleted.
Best Practices
- Ensure you have a backup of the data before using DROP TABLE.
- Use IF EXISTS to avoid errors when the table doesn't exist, e.g., DROP TABLE IF EXISTS table_name;
.