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

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


当前回答

SQL server中删除与截断的总结 完整文章请点击这个链接:http://codaffection.com/sql-server-article/delete-vs-truncate-in-sql-server/

摘自dotnet mob文章:删除Vs截断SQL Server

其他回答

如果不小心使用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.

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

删除

DELETE是一个DML命令 DELETE可以回退 Delete =仅删除-因此可以回滚 在DELETE中,可以使用WHERE子句编写条件 语法- Delete from [Table] where [Condition]

截断

TRUNCATE是一个DDL命令 不能在TRUNCATE中回滚,TRUNCATE将永久删除该记录 Truncate = Delete+Commit -这样我们就不能回滚 不能在TRUNCATE中使用条件(WHERE子句) 语法- Truncate table [table]

详情请浏览

http://www.zilckh.com/what-is-the-difference-between-truncate-and-delete/

这两个操作的另一个区别是,如果表包含一个标识列,则在TRUNCATE下该列的计数器将重置1(或为该列定义的种子值)。DELETE没有这种影响。

下面是我对SQL Server中DELETE和TRUNCATE的区别的详细回答

•删除数据:首先,两者都可以用于从表中删除行。 但是,根据提供程序的功能,DELETE不仅可以用于从表中删除行,还可以用于从VIEW或OPENROWSET或OPENQUERY的结果中删除行。

•FROM子句:使用DELETE,您还可以使用另一个FROM子句根据另一个表的行从一个表/视图/rowset_function_limited中删除行。在那个FROM子句中,您还可以编写正常的JOIN条件。实际上,通过将SELECT替换为DELETE并删除列名,可以从不包含任何聚合函数的SELECT语句创建DELETE语句。 对于TRUNCATE,你不能这样做。

•WHERE: TRUNCATE不能有WHERE条件,但DELETE可以。这意味着使用TRUNCATE您不能删除特定的行或特定的行组。 TRUNCATE TABLE类似于不带WHERE子句的DELETE语句。

•性能:TRUNCATE TABLE更快,使用更少的系统和事务日志资源。 其中一个原因是两个语句都使用了锁。DELETE语句使用行锁执行,表中的每一行都被锁定以便删除。TRUNCATE TABLE总是锁定表和页,而不是每一行。

•事务日志:DELETE语句一次删除一行,并在事务日志中为每行创建单独的条目。 TRUNCATE TABLE通过释放用于存储表数据的数据页来删除数据,并且在事务日志中只记录页的释放。

•page:执行DELETE语句后,表中仍然可以包含空页。 TRUNCATE通过释放用于存储表数据的数据页来删除数据。

•Trigger: TRUNCATE不会激活表上的删除触发器。因此,在使用TRUNCATE时必须非常小心。如果在表上定义了delete Trigger以在删除行时执行一些自动清理或记录操作,则永远不应该使用TRUNCATE。

•标识列:如果表包含标识列,则使用TRUNCATE,该列的计数器将重置为为该列定义的种子值。如果没有定义种子,则使用默认值1。 DELETE不会重置标识计数器。因此,如果希望保留标识计数器,请使用DELETE。

•Replication: DELETE可以用于事务性复制或合并复制中使用的表。 而TRUNCATE不能用于事务性复制或合并复制中涉及的表。

•Rollback: DELETE语句可以回滚。 TRUNCATE也可以回滚,前提是它被包含在TRANSACTION块中并且会话没有关闭。一旦会话关闭,您将无法回滚TRUNCATE。

• Restrictions : The DELETE statement may fail if it violates a trigger or tries to remove a row referenced by data in another table with a FOREIGN KEY constraint. If the DELETE removes multiple rows, and any one of the removed rows violates a trigger or constraint, the statement is canceled, an error is returned, and no rows are removed. And if DELETE is used against View, that View must be an Updatable view. TRUNCATE cannot be used against the table used in Indexed view. TRUNCATE cannot be used against the table referenced by a FOREIGN KEY constraint, unless a table that has a foreign key that references itself.