oracle 为什么截断 DDL 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26059855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:32:09  来源:igfitidea点击:

Why is truncate a DDL statement?

sqloracleddl

提问by Kan

The table structure remains same after truncate statement and only records get deleted (with auto commit). Then what is the reason that truncate is a DDL statement ?

truncate 语句后表结构保持不变,只有记录被删除(自动提交)。那么 truncate 是 DDL 语句的原因是什么?

采纳答案by Mussa

I'd add here some explanation: By default, when truncating a table , you are not just removing rows, you are implicitly telling Oracle Database to also perform the following tasks:

我在这里添加一些解释:默认情况下,截断表时,您不仅仅是删除行,您还隐含地告诉 Oracle 数据库还执行以下任务:

Deallocates all space used by the removed rows except that specified by the MINEXTENTS storage parameter

释放已删除行使用的所有空间,但由 MINEXTENTS 存储参数指定的空间除外

Sets the NEXT storage parameter to the size of the last extent removed from the segment by the truncation process

将 NEXT 存储参数设置为截断过程从段中删除的最后一个区的大小

So, it changes the storage definitions and changing the structure of the table by resetting the water high mark.

因此,它通过重置水位高标来更改存储定义并更改表的结构。

Also it can't be a DML ,right? Where clause can't be specified or comes along with DDL statement. If truncate is a DML statement then Oracle would have allowed us to use truncate along with where clause.
Note: A WHERE clause in SQL specifies that a SQL Data Manipulation Language (DML) statement should only affect rows that meet specified criteria.

它也不能是 DML,对吧?不能指定 where 子句或随 DDL 语句一起出现。如果 truncate 是 DML 语句,那么 Oracle 将允许我们将 truncate 与 where 子句一起使用。
注意:SQL 中的 WHERE 子句指定 SQL 数据操作语言 (DML) 语句应仅影响满足指定条件的行。

回答by Mureinik

TRUNCATEresets the high water mark of the table, effectively eliminating all the previously existing rows. Treating it as a DDL statement allows it to be super-fast, as it allows it to function without retaining undo (rollback) information like DML statements.

TRUNCATE重置表的高水位标记,有效地消除所有先前存在的行。将其视为 DDL 语句可以使其速度超快,因为它可以在不保留撤消(回滚)信息(如 DML 语句)的情况下运行。

回答by kmwtnarendra

A simple practical explaination can be :

一个简单实用的解释可以是:

Truncate reinitializes the identity by making changes in data definition therefore it is DDL, whereas Delete only delete the records from the table and doesn't make any changes in its Definition that's why it is DML.

Truncate 通过在数据定义中进行更改来重新初始化身份,因此它是DDL,而 Delete 仅从表中删除记录,而不对其定义进行任何更改,这就是为什么它是DML

Like Create a Table Namesand Insert Some Initial records.

比如创建一个表并插入一些初始记录。

Created table Inserted Some Records

创建的表插入了一些记录

Delete all records(Without where Clause) and Insert records again. Identity is not reinitialized.

删除所有记录(没有 where 子句)并再次插入记录。身份不会重新初始化。

Delete All Records and insert again

删除所有记录并再次插入

Now Truncate Table and Reinsert the records.

现在截断表并重新插入记录。

Truncate table and reinsert records

截断表并重新插入记录