patternModerate
Why is truncate DDL?
Viewed 0 times
truncatewhyddl
Problem
I have an interview question, which was asked during my interview. I answered the question, but interviewer was not so convinced with my answer. So, anyone please correct me with my understanding ?
Q. Why Truncate is DDL Where as Delete is DML ? Both do almost same job (removing rows)
Ans. When we are using Truncate, we are de-allocating the whole space allocated by the data without saving into the undo-table-space. But, in case of Delete, we are putting all the data into undo table-space and then we are deleting all the data.
Please, if anyone knows best answer for the above, please explain.
Q. Why Truncate is DDL Where as Delete is DML ? Both do almost same job (removing rows)
Ans. When we are using Truncate, we are de-allocating the whole space allocated by the data without saving into the undo-table-space. But, in case of Delete, we are putting all the data into undo table-space and then we are deleting all the data.
Please, if anyone knows best answer for the above, please explain.
Solution
The DML versus DDL distinction isn't as clear as their names imply, so things get a bit muddy sometimes.
Oracle clearly classifies
The main points that put
The fact that
That same documentation notes that
So I'd sum it up as:
Oracle clearly classifies
TRUNCATE as DDL in the Concepts Guide, but DELETE as DML. The main points that put
TRUNCATE in the DDL camp on Oracle, as I understand it, are:TRUNCATEcan change storage parameters (theNEXTparameter), and those are part of the object definition - that's in the DDL camp.
TRUNCATEdoes an implicitcommit, and cannot be rolled back (flashback aside) - most (all?) DDL operations in Oracle do this, no DML does.
The fact that
TRUNCATE doesn't run ON DELETE triggers also sets it apart from normal DML operations (but some direct path DML operations also skip triggers, so that's not a clear indicator).That same documentation notes that
DELETE generates UNDO, but TRUNCATE doesn't, so your statement is correct in this respects. (Note that TRUNCATE does generate some REDO so that the truncation can be replayed in case of restore/recovery.) But some NOLOGGING operations can also produce reduced UNDO (not sure about none at all), so that's not a clear indicator either in my opinion.So I'd sum it up as:
truncateis not "transactional" in the sense that it commits and can't be rolled back, and can modify object storage attributes. So it's not ordinary DML - Oracle classifies it as DDL.
deleteis an ordinary DML statement.
Context
StackExchange Database Administrators Q#36607, answer score: 17
Revisions (0)
No revisions yet.