What is the TRUNCATE Statement in SQL ?
Answer : The SQL TRUNCATE statement is used to remove all records from a table. It performs the same function as a DELETE statement without a WHERE Clause. if you want to truncate a table, the TRUNCATE TABLE statement cannot be rolled back.
Syntax
TRUNCATE TABLE table_name;
Suppose we have an Employees table:
| Employee_ID | Employee_Name | Salary |
|---|---|---|
| 101 | Anjali | 50000 |
| 102 | Priya | 45000 |
| 103 | Amit | 55000 |
To remove all records:
TRUNCATE TABLE Employees;
After TRUNCATE
| Employee_ID | Employee_Name | Salary |
|---|
The Employees table still exists, but all its records have been removed.
DELETE vs TRUNCATE
| DELETE | TRUNCATE |
|---|---|
Can delete specific rows using WHERE | Removes all rows |
WHERE can be used | WHERE cannot be used |
| Generally logs row-level deletions | Generally performs a more bulk-oriented removal |
| Table structure remains | Table structure remains |