SQL中TRUNCATE和DELETE的区别是什么?

如果你的答案是针对特定平台的,请注明。


当前回答

如果不小心使用Delete/Truncate从表中删除了所有数据。您可以回滚已提交的事务。恢复上次备份并运行事务日志,直到将要执行删除/截断操作。

以下相关信息来自一篇博客文章:

While working on database, we are using Delete and Truncate without knowing the differences between them. In this article we will discuss the difference between Delete and Truncate in Sql. Delete: Delete is a DML command. Delete statement is executed using a row lock,each row in the table is locked for deletion. We can specify filters in where clause. It deletes specified data if where condition exists. Delete activities a trigger because the operation are logged individually. Slower than Truncate because it Keeps logs Truncate Truncate is a DDL command. Truncate table always lock the table and page but not each row.As it removes all the data. Cannot use Where condition. It Removes all the data. Truncate table cannot activate a trigger because the operation does not log individual row deletions. Faster in performance wise, because it doesn't keep any logs. Note: Delete and Truncate both can be rolled back when used with Transaction. If Transaction is done, means committed then we can not rollback Truncate command, but we can still rollback Delete command from Log files, as delete write records them in Log file in case it is needed to rollback in future from log files. If you have a Foreign key constraint referring to the table you are trying to truncate, this won't work even if the referring table has no data in it. This is because the foreign key checking is done with DDL rather than DML. This can be got around by temporarily disabling the foreign key constraint(s) to the table. Delete table is a logged operation. So the deletion of each row gets logged in the transaction log, which makes it slow. Truncate table also deletes all the rows in a table, but it won't log the deletion of each row instead it logs the deallocation of the data pages of the table, which makes it faster. ~ If accidentally you removed all the data from table using Delete/Truncate. You can rollback committed transaction. Restore the last backup and run transaction log till the time when Delete/Truncate is about to happen.

其他回答

对原始答案的一个小修正——删除也会产生大量的重做(因为undo本身是由重做保护的)。这可以从autotrace输出中看到:

SQL> delete from t1;

10918 rows deleted.

Elapsed: 00:00:00.58

Execution Plan
----------------------------------------------------------
   0      DELETE STATEMENT Optimizer=FIRST_ROWS (Cost=43 Card=1)
   1    0   DELETE OF 'T1'
   2    1     TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=43 Card=1)




Statistics
----------------------------------------------------------
         30  recursive calls
      12118  db block gets
        213  consistent gets
        142  physical reads
    3975328  redo size
        441  bytes sent via SQL*Net to client
        537  bytes received via SQL*Net from client
          4  SQL*Net roundtrips to/from client
          2  sorts (memory)
          0  sorts (disk)
      10918  rows processed

下面是这些sql命令之间一些重要区别的总结:

SQL truncate命令:

1)它是一个DDL(数据定义语言)命令,因此诸如COMMIT和ROLLBACK之类的命令不适用于此命令(这里的例外是PostgreSQL和MSSQL,它们的TRUNCATE命令的实现允许该命令在事务中使用)

2)你不能撤销删除记录的操作,它是自动发生的,并且是不可逆的(除了上面的例外情况-但是,如果该操作包含在TRANSACTION块中并且会话没有关闭)。Oracle -包含两个隐式提交,一个在语句执行之前,一个在语句执行之后。因此,该命令不能被撤回,而运行时错误将导致提交

3)删除表中的所有记录,记录不能被限制删除。对于Oracle,当表被划分为每个分区时,单个分区可以被隔离截断(TRUNCATE),从而可以从表中部分删除所有数据

4)释放表中数据所占用的空间(在磁盘的表空间中)。对于Oracle -如果你使用REUSE STORAGE子句,数据段将不会回滚,也就是说,你将保留已删除行的空间分配给表,如果表要用数据重新加载,这可能会更有效一点。高分将被重置

5) TRUNCATE比DELETE工作得快得多

6)在TRUNCATE的情况下,Oracle闪回防止回到术前状态

7) Oracle - TRUNCATE不能授予(GRANT)不使用DROP ANY表

8) TRUNCATE操作使不可用的索引重新可用

9)当启用的外键指向另一个表时,TRUNCATE不能使用,那么你可以:

执行命令:DROP CONSTRAINT,然后TRUNCATE,然后通过CREATE CONSTRAINT或播放它 SET FOREIGN_KEY_CHECKS = 0;然后截断,然后:SET_FOREIGN_KEY_CHECKS = 1;

10) TRUNCATE需要一个排他表锁,因此,关闭排他表锁是防止在表上进行TRUNCATE操作的一种方法

11) DML触发器在执行TRUNCATE后不会触发(所以在这种情况下要非常小心,如果在表中定义了删除触发器来执行自动的表清理或行删除后的登录操作,则不应该使用TRUNCATE)。在Oracle上,DDL触发器被触发

12) Oracle - TRUNCATE不能用于:database link的情况 13) TRUNCATE不返回删除的记录数量

14)事务日志-一个指示页面释放的日志(删除数据,释放用于存储表数据的数据页的分配,并只将页面释放写入事务日志)-执行速度比DELETE更快。TRUNCATE只需要调整数据库中指向表(High Water Mark)的指针,数据立即被删除,因此它使用更少的系统资源和事务日志

15)性能(获得锁)-表和页锁-在执行过程中不会降低性能

16) TRUNCATE不能用于涉及事务复制或合并复制的表

SQL删除命令:

1)该命令为DML (Data Manipulation Language)命令,因此使用了COMMIT和ROLLBACK命令

2)可以使用ROLLBACK命令撤销删除记录的操作

3)从表中删除全部或部分记录,您可以使用WHERE子句限制要删除的记录

4)不释放表中数据占用的空间(在表空间中-在磁盘上)

5) DELETE比TRUNCATE慢得多

6) Oracle Flashback适用于DELETE

7) Oracle -对于DELETE,您可以使用GRANT命令

8) DELETE操作不会使不可用的索引重新可用

9)如果外键enabled是指另一个表,可以(或不)应用,取决于外键配置(如果没有),请:

执行命令:DROP CONSTRAINT,然后TRUNCATE,然后通过CREATE CONSTRAINT或播放它 SET FOREIGN_KEY_CHECKS = 0;然后截断,然后:SET_FOREIGN_KEY_CHECKS = 1;

10) DELETE需要一个共享表锁

11)引发火灾

12) DELETE可用于:database link的情况

13) DELETE返回已删除记录的数量

14)事务日志-对于每个删除的记录(每次删除一行,并为每个删除的行在事务日志中记录一个条目)-执行速度比TRUNCATE慢。执行DELETE语句后,表可能仍然包含空白页。DELETE需要读取记录、检查约束、更新块、更新索引以及生成重做/撤销。所有这些都需要时间,因此它所花费的时间比TRUNCATE要长得多

15)性能(获得锁)-记录锁-在执行过程中降低性能-表中的每条记录都被锁定以便删除

16)可以在事务性复制或合并复制的表上使用DELETE

最大的区别是truncate是不记录日志的操作,而delete是。

简单地说,这意味着在数据库崩溃的情况下,不能通过截断恢复所操作的数据,但可以通过删除恢复。

详情请点击这里

DELETE语句可以有一个WHERE子句来删除特定的记录,而TRUNCATE语句不需要任何子句并擦除整个表。 重要的是,DELETE语句记录删除日期,而TRUNCATE语句不记录删除日期。

我想对matthieu的帖子发表评论,但我还没有得到代表…

在MySQL中,自动递增计数器通过truncate重置,而不是通过delete重置。