DROP & TRUNCATE
DELETE is not the only way to get rid of data. SQL gives you two more heavyweight tools — DROP TABLE and TRUNCATE TABLE — that each remove things at a different scale. Knowing exactly what each one does (and does not do) is essential before you ever run one against a real database.
DROP TABLE — remove the table entirely
DROP TABLE deletes the table’s structure and all of its data, permanently. The columns, constraints, indexes, and rows are all gone — it is as if the table never existed.
Dropping a table
DROP TABLE employees;
Most databases also support a safer variant that avoids an error if the table is already gone:
Guarding against a missing table
DROP TABLE IF EXISTS employees;
TRUNCATE TABLE — remove all rows, keep the table
TRUNCATE TABLE deletes every row in a table but leaves the table structure — its columns, constraints, and indexes — intact and ready to receive new data.
Emptying a table
TRUNCATE TABLE order_logs;
Comparing DELETE, TRUNCATE, and DROP
Operation | Removes rows | Removes structure | Can use WHERE? | Resets auto-increment? | Can be rolled back? | Speed |
|---|---|---|---|---|---|---|
DELETE | Yes (selectively) | No | Yes | No | Yes | Slowest |
TRUNCATE | Yes (all rows) | No | No | Usually yes | Depends on database | Fast |
DROP | Yes (all rows) | Yes | No | N/A — table is gone | Depends on database | Fast |
When to use which
Use DELETE when you need to remove a subset of rows based on a condition, or when you need row-level triggers to fire
Use TRUNCATE when you want to empty an entire table quickly and don't need a WHERE clause
Use DROP when you no longer need the table at all — for example, cleaning up a temporary staging table or removing a deprecated feature's schema