【问题标题】:What is the difference between truncate and delete?截断和删除有什么区别?
【发布时间】:2014-10-21 08:00:56
【问题描述】:

两者的主要区别是什么

truncate table mytable

delete from mytable

【问题讨论】:

标签: sql sql-delete truncate


【解决方案1】:

TRUNCATE 使用表锁执行,整个表被锁定以删除所有记录。 DELETE 使用行锁执行,表中的每一行都被锁定以进行删除。 TRUNCATE 从表中删除所有行意味着我们不能使用 where 条件进行截断 DELETE 命令用于根据 WHERE 条件从表中删除行。

【讨论】:

    【解决方案2】:

    虽然已经回答了这个问题,但考虑将 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>  |
    +----------------------------------------+----------------------------------------------+
    

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 2012-06-09
      相关资源
      最近更新 更多