SQL语法中drop,delete与truncate的区别
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语法中drop,delete与truncate的区别相关的知识,希望对你有一定的参考价值。
SQL语法中drop,delete与truncate的区别
- delete是DML语句,可以选择删除部分数据,也可以选择删除全部数据;删除的数据可以回滚;不会释放空间
- drop是DDL语句,删除表结构和所有数据,同时删除表结构所依赖的约束、触发器和索引;删除的数据无法回滚;会释放空间
- truncate是DDL语句,删除表的所有数据,不能删除表的部分数据,也不能删除表的结构;删除的数据无法回滚;会释放空间
- 执行速度:一般来说:drop>truncate>delete
- 一般使用场景:如果一张表确定不再使用,我们使用drop来操作;如果只是删表中的全部数据,一般使用truncate;如果删除的是表中的部分数据,一般使用delete
drop直接删掉表 truncate删除表中数据,再插入时自增长id又从1开始 delete删除表中数据,可以加where字句。
(1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。
(2) 表和索引所占空间。当表被TRUNCATE 后,这个表和索引所占用的空间会恢复到初始大小,而DELETE操作不会减少表或索引所占用的空间。drop语句将表所占用的空间全释放掉。
(3) 一般而言,drop > truncate > delete
(4) 应用范围。TRUNCATE 只能对TABLE;DELETE可以是table和view
(5) TRUNCATE 和DELETE只删除数据,而DROP则删除整个表(结构和数据)。
(6) truncate与不带where的delete :只删除数据,而不删除表的结构(定义)drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
(7) delete语句为DML(Data Manipulation Language),这个操作会被放到 rollback segment中,事务提交后才生效。如果有相应的 tigger,执行的时候将被触发。
(8) truncate、drop是DDL(Data Define Language),操作立即生效,原数据不放到 rollback segment中,不能回滚
(9) 在没有备份情况下,谨慎使用 drop 与 truncate。要删除部分数据行采用delete且注意结合where来约束影响范围。回滚段要足够大。要删除表用drop;若想保留表而将表中数据删除,如果于事务无关,用truncate即可实现。如果和事务有关,或老是想触发trigger,还是用delete。
(10) Truncate table 表名 速度快,而且效率高,因为: truncate table 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。
(11) TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。
(12) 对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。
在速度上,一般来说,drop> truncate > delete。
2、在使用drop和truncate时一定要注意,虽然可以恢复,但为了减少麻烦,还是要慎重。
3、如果想删除部分数据用delete,注意带上where子句,回滚段要足够大;
如果想删除表,当然用drop;
如果想保留表而将所有数据删除,如果和事务无关,用truncate即可;
如果和事务有关,或者想触发trigger,还是用delete;
如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据。
In this article, we look into the difference between DELETE, DROP and TRUNCATE commands in SQL. We will also see the general syntax related to each of the commands and the corresponding queries to implement these commands in SQL.
DELETE
The SQL DELETE command is used to delete existing records or rows from a table. The query can be used to delete one or multiple records from an existing table depending on the condition specified. The DELETE command is a Data Manipulation Language (DML) command. This command returns the number of records deleted after execution. With this command, we can also delete all the records of the table if we do not specify the condition in the WHERE clause.
DELETE Command Syntax to REMOVE all Rows:
1 | DELETE FROM Table_Name; |
This query deletes all records from the given table name.
DELETE Command Syntax with WHERE Clause:
1 | DELETE FROM Table_Name WHERE [Conditions]; |
Note: We do not need to list fields or column names of the table because the entire row or tuple is deleted.
Example Queries:
This query removes all records or rows where Student_Id>100 (Student_Id is a column or attribute of table Student)
1 | DELETE FROM Student WHERE Student_Id>100; |
This query deletes all rows from table Student as WHERE clause and the conditions are not specified.
1 | DELETE FROM Student; |
DROP
The SQL DROP command is used to drop the whole table. In other words, it deletes the entire records as well as the schema and structure of the table along with its named elements. The DROP command is a Data Definition Language (DDL) command. In addition, this command can also be used to drop databases. Databases or tables once dropped are wiped out of existence and cannot be recovered.
DROP Command Syntax:
1 | DROP TABLE Table_Name; |
Example Queries:
This query will drop the table Student from the database
1 | DROP TABLE Student; |
This query drops/deletes the entire Database School.
1 | DROP DATABASE School; |
TRUNCATE
The SQL TRUNCATE command is used to delete all the records/rows from the table without affecting the schema and structure of the database. The TRUNCATE Command is a Data Definition Language (DDL) command. This command cannot be used to delete a single row in a database because the TRUNCATE command cannot use the WHERE clause.
TRUNCATE Command Syntax:
1 | TRUNCATE TABLE Table_Name; |
Example Queries:
This query removes all records from table Student.
1 | TRUNCATE TABLE Student; |
Difference between DELETE, DROP and TRUNCATE
Now, we compare and contrast between key features of DELETE, DROP and TRUNCATE commands respectively.
DELETE | DROP | TRUNCATE |
1. It is a DML command. | 1. It is a DDL command. | 1. It is a DDL command. |
2. It is used to delete rows or records based on conditions specified in the WHERE clause. | 2. It is used to delete the entire table along with its schema and structure respectively. | 2. It is used to delete the entire records of a table without affecting the schema of the table. |
3. If the WHERE clause is not specified with conditions it deletes all the records of the table. | 3. There is no WHERE clause. | 3. There is no WHERE clause. |
4. It is a DML command. As a result, the operation can be rolled back. | 4. It is a DDL command. As a result, the changes cannot be rolled back or undone. | 4. It is a DDL command. As a result, the changes cannot be rolled back or undone. |
5. It scans every row before deleting making it slower and time-consuming. | 5. It is faster and time-saving. | 5. It is faster than DELETE in execution because it does not scan every row before deleting which makes it the least time-consuming. |
6. Syntax: DELETE FROM TABLE Table_Name WHERE [CONDITIONS]; | 6. Syntax: DROP TABLE Table_Name; | 6. Syntax: TRUNCATE TABLE Table_Name; |
That’s it for the article you can create a sample table with some dummy records and execute each of the commands to see their working. You can ask your queries in the comment section below.
参考:drop,delete与truncate的区别
参考:Difference between DELETE, DROP and TRUNCATE
参考:drop、truncate和delete的区别
参考:Truncate/Delete/Drop table的特点和区别
参考:drop、truncate和delete的区别?
以上是关于SQL语法中drop,delete与truncate的区别的主要内容,如果未能解决你的问题,请参考以下文章
SQL语句中的 truncate delete与drop区别