Question №10
Remaining:

What are the key differences between DELETE and TRUNCATE?

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 row by row in the transaction log.
  • DELETE triggers are activated.
  • Slower compared to TRUNCATE.

Example:

MySQL 8.4
DELETE FROM employees WHERE salary < 30000;

TRUNCATE:

  • Removes all records from a table.
  • Cannot use WHERE.
  • Faster since it is not logged row by 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).
  • Whether it can be rolled back depends on the DBMS: in PostgreSQL TRUNCATE can be undone with ROLLBACK, in MySQL it can't — the command implicitly commits the transaction.

Example:

MySQL 8.4
TRUNCATE TABLE employees;

For a deeper dive with examples, see the article “TRUNCATE vs DELETE vs DROP: The Difference”.