Monday, December 31, 2012

Difference between Truncate and Delete

i) Truncate an Delete both are used to delete data from the table.
ii) These both the command will only delete the data of the specified table, they cannot remove the whole table data along with its structure both the SQL statements are used to only delete the data from the table but they both differ from each other in many aspects like syntax, performance, resources uses etc.
So, first let’s take a look of both of these terms (Truncate and Delete),


Truncate
-> Truncate command in SQL removes all rows from a table without logging the individual row deletion in the transaction log.
-> Truncate statement having the sane functionality as the Delete command has that is it deletes the data from the table without modifying or deleting the structure of the table.
-> You can not use the Where Clause with this (Truncate) statement.

Syntax:
TRUNCATE TABLE [ { database_name.[ schema_name ]. | schema_name . } ] table_name Table_name : Is the name of the table to truncate or from which all rows are removed.
Simple it looks like below query.
TRUNCATE TABLE Employees
The above command will delete all data from the table Employees.




Delete
-> Delete command in SQL also removes all rows from a table with logging the individual row deletion in the transaction log.
-> You can use the Where Clause with this (Delete) statement.
-> For more focus please follow the link Click me

Syntax:
DELETE FROM TABLE_NAME[ { database_name.[ schema_name ]. | schema_name . } ] table_name Table_name : Is the name of the table to truncate or from which all rows are removed.
Simple it looks like below query.
DELETE FROM Employees
The above command will delete all data from the table Employees.
In case of delete statements you can limit your delete query using where clause to delete, only particular records that fulfills the condition of where clause will be deleted not the all records.
It looks like below query with where clause.
DELETE FROM Employees Where EmployeeID IN (1,2,3)


Happy Coding!!! 

No comments:

Post a Comment