Why is truncate a DDL statement?

我只是一个虾纸丫 提交于 2019-12-10 03:05:52

问题


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 ?


回答1:


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:

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

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

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.




回答2:


TRUNCATE resets 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.




回答3:


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.

Like Create a Table Names and Insert Some Initial records.

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

Now Truncate Table and Reinsert the records.



来源:https://stackoverflow.com/questions/26059855/why-is-truncate-a-ddl-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!