SQL Lesson 18: Dropping Tables
Learn how to remove tables from your database using DROP TABLE statements.
SQL Lesson 18: Dropping tables
In some rare cases, you may want to remove an entire table including all of its data and metadata, and to do so, you can use the DROP TABLE statement, which differs from the DELETE statement in that it also removes the table schema from the database entirely.
Like the CREATE TABLE statement, the database may throw an error if the specified table does not exist, and to suppress that error, you can use the IF EXISTS clause.
In addition, if you have another table that is dependent on columns in table you are removing (for example, with a FOREIGN KEY dependency) then you will have to either update all dependent tables first to remove the dependent rows or to remove those tables entirely.
Danger Zone
WARNING: DROP TABLE is a destructive operation that permanently removes both the table structure and all its data. Unlike DELETE, which only removes rows, DROP TABLE removes the entire table from the database. Always make sure you have backups and that you really want to remove the table completely.
When to use DROP TABLE
- Removing temporary tables: Tables created for temporary data processing
- Database cleanup: Removing unused or obsolete tables
- Schema changes: When restructuring the database design
- Development/testing: Cleaning up test tables
- Migration rollbacks: Removing tables that were added in error
Best practices
- Always backup first: Create backups before dropping important tables
- Check dependencies: Ensure no other tables reference the table you're dropping
- Use IF EXISTS: Prevents errors if the table doesn't exist
- Test in development: Always test DROP operations in a safe environment first
- Document changes: Keep track of schema changes for team coordination
Exercise
We've gone ahead and added a few tables to the Pixar database, but it turns out that most of them aren't going to be used in the application, so we need to clean them up.
Current Database Tables
Query results
Exercise 18 — Tasks
Solve all tasks to continue to the next lesson.