You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-08-04-difference-between-delete-and-truncate-commands-in-sql.markdown
+23-12Lines changed: 23 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,32 +8,44 @@ tags:
8
8
layout: post
9
9
---
10
10
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
+
11
18
# **What is a DELETE command?**
12
19
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**.
17
27
18
28
### **Syntax:**
19
29
20
30
> DELETE FROM table_name WHERE condition;
21
-
>
22
31
23
32
# **What is a TRUNCATE command?**
24
33
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.
30
43
31
44
**Seed value**: seed value in SQL refers to the internal value which the Server uses to generate the next value.
32
45
33
46
### **Syntax:**
34
47
35
48
> TRUNCATE TABLE table_name;
36
-
>
37
49
38
50
# **"TRUNCATE cannot be rolled back" - Fact or Myth?**
39
51
@@ -48,7 +60,6 @@ Meanwhile, the DELETE statement logs all the rows of the table, so, the DELETE s
48
60
In conclusion, the TRUNCATE command can be rolled back for truncated records inside the transaction.
49
61
50
62
> Page: It is the fundamental unit of data storage in SQL Server.
0 commit comments