之间的主要区别是什么?
truncate table mytable
和
delete from mytable
答案 0 :(得分:1)
使用表锁执行TRUNCATE,并锁定整个表以删除所有记录。 使用行锁执行DELETE,表中的每一行都被锁定以进行删除。 TRUNCATE从表中删除所有行意味着我们不能使用where条件进行截断 DELETE命令用于根据WHERE条件从表中删除行。
答案 1 :(得分:1)
尽管这个问题已经回答了,但还是想将TRUNCATE&DELETE与一些基本要点进行比较。
+----------------------------------------+----------------------------------------------+
| Truncate | Delete |
+----------------------------------------+----------------------------------------------+
| Its DDL(Data Definition Language) | Its DML(Data Manipulation Language) |
| command. | command. |
+----------------------------------------+----------------------------------------------+
| Generally used to remove all the | Generally used to delete particular rows of |
| data from the TABLE. | data from the TABLE based on WHERE condition |
+----------------------------------------+----------------------------------------------+
| We can't Rollback after performing | We can Rollback after delete. |
| Truncate. | |
+----------------------------------------+----------------------------------------------+
| Truncate reset identity of table. | Delete does not reset identity of table. |
+----------------------------------------+----------------------------------------------+
| It locks the entire table. | It locks the table row. |
+----------------------------------------+----------------------------------------------+
| We can't use WHERE clause with it. | We can use WHERE to filter data to delete. |
+----------------------------------------+----------------------------------------------+
| Trigger is not fired while truncate. | Trigger is fired. |
+----------------------------------------+----------------------------------------------+
| Faster in performance wise, because it |Slower than Truncate because it Keeps logs |
| doesn't keep any logs. | |
+----------------------------------------+----------------------------------------------+
| Syntax : | Syntax : |
| 1) TRUNCATE TABLE Table_Name | 1) DELETE FROM Table_Name |
| | 2) DELETE FROM Table_Name WHERE <Condition> |
+----------------------------------------+----------------------------------------------+