📜  DELETE 和 TRUNCATE 的区别

📅  最后修改于: 2021-09-10 02:21:36             🧑  作者: Mango

1. 删除:
DELETE 是一个 DML(数据操作语言)命令,当我们指定要从表或关系中删除或删除的行(元组)时使用。 DELETE 命令可以包含 WHERE 子句。如果WHERE子句与 DELETE 命令一起使用,则它只删除或删除那些满足条件的行(元组),否则默认情况下它会从表中删除所有元组(行)。

DELETE 命令的语法:

DELETE FROM TableName 
WHERE condition; 

2.截断:
TRUNCATE 是一个 DDL(数据定义语言)命令,用于删除表中的所有行或元组。与 DELETE 命令不同,TRUNCATE 命令不包含 WHERE 子句。在 TRUNCATE 命令中,记录了每个已删除数据页的事务日志。与 DELETE 命令不同,TRUNCATE 命令速度很快。像 DELETE 一样,我们甚至可以在使用 TRUNCATE 命令后回滚数据。

TRUNCATE 命令的语法:-

TRUNCATE TABLE  TableName; 

让我们看看 DELETE 和 TRUNCATE 命令之间的区别:-

S.NO Delete Truncate
1. The DELETE command is used to delete specified rows(one or more). While this command is used to delete all the rows from a table.
2. It is a DML(Data Manipulation Language) command. While it is a DDL(Data Definition Language) command.
3. There may be WHERE clause in DELETE command in order to filter the records. While there may not be WHERE clause in TRUNCATE command.
4. In the DELETE command, a tuple is locked before removing it. While in this command, data page is locked before removing the table data.
5. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.
6. DELETE command is slower than TRUNCATE command. While TRUNCATE command is faster than DELETE command.
7. To use Delete you need DELETE permission on the table. To use Truncate on a table we need at least ALTER permission on the table.
8. Identity of column retains the identity after using DELETE Statement on table. Identity of the column is reset to its seed value if the table contains an identity column.
9. The delete can be used with indexed views. Truncate cannot be used with indexed views.