📜  SQL中DROP和TRUNCATE的区别

📅  最后修改于: 2021-09-09 11:13:20             🧑  作者: Mango

先决条件 – SQL 中的 DROP 和 TRUNCATE

1. 删除:
DROP 是一个 DDL(数据定义语言)命令,用于删除该表的表定义和索引、数据、约束、触发器等。在性能方面,DROP 命令执行起来很快,但比 TRUNCATE 慢,因为它会引起并发症。与 DELETE 不同,我们不能在使用 DROP 命令后回滚数据。在 DROP 命令中,表空间从内存中释放,因为它永久删除表及其所有内容。

DROP 命令的语法 –

DROP TABLE table_name;

2.截断:
TRUNCATE 是 DDL(数据定义语言)命令。它用于从表中删除所有元组。与 DROP 命令一样,TRUNCATE 命令也不包含 WHERE 子句。 TRUNCATE 命令比 DROP 和 DELETE 命令都快。和 DROP 命令一样,我们也不能在使用 this 命令后回滚数据。

TRUNCATE 命令的语法 –

TRUNCATE TABLE table_name; 

让我们看看 SQL 中 DROP 和 TRUNCATE 命令之间的区别:-

S.NO DROP TRUNCATE
1. The DROP command is used to remove table definition and its contents. Whereas the TRUNCATE command is used to delete all the rows from the table.
2. In the DROP command, table space is freed from memory. While the TRUNCATE command does not free the table space from memory.
3. DROP is a DDL(Data Definition Language) command. Whereas the TRUNCATE is also a DDL(Data Definition Language) command.
4. In the DROP command, view of table does not exist. While in this command, view of table exist.
5. In the DROP command, integrity constraints will be removed. While in this command, integrity constraints will not be removed.
6. In the DROP command, undo space is not used. While in this command, undo space is used but less than DELETE.
7. The DROP command is quick to perform but gives rise to complications. While this command is faster than DROP.