what is the diffrence between truncate and delete in sql server?

后端 未结 3 419
时光取名叫无心
时光取名叫无心 2021-01-18 09:28

Can anybody provide me the list of all the differences between truncate and delete in SQL server?

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 10:04

    The difference between truncate and delete is listed below:

    +----------------------------------------+----------------------------------------------+
    |                Truncate                |                    Delete                    |
    +----------------------------------------+----------------------------------------------+
    | We can't Rollback after performing     | We can Rollback after delete.                |
    | Truncate.                              |                                              |
    |                                        |                                              |
    | Example:                               | Example:                                     |
    | BEGIN TRAN                             | BEGIN TRAN                                   |
    | TRUNCATE TABLE tranTest                | DELETE FROM tranTest                         |
    | SELECT * FROM tranTest                 | SELECT * FROM tranTest                       |
    | ROLLBACK                               | ROLLBACK                                     |
    | SELECT * FROM tranTest                 | SELECT * FROM tranTest                       |
    +----------------------------------------+----------------------------------------------+
    | Truncate reset identity of table.      | Truncate reset identity of table.            |
    +----------------------------------------+----------------------------------------------+
    | It locks the entire table.             | It locks the table row.                      |
    +----------------------------------------+----------------------------------------------+
    | Its DDL(Data Definition Language)      | Its DML(Data Manipulation Language)          |
    | command.                               | command.                                     |
    +----------------------------------------+----------------------------------------------+
    | 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.                            |
    +----------------------------------------+----------------------------------------------+
    | Syntax :                               | Syntax :                                     |
    | 1) TRUNCATE TABLE table_name           | 1) DELETE FROM table_name                    |
    |                                        | 2) DELETE FROM table_name WHERE              |
    |                                        |    example_column_id IN (1,2,3)              |
    +----------------------------------------+----------------------------------------------+
    

提交回复
热议问题