Lesson 29 +10 XP

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

CommandRemovesKeeps
DELETErows (can use WHERE)table
TRUNCATEall rowstable structure
DROPtable and its structurenothing

TL;DR

  • DROP TABLE deletes the whole table.
  • TRUNCATE TABLE empties the table, keeping its structure.
  • DELETE removes rows and can use WHERE.