Truncate Function In Sql in SQL
The TRUNCATE statement in SQL is used to quickly delete all rows from a table, removing data without logging individual row deletions. It’s faster than using a DELETE without a WHERE clause but does not log each deleted row.
? Key Points about TRUNCATE
Removes all rows from a table.
Resets identity counters (like auto-increment columns) in many databases.
Cannot be used with a
WHEREclause (all rows are removed).Faster and uses fewer system resources than
DELETE.Often cannot be rolled back depending on the database and transaction settings.
Does not fire triggers in many systems.
Syntax
TRUNCATE TABLE table_name;Example
TRUNCATE TABLE Employees;This removes all data from the Employees table quickly.
Differences between TRUNCATE and DELETE
| Feature | TRUNCATE | DELETE |
|---|---|---|
| Deletes rows | All rows, no filtering | Rows can be filtered with WHERE |
| Logging | Minimal logging (usually) | Logs each deleted row |
| Triggers | Usually does not fire | Triggers fire |
| Transaction rollback | Depends on DB (often can't) | Can be rolled back |
| Identity reset | Resets identity counters | Does not reset identity |
| Speed | Faster for large tables | Slower for large tables |
If you want, I can help you with examples or details for specific SQL databases!