If you want to remove data from a table, you can do this in a couple of ways using TRUNCATE TABLE or DELETE. A common approach is to use DELETE to remove all rows in the table but TRUNCATE TABLE offers an alternative and I will list what the differences between the two are.
DELETE
[sourcecode language=’sql’]
–Delete rows with filter
DELETE FROM SchemaName.Table WHERE…
[/sourcecode]
[sourcecode language=’sql’]
–Delete all rows
DELETE SchemaName.Table
[/sourcecode]
[sourcecode language=’sql’]
–Delete all rows
DELETE FROM SchemaName.Table
[/sourcecode]
[sourcecode language=’sql’]
–Delete using JOINS
DELETE T1
FROM SchemaName.Table1 AS T1
JOIN SchemaName.Table2 AS T2 ON T1.Column1 = T2.Column1
[/sourcecode]
[sourcecode language=’sql’]
–Delete using IN
DELETE FROM SchemaName.Table1
WHERE Column1 IN (SELECT Column1 FROM SchemaName.Table2)
[/sourcecode]
[sourcecode language=’sql’]
–Delete using EXISTS
DELETE T1
FROM SchemaName.Table1 AS T1
WHERE EXISTS
(SELECT *
FROM SchemaName.Table2 AS T2
WHERE T1.Column1 = T2.Column1)
[/sourcecode]
- Provides more control over what data is and can be deleted.
- Will not reseed the identity value on an identity column
- Can be used where the DELETE operation does not violate foreign key constraint
- Can be a slow operation if the table is large
- Locking can be an issue if the operation is removing many rows
- Will fire triggers
- Transactions can be rolled back as LSN (log sequence number) is maintained in the transaction log
- Data pages consumed may need to be reclaimed using a DBCC SHRINKDATABASE operation
- DELETE will typically consume more transaction log space because of extra logging
TRUNCATE TABLE
[sourcecode language=’sql’]
TRUNCATE TABLE Schema.TableName;
[/sourcecode]
- WHERE clause is not optional – it will remove all data from your table
- Reseeds the identify value of an identity column
- Cannot be used on a table which is referenced by a foreign key constraint
- Can be considerably quicker than DELETE due to deallocation of data pages and minimal logging
- Cannot be used in replication or log shipping
- Will not fire triggers
- Cannot be used on tables which are part of an indexed view
- Who has permission: table owner, sysadmin, db_owner, db_ddladmin or any user with ALTER permission on the table
I wanted to expand on the differences in speed between the two operations.
When TRUNCATE TABLE is issued against a table, all the data pages are deallocated which are used to store the tables data. Compared with a DELETE, this is significantly faster as each DELETE operation is logged in the transaction log but only the deallocations are logged in the transaction log for a TRUNCATE operation.
The DELETE operation will consume database resource and locks and can therefore be considerably slower and have greater impact on your system in comparison to TRUNCATE TABLE. See this post on reducing overhead for large delete operations.
As each DELETE operation is logged inside of the transaction log, it is possible to rollback to a point in time but with TRUNCATE TABLE it is not possible to have this level of precision. In a rollback scenario, only a complete rollback could be performed.
As with a DELETE it is also possible to rollback a TRUNCATE if it has not yet been committed. You can do this by using transactions like in this very simple example:
[sourcecode language=’sql’]
BEGIN TRAN
TRUNCATE TABLE SchemaName.Table
–test success here
ROLLBACK TRAN
ELSE
COMMIT TRAN
[/sourcecode]
Time to wrap this up..
So there are a number of differences between the two. Both operations have their pros and cons and provide you with a choice over how you want to remove your data.
java67 says
Wonderful comparison of two, I have also shared couple of tips on this topic as http://java67.blogspot.hk/2013/07/difference-between-truncate-vs-delete-SQL-Command-database-interview-question.html. When to use truncate vs delete in database, programmer may find it useful.
Andy Hayes says
Glad you like the article. Thanks for the comment.