Sobes.tech
Middle

What is the difference between TRUNCATE and DELETE?

sobes.tech AI

Answer from AI

Difference between TRUNCATE and DELETE:

  • DELETE is an SQL operator that removes rows from a table based on a condition (if no condition is specified, it deletes all rows). It logs each row deletion, allowing rollback. Triggers are activated when deleting with DELETE.

  • TRUNCATE is a command that quickly removes all rows from a table, effectively resetting it. TRUNCATE does not log each row deletion but works at the data page level, making it faster. Usually, TRUNCATE cannot be used with a condition and does not activate triggers. Also, TRUNCATE often cannot be rolled back in some DBMS.

Example:

DELETE FROM employees WHERE department = 'Sales'; -- deletes only sales department employees
TRUNCATE TABLE employees; -- quickly and completely deletes all employees