Skip to main content

Difference between Delete and Truncate in sql server ?


DELETE
The DELETE command is used to remove rows from a table. A WHERE clause can be used to delete specified records based on conditions. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. It removes rows from a table or view. DELETE statements delete rows one at a time, logging each row in the transaction log, as well as maintaining log sequence number (LSN) information.
e.g.
delete from employee ;( this command will remove all the data from employee table)
delete from employee where emp_id=100;(This command will remove only that row from employee table where emp_id=100);
TRUNCATE
TRUNCATE removes all rows from a table without logging the individual row deletions .No triggers will be fired in TRUNCATE. As such, TRUNCATE is faster and doesn’t use as much undo space as a DELETE.
e.g. truncate table employee.( This command will remove all the data from the employee table)
DELETE vs TRUNCATE
1) Counter of the Identity column is reset in Truncate where it is not reset in Delete.
2) Delete keeps the lock over each row where Truncate keeps the lock on table not on all the row.
3) TRUNCATE is much faster than DELETE. The reason is when you type DELETE all the data get copied into the Rollback Tablespace first and then delete operation get performed. That is why in case of ROLLBACK , after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process takes time. But in case of TRUNCATE, it removes data directly without copying it into the Rollback Tablespace. That’s why TRUNCATE is faster. Once you truncate you can’t get back the data.(but in fact it can be rolled back as i proved in the end of the section.)
4) Truncate is faster in performance wise, because it is minimally logged in transaction log. Delete is slower than truncate because, it maintain logs for every record
5) DELETE command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. But it does not free the space containing the table. TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
6) In truncate we cannot use WHERE Clause where as in delete we can specify
filters in WHERE clause.
7) TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. Delete activates a trigger because the operation is logged individually.
8) TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row
9) If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the column. DELETE retain the identity. This means if you have a table with an identity column and you have 100 rows with a seed value of 1, your last record will have the value 100 (assuming you started with value 1) in its identity columns. After truncating your table, when you insert a new record into the empty table, the identity column will have a value of 1 but DELETE will not do this. In the same scenario, after deleting rows, when inserting a new row into the empty table, the identity column will have a value of 101.
10) As TRUNCATE is a DDL (data definition language) statement it does not require a commit to make the changes permanent. And this is the reason why rows deleted by truncate could not be rollbacked. On the other hand DELETE is a DML (data manipulation language) statement hence requires explicit commit to make its effect permanent.
11) We may use DELETE statement against a view (with some limitations). But we can’t use TRUNCATE statement against a view.
Myth about Rollback in DELETE and TRUNCATE
Myth: We cannot rollback in TRUNCATE but in DELETE we can rollback.
But this is not true. Truncate can also be rolled back if used with transaction. Lets proove it:
DELETE example:
BEGIN TRAN
DELETE EMPLOYEE
SELECT * FROM EMPLOYEE (no data because of DELETE command)
ROLLBACK
SELECT * FROM EMPLOYEE (data is rolled back because of ROLLBACK command)
TRUNCATE example:
BEGIN TRAN
TRUNCATE TABLE EMPLOYEE
SELECT * FROM EMPLOYEE (no data because of TRUNCATE command)
ROLLBACK
SELECT * FROM EMPLOYEE (data is rolled back because of ROLLBACK command similar to DELETE command)

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome...