Sobes.tech
Junior

What is the difference between DELETE and TRUNCATE commands in SQL?

sobes.tech AI

Answer from AI

Attribute TRUNCATE TABLE DELETE FROM
Data Reset Removes all rows from the table. Removes rows matching the WHERE condition (if specified), or all rows (if WHERE is absent).
Auto-increment Reset Usually resets the auto-increment field value. Does not reset the auto-increment field value.
Triggers Does not activate delete triggers. Activates delete triggers for each deleted row.
Logging Is a DDL operation, usually less detailed logging. Is a DML operation, logged row by row.
Rollback (ROLLBACK) Harder or impossible to rollback (depends on DBMS). Easy to rollback within a transaction.
Performance Significantly faster for complete data removal, as it does not scan each row. Slower when deleting a large number of rows due to row-by-row deletion and logging.
Locks Usually locks the entire table. Locks rows or the table (depends on DBMS and scale).
-- Removes all rows from the employees table, resets auto-increment.
TRUNCATE TABLE employees;
-- Removes all rows from the employees table.
DELETE FROM employees;
-- Removes rows from the employees table where department_id equals 10.
DELETE FROM employees WHERE department_id = 10;
What is the difference between DELETE and TRUNCATE… - sobes.tech