SQL中TRUNCATE和DELETE的区别是什么?
如果你的答案是针对特定平台的,请注明。
SQL中TRUNCATE和DELETE的区别是什么?
如果你的答案是针对特定平台的,请注明。
当前回答
并不是说截断不记录SQL Server中的任何内容。truncate不记录任何信息,但它记录您触发truncate的表的数据页的释放。
如果在开始时定义事务,则可以回滚被截断的记录,回滚后可以恢复被截断的记录。但不能在提交截断事务后从事务日志备份中恢复截断记录。
其他回答
DELETE The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. TRUNCATE TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE. DROP The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
来自:http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands
DROP The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. TRUNCATE TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as a DELETE. Table level lock will be added when Truncating. DELETE The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. Row level lock will be added when deleting.
来自:http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands
TRUNCATE是DDL语句,而DELETE是DML语句。以下是两者的区别:
As TRUNCATE is a DDL (Data definition language) statement it does not require a commit to make the changes permanent. And this is the reason why rows deleted by truncate could not be rollbacked. On the other hand DELETE is a DML (Data manipulation language) statement hence requires explicit commit to make its effect permanent. TRUNCATE always removes all the rows from a table, leaving the table empty and the table structure intact whereas DELETE may remove conditionally if the where clause is used. The rows deleted by TRUNCATE TABLE statement cannot be restored and you can not specify the where clause in the TRUNCATE statement. TRUNCATE statements does not fire triggers as opposed of on delete trigger on DELETE statement
这里有一个非常好的与主题相关的链接。
下面是这些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
对于SQL Server或MySQL,如果有一个带有自动递增的PK, truncate将重置计数器。