|
| 1 | +--- |
| 2 | +title: Difference Between DELETE and TRUNCATE Commands In SQL |
| 3 | +date: 2025-08-04 14:18:00 +05:30 |
| 4 | +categories: |
| 5 | +- database |
| 6 | +tags: |
| 7 | +- sql |
| 8 | +layout: post |
| 9 | +--- |
| 10 | + |
| 11 | +# **What is a DELETE command?** |
| 12 | + |
| 13 | +- The DELETE statement is a **DML command (Data Manipulation Language).** It is used to delete single or multiple rows (records) from a table. |
| 14 | +- The **WHERE** clause in DELETE command **defines a condition** for deleting only the selected unwanted rows from the target table. |
| 15 | +- The DELETE query creates a log file in the transaction log. It stores the records before deleting them. So, in case, we delete one or many important rows, we can get them back using the **ROLLBACK** command. |
| 16 | +- Since the DELETE statement is a DML command, we have to commit the changes manually. Point to remember, the **ROLLBACK must be done before COMMIT**. |
| 17 | + |
| 18 | +### **Syntax:** |
| 19 | + |
| 20 | +> DELETE FROM table_name WHERE condition; |
| 21 | +> |
| 22 | +
|
| 23 | +# **What is a TRUNCATE command?** |
| 24 | + |
| 25 | +- The TRUNCATE command is a **DDL command** **(Data Definition Language).** We use this command on tables to delete all the records at once. |
| 26 | +- The TRUNCATE command **does not use any condition,** like a WHERE clause to define a condition. So, we should use it only when all the data in the target table is unwanted. |
| 27 | +- The TRUNCATE command uses table lock to **lock the table and page** for truncate operation, instead of row lock. So, it only logs the deallocation of the pages that store the records, and as a result, it takes much less time than the DELETE command. |
| 28 | +- Since the TRUNCATE command is a DDL command, the **commit is automatically done** here. |
| 29 | +- The TRUNCATE command **resets the identity** to its seed value. |
| 30 | + |
| 31 | +**Seed value**: seed value in SQL refers to the internal value which the Server uses to generate the next value. |
| 32 | + |
| 33 | +### **Syntax:** |
| 34 | + |
| 35 | +> TRUNCATE TABLE table_name; |
| 36 | +> |
| 37 | +
|
| 38 | +# **"TRUNCATE cannot be rolled back" - Fact or Myth?** |
| 39 | + |
| 40 | +We know the DELETE command can use rollback. But, many people have a misconception that the TRUNCATE statements cannot use rollback. It is **MYTH**! |
| 41 | + |
| 42 | +**Myth**: The truncate command works so fast that it does not have time to log its records in the transaction log. |
| 43 | + |
| 44 | +**Fact:** It **only logs the deallocation of the page of stored records**, so the work is **faster than the DELETE** command. |
| 45 | + |
| 46 | +Meanwhile, the DELETE statement logs all the rows of the table, so, the DELETE statement takes much more time than the TRUNCATE command. |
| 47 | + |
| 48 | +In conclusion, the TRUNCATE command can be rolled back for truncated records inside the transaction. |
| 49 | + |
| 50 | +> Page: It is the fundamental unit of data storage in SQL Server. |
| 51 | +> |
| 52 | +
|
| 53 | +**DELETE vs TRUNCATE** |
| 54 | + |
| 55 | +| Topic | DELETE | TRUNCATE | |
| 56 | +| --- | --- | --- | |
| 57 | +| Definition | DELETE is a SQL command that **removes one or multiple rows from a table using conditions.** | TRUNCATE is a SQL command that **removes all the rows** from a table without using any condition. | |
| 58 | +| Language | It is a **DML(Data Manipulation Language)** command. | It is a **DDL(Data Definition Language)** command. | |
| 59 | +| Commit | We have to manually COMMIT the changes in the DELETE command. | The changes are automatically COMMIT in the TRUNCATE command. | |
| 60 | +| Process | It deletes rows **one by one with conditions.** | It deletes **all the data at once**. | |
| 61 | +| Condition | It uses the **WHERE clause** as a condition. | It does not take any condition i.e. **does not use WHERE clause.** | |
| 62 | +| Lock | It **locks all the rows** for deletion. | It uses table lock to **lock the pages** for deletion. | |
| 63 | +| Log | It **logs every single record in the transaction log**. | It **only logs the deallocation of the pages where the data is stored.** | |
| 64 | +| Transaction space | It uses **more transaction space** compared to the TRUNCATION command. | It uses **less transaction space** compared to the DELETE command. | |
| 65 | +| Identity | It does **not reset the table identity** to its seed value if there is an identity column. | It **resets** the table identity to its seed value. | |
| 66 | +| Permission | It needs **delete** permission. | It needs **alter** table permission. | |
| 67 | +| Speed | It is much **slower** when comes to big databases. | It is faster or in other words, **instant**. | |
0 commit comments