Naveen P.N

Just another Blog to play with programming

Archive for the ‘SQL’ Category

Difference b/w Truncate & Delete

without comments

Truncate an Delete both are used to delete data from the table. These both the command will only delete the data of the specified table, they cannot remove the whole table data along with its structure. Now it’s ok that both the SQL statements are used to delete only 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’t 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 querry.
TRUNCATE TABLE authors

The above command will delete all data from the table author.


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. In case of delete I am writing just the simple syntax for more detailed explanation of delete syntax visit this link: DELETE (Transact-SQL).
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 querry.
DELETE FROM authors

The above command will delete all data from the table author.

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 querry with where clause.
DELETE FROM authors Where AuthorId IN (1,2,3)

Written by Naveen P.N

June 26, 2008 at 2:54 am

Posted in SQL