Question №34
Remaining:
Explain the difference between DELETE, TRUNCATE, and DROP
Sample Answer
Show Answer by Default
DELETE
- Removes selected records from a table.
- A WHERE condition can be used to delete specific records.
- The operation is logged in the transaction log row by row.
- DELETE triggers are activated.
Syntax
MySQL 8.4DELETE FROM table_name [WHERE condition];
TRUNCATE
- Removes all data from the table.
- WHERE cannot be used.
- Faster because it does not log the deletion of each row.
- Resets the auto-increment counter: always in MySQL, with RESTART IDENTITY in PostgreSQL.
- DELETE triggers are not activated (PostgreSQL lets you create a separate trigger for TRUNCATE).
- Can be undone with ROLLBACK in PostgreSQL, but not in MySQL: the command implicitly commits the transaction.
Syntax
MySQL 8.4TRUNCATE TABLE table_name;
DROP
- Deletes the entire table along with its data, structure, indexes, and constraints.
- In MySQL the action is irreversible; in PostgreSQL dropping a table inside a transaction can be undone with ROLLBACK.
Syntax
MySQL 8.4DROP TABLE table_name;
Choosing between commands
- Use DELETE when you need to remove specific records.
- Use TRUNCATE to quickly remove all data from the table while keeping its structure.
- Use DROP to completely remove the table from the database.
For a deeper dive with examples, see the article “TRUNCATE vs DELETE vs DROP: The Difference”.