Friday, June 1, 2012

Difference between Delete,Truncate and Drop

Delete
  1. It  is used to delete rows from the table.
  2. Where clause can be used.
  3. Can be rollbacked.
Truncate
  1. Used to delete all rows  from table.
  2. NO where clause can be used.
  3. cannot be rolled back.
Drop
  1. The DROP command removes a table from the database.
  2. Cannot be rolled back.
Note:-
As described above truncate command cannot be rolled back.But there is an exception to that "Truncate can be rolled back when used in a transaction within that particular session but when session is closed there is no guarantee that truncated rows are rolled back".By default all statements are commited .In case of delete all the rows that we delete are stored in log file so they can be recovered.When we use transaction commit operation is performed only after end of transaction.


BEGIN TRAN
TRUNCATE TABLE 
Employee

//table is truncated
SELECT *FROM Employee
//no records will be found
ROLLBACK
SELECT 
*FROM Employee

//original data will be shown

No comments :

Post a Comment