DROP TABLE
DROP TABLE
Section titled βDROP TABLEβWhat it means
Section titled βWhat it meansβDROP TABLE permanently removes a tableβs structure and all its data from the database β unlike DELETE, which removes rows but keeps the table, and unlike TRUNCATE, which empties a table but keeps its structure. Thereβs no built-in βundoβ beyond restoring from a backup.
Examples
Section titled βExamplesβDROP TABLE Orders;
-- Guard against errors if the table might not existDROP TABLE IF EXISTS Orders;
-- Dropping multiple tables at onceDROP TABLE IF EXISTS Orders, OrderItems;Common mistake
Section titled βCommon mistakeβConfusing DROP TABLE, TRUNCATE TABLE, and DELETE FROM β they sound similar but have very different blast radii: DELETE removes some or all rows but the table remains; TRUNCATE empties all rows but keeps the table structure; DROP removes the table entirely, structure included.
DROP TABLE Orders; -- table is GONE -- structure, indexes, constraints, everythingTRUNCATE TABLE Orders; -- table still exists, empty, structure/indexes intactDELETE FROM Orders; -- table still exists, rows removed (optionally filtered), structure intactQuick practice
Section titled βQuick practiceβ-
Whatβs the key difference between
DROP TABLEandTRUNCATE TABLE?Answer
DROP TABLEremoves the table's structure entirely;TRUNCATE TABLEempties all rows but keeps the table (and its columns, indexes, constraints) intact. -
What does
DROP TABLE IF EXISTS Orders;do differently from a plainDROP TABLE Orders;?Answer
It doesn't raise an error if theOrderstable doesn't exist β useful in scripts that need to run safely whether or not the table is already present. -
After dropping a table, whatβs the standard way to recover it if it turns out to have been a mistake?
Answer
Restore from a database backup β there's no built-in "undo" forDROP TABLEitself.