Skip to content

Commit 27011fa

Browse files
code-atomSiteleaf
authored andcommitted
Updated Difference Between Delete And Truncate Commands In Sql
1 parent 716ab5d commit 27011fa

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

_posts/2025-08-04-difference-between-delete-and-truncate-commands-in-sql.markdown

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,44 @@ tags:
88
layout: post
99
---
1010

11+
**The SQL query for delete operation includes TRUNCATE and DELETE commands.**
12+
13+
They perform **deletion operations on unwanted records or rows of a table**. In the **DELETE statement, the rows from the table get deleted with a condition**. In simple words, **one or more rows can be deleted at once**. but in the **TRUNCATE command, all the rows get deleted at once**.
14+
15+
When we use the DELETE query, a **log is created for every row in the transaction log**. This way we can get back the records using **ROLLBACK before COMMIT**. Also, when using TRUNCATE, we can get the records back using ROLLBACK. The difference is that **TRUNCATE only logs the deallocation of the page where data is stored.**
16+
17+
1118
# **What is a DELETE command?**
1219

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**.
20+
* The DELETE statement is a **DML command (Data Manipulation Language).** It is used to delete single or multiple rows (records) from a table.
21+
22+
* The **WHERE** clause in DELETE command **defines a condition** for deleting only the selected unwanted rows from the target table.
23+
24+
* 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.
25+
26+
* 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**.
1727

1828
### **Syntax:**
1929

2030
> DELETE FROM table_name WHERE condition;
21-
>
2231
2332
# **What is a TRUNCATE command?**
2433

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.
34+
* The TRUNCATE command is a **DDL command** **(Data Definition Language).** We use this command on tables to delete all the records at once.
35+
36+
* 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.
37+
38+
* 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.
39+
40+
* Since the TRUNCATE command is a DDL command, the **commit is automatically done** here.
41+
42+
* The TRUNCATE command **resets the identity** to its seed value.
3043

3144
**Seed value**: seed value in SQL refers to the internal value which the Server uses to generate the next value.
3245

3346
### **Syntax:**
3447

3548
> TRUNCATE TABLE table_name;
36-
>
3749
3850
# **"TRUNCATE cannot be rolled back" - Fact or Myth?**
3951

@@ -48,7 +60,6 @@ Meanwhile, the DELETE statement logs all the rows of the table, so, the DELETE s
4860
In conclusion, the TRUNCATE command can be rolled back for truncated records inside the transaction.
4961

5062
> Page: It is the fundamental unit of data storage in SQL Server.
51-
>
5263
5364
**DELETE vs TRUNCATE**
5465

0 commit comments

Comments
 (0)