Loading lessons...
DROP and TRUNCATE TABLE
DROP and TRUNCATE TABLE
DROP TABLE
DROP TABLE removes the entire table - both its structure and its data.
DROP TABLE Shippers;
After this, the Shippers table no longer exists.
Drop a table if it exists
Some databases let you avoid errors:
DROP TABLE IF EXISTS Shippers;
TRUNCATE TABLE
TRUNCATE TABLE deletes all rows but keeps the table and its structure.
TRUNCATE TABLE Shippers;
The table stays, just empty.
DROP vs TRUNCATE vs DELETE
| Command | Removes | Keeps |
|---|---|---|
| DELETE | rows (can use WHERE) | table |
| TRUNCATE | all rows | table structure |
| DROP | table and its structure | nothing |
TL;DR
- DROP TABLE deletes the whole table.
- TRUNCATE TABLE empties the table, keeping its structure.
- DELETE removes rows and can use WHERE.