Geeks Talk

Prepare for your Next Interview




difference between delete and truncate

This is a discussion on difference between delete and truncate within the MY SQL forums, part of the Databases category; Hi can any one tell me the difference between delete and truncate statement in mysql . which is better to use?...


Go Back   Geeks Talk > Databases > MY SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-23-2007
Expert Member
 
Join Date: Apr 2006
Location: India, Hyderabad
Posts: 129
Thanks: 19
Thanked 40 Times in 17 Posts
bvani is on a distinguished road
difference between delete and truncate

Hi

can any one tell me the difference between delete and truncate statement in mysql . which is better to use?
Reply With Quote
The Following User Says Thank You to bvani For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 03-23-2007
Junior Member
 
Join Date: Mar 2007
Location: bangalore
Posts: 1
Thanks: 0
Thanked 2 Times in 1 Post
veera_kk is on a distinguished road
Re: difference between delete and truncate

Hi,
This is Veera Reddy
The difference between delete and truncate is.
if u use delete, it will delete row by row and after deletion there is a possibility of rollback your data where as if you use truncate it will delete the entire table i.e., with structure and after deletion it will recreate the structure and there is no possibility of rollback of data once you apply truncate.
Reply With Quote
The Following 2 Users Say Thank You to veera_kk For This Useful Post:
  #3 (permalink)  
Old 05-25-2007
Expert Member
 
Join Date: Jun 2006
Location: India
Posts: 411
Thanks: 15
Thanked 33 Times in 25 Posts
jamesravid is on a distinguished road
Re: difference between delete and truncate

Just wanted to add one more point.

Truncate is a DDL command and Delete is a Dml command.

DDL statements perform commit implicitly. That is why your changed are committed automatically when ever you perform a DDL statement ( Create,Alter etc).

DML statements does not commit your changes. You have to commit explicitly inorder to save your changes. Examples of DML commands are insert,update, delete etc.
__________________
Cheers,
:) James:)
Reply With Quote
  #4 (permalink)  
Old 05-25-2007
Expert Member
 
Join Date: Nov 2006
Location: Hyd-IND
Posts: 509
Thanks: 1
Thanked 53 Times in 45 Posts
sutnarcha is on a distinguished road
Re: difference between delete and truncate

Good one from veera_KK and jamesravid
Reply With Quote
  #5 (permalink)  
Old 06-12-2007
Contributing Member
 
Join Date: Jun 2007
Location: Bangalore
Posts: 30
Thanks: 2
Thanked 20 Times in 5 Posts
vcyogi is on a distinguished road
Re: difference between delete and truncate

Add one more point also

When Delete the Particular row the Corresponding Delete Trigger(if exists) Fire.
In Case of Truncate the Trigger is not fired.
Reply With Quote
The Following User Says Thank You to vcyogi For This Useful Post:
  #6 (permalink)  
Old 06-19-2007
I am the DON
 
Join Date: Apr 2006
Location: Just on Earth with all....
Posts: 102
Thanks: 1
Thanked 31 Times in 24 Posts
sanghala is on a distinguished road
Lightbulb Re: difference between delete and truncate

Simply to say -

DML statements (Delete) can be rollbacked where DDL (Truncate) are autocommit.
__________________
Sanghala
www.dwforum.net - A Great resource for Data Warehousing Professionals

If you are satisfied with my reply. Please click “Thanks” button which increases the reputation and can serve you better.
Reply With Quote
The Following 2 Users Say Thank You to sanghala For This Useful Post:
  #7 (permalink)  
Old 06-19-2007
Contributing Member
 
Join Date: May 2007
Location: bangalore
Posts: 65
Thanks: 3
Thanked 8 Times in 6 Posts
hari.nattuva is on a distinguished road
Re: difference between delete and truncate

'truncate' requires no condition bcoz it deletes the total table once where as 'delete' requires condition bcoz it deletes the data row by row.....
Reply With Quote
  #8 (permalink)  
Old 07-04-2007
Junior Member
 
Join Date: Jul 2007
Location: INDIA
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
naga_mcat is on a distinguished road
Arrow Re: difference between delete and truncate

Truncate is a DDL command and Delete is a DML command. Hence Truncate operation can not be rolled back, where as delete operation can be rolled back. where clause can be used with delete command and not with the truncate command.
Reply With Quote
  #9 (permalink)  
Old 10-25-2007
Moderator
 
Join Date: Sep 2007
Location: Chennai, INDIA
Posts: 406
Thanks: 2
Thanked 148 Times in 81 Posts
sridharrganesan will become famous soon enoughsridharrganesan will become famous soon enough
Thumbs up Re: difference between delete and truncate

Both delete and truncate will delete the rows in a table. But there are some differences between them

1. Delete will delete rows on the table based on the condition, either may be a single row or many rows
eg., Delect * from tablename where condition;
Truncate will delete all the rows in the table.

2. Truncate is a DDL command but Delete is a DML command.

3. Rollback is possible after the delete command, but since truncate is a ddl command rollback is not possible.
Reply With Quote
  #10 (permalink)  
Old 10-25-2007
Junior Member
 
Join Date: Oct 2007
Location: hyderabad
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
jyoti akella is on a distinguished road
Re: difference between delete and truncate

hi
i want to add one more point
truncate is faster then the delete and doesn't use as much undo space as delete
Reply With Quote
  #11 (permalink)  
Old 10-26-2007
Junior Member
 
Join Date: Oct 2007
Location: chennai
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mailtokarthik24 is on a distinguished road
Re: difference between delete and truncate

if u delete a record from a table the deleted record will be stored in log file.u can retrieve it later.But its not in the case of truncate.it will delete frm the log file also
Reply With Quote
  #12 (permalink)  
Old 10-27-2007
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,365
Thanks: 7
Thanked 123 Times in 111 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: difference between delete and truncate

Since Delete is DML it deletes only the data.
Truncate is DDL it changes the table structure it self, which is not allowed while another user is connected to the table.
Reply With Quote
  #13 (permalink)  
Old 10-27-2007
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 427
Thanks: 17
Thanked 53 Times in 53 Posts
susarlasireesha is on a distinguished road
Re: difference between delete and truncate

Delete
At the simplest level, delete scans the table and removes any rows that match the given criteria in the (optional) where clause. It generates rollback information so that the deletions can be undone should it be necessary. Index entries for the deleted rows are removed from the indexes. You must commit to make the deletions permanent.

When deleting rows from a table, extents are not deallocated, so if there were 50 extents in the table before the deletion, there will still be 50 after the deletion. In addition the High Water Mark is not moved down, so it remains where it was before the deletion began. This means that any subsequent full table scans may (still) take a long time to complete - because a full table scan always scans up to the HWM. So, by example, if a select count(*) from very_large_table; took 15 minutes to complete before all the rows were deleted, you will find that it still takes about 15 mins after the deletion - because Oracle is still scanning every single block up to the HWM - even though some (or all) of the blocks may have no data in them.

Truncate
Truncate, on the other hand, simply moves the high water mark on the table right down to the beginning. It does this very quickly, and does not need to be committed. Once you truncate a table, there is no going back. Indexes are also truncated. There is no facility to be able to specify which rows to 'delete' as you can with the where clause on the delete command.

When a table is truncated, all its extents are deallocated leaving only the extents specified when the table was originally created. So if the table was originally created with minextents 3, there will be 3 extents remaining when the tables is truncated.

If you specify the reuse storage clause, then the extents are not deallocated. This saves time in the recursive SQL department if you intend to reload the table with data from an export for example, and can reduce the time it takes to do the import as there is no need to dynamically allocate any new extents.
Reply With Quote
The Following User Says Thank You to susarlasireesha For This Useful Post:
  #14 (permalink)  
Old 12-11-2007
Junior Member
 
Join Date: Aug 2006
Location: banglore(India)
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
debrajdc1981 is on a distinguished road
Re: difference between delete and truncate

Quote:
Originally Posted by bvani View Post
Hi

can any one tell me the difference between delete and truncate statement in mysql . which is better to use?
with delete operation one can rollback the data; this shows after performing delete operation , data is not completely deleted;

but in case of trancate , one cannot retrive the data using rollback operation....................
Reply With Quote
  #15 (permalink)  
Old 12-11-2007
Junior Member
 
Join Date: Aug 2006
Location: banglore(India)
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
debrajdc1981 is on a distinguished road
Re: difference between delete and truncate

Quote:
Originally Posted by bvani View Post
Hi

can any one tell me the difference between delete and truncate statement in mysql . which is better to use?
trancate delets the data permanently; ie one cannot perform rollback operation on the trancated data.........

in case of delete one can retrive the data the deleted data using rollback operation.......................
Reply With Quote
  #16 (permalink)  
Old 12-11-2007
Junior Member
 
Join Date: Dec 2007
Location: hyderabad
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
chskumar is on a distinguished road
Re: difference between delete and truncate

truncate does not take any data to the rollback segment or undo tablespace.
truncate is fast.
truncate delete's the data permanently.

delete will copy the data to the undotablespace for rollback.
delete is slow.
Reply With Quote
  #17 (permalink)  
Old 12-11-2007
Junior Member
 
Join Date: Feb 2007
Location: HYDERABAD
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kalpanachaitu is on a distinguished road
Re: difference between delete and truncate

hi,
delete removes data in the table

but truncate deletes the structure of the table.
Reply With Quote
  #18 (permalink)  
Old 12-12-2007
Contributing Member
 
Join Date: Sep 2007
Location: hyderabad
Posts: 36
Thanks: 5
Thanked 2 Times in 2 Posts
ashalalaxmi2002 is on a distinguished road
Re: difference between delete and truncate

delete means it will delete all rows.and space of deleted rows will not be released.
but when truncate the table all the rows will be deleted & space of rows also deleted.
when deleting we can roll back but in trucate roll back is not possible.