-2

I am doing some practice with AdventureWorks. I am currently experimenting with DMLs.

However, could someone tell me the major difference between Truncate and Delete?

1

2 Answers 2

2
  • Truncate is used to remove all the values in a table (there are some limitations with this like for ex. the table cannot have foreign keys)

  • Delete is to remove values from a table under any condition.

Usually if you want to clean a table Truncate is faster.

Hope this helps

2

Here's my complete list. Some are probably Oracle-specific but others will be generally applicable. Some are obvious, but still worth stating I think.

Statement type: Delete is DML, Truncate is DDLCommit: Delete has no autocommit, a truncate is autocommited (actually, there are two commits involved I believe)Space reclamation: Delete does not recover space, Truncate recovers space (unless you use the REUSE STORAGE clause)Row scope: Delete can remove only some rows. Truncate removes all rows except where used in a partitioning context.Object types: Delete can be applied to tables and tables inside a cluser. Truncate applies only to tables or the entire clusterData Object ID's: Delete does not affect the data object id, but truncate assigns a new data object id unless there has never been an insert against the table (even a single insert that is rolled back will cause a new data object id to be assigned).Rollback: In some implementations (eg. Oracle) truncate cannot be rolled back.Flashback: Flashback works across deletes, but a truncate prevents flashback operations to before the operation.Grants: Delete can be granted on a table to another user or role, but truncate cannot be without using a DROP ANY TABLE grant.Redo/Undo: Delete generates a small amount of redo and a large amount of undo. Truncate generates a negligible amount of each.Indexes: A truncate operation renders unusable indexes usable again. Delete does not.Foreign Keys: A truncate cannot be applied when an enabled foreign key references the table. Treatment with delete depends on the configuration of the foreign keysLocking: Truncate requires an exclusive table lock, delete requires a shared table lock.Triggers: DML triggers do not fire on a truncate. (thanks Polara)

Um ... let me think ... I'll add more if i think of them. Let me know if I missed any and I'll add them and credit you.

Same question

1
  • <<Delete can remove only some rows>> I'm not agree with this, delete also can remove all rows
    – Vasily
    Apr 24, 2015 at 4:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.