SQL DROP TABLE
DROP TABLE removes a table and all its rows in one shot. No undo unless you're inside a transaction.
Basic form
SQL
DROP TABLE customers;
If exists
SQL
DROP TABLE IF EXISTS customers;
Cascade — drop dependents too
If other tables have foreign keys pointing at this table, the drop fails. To force it:
SQL
DROP TABLE customers CASCADE; -- PostgreSQL -- MySQL: drop the FKs / dependent tables first.
DROP vs TRUNCATE vs DELETE
| Statement | Removes | Reusable table after? |
|---|---|---|
DROP TABLE t | The table and its data | No — table is gone |
TRUNCATE t | All rows, fast, resets auto-increment | Yes |
DELETE FROM t | Selected rows (or all) | Yes |
Transactions can save you
PostgreSQL and SQL Server allow DROP TABLE inside a transaction — you can ROLLBACK the drop. MySQL's MyISAM and most DDL does not roll back. Check before you trust it.
Tip: Before a destructive drop in production, rename the table first:
ALTER TABLE customers RENAME TO customers_to_drop_20260606;. Wait a week, then drop. If anything broke, just rename back.Example
Exercise
Make the drop safe even when the table is missing.
DROP TABLE
EXISTS customers;
Two letters.
Discussion
Loading…