What is the difference between TRUNCATE and DELETE?
- TRUNCATE: Deletes all rows in a table, but the table structure remains. It is faster
and does not log individual row deletions.
- Cannot be rolled back (in most databases).
- Resets auto-increment counters.
- DELETE: Deletes rows based on a condition, and can be rolled back (if using
transactions).
- Slower compared to TRUNCATE.
Example:
DELETE FROM Employees WHERE Age < 18; -- Deletes only specific rows
TRUNCATE TABLE Employees; -- Deletes all rows