Published on

August 30, 2019

Understanding Truncate and Delete in SQL Server

When it comes to deleting data from a table in SQL Server, there are two commonly used commands: TRUNCATE and DELETE. While both commands serve the purpose of deleting data, they have some key differences in terms of syntax, performance, and resource usage.

Truncate

The TRUNCATE command removes all rows from a table without logging the individual row deletions in the transaction log. It has the same functionality as the DELETE statement, but it cannot be used with a WHERE clause. The syntax for the TRUNCATE command is:

TRUNCATE TABLE table_name;

For example, to delete all data from the “authors” table, you would use the following query:

TRUNCATE TABLE authors;

Delete

The DELETE command also removes rows from a table, but it logs the individual row deletions in the transaction log. It can be used with a WHERE clause to specify which rows should be deleted. The syntax for the DELETE command is:

DELETE FROM table_name WHERE condition;

For example, to delete rows from the “authors” table where the AuthorId is 1, 2, or 3, you would use the following query:

DELETE FROM authors WHERE AuthorId IN (1,2,3);

Differences between Truncate and Delete

Here are some key differences between the TRUNCATE and DELETE commands:

  • Data vs. Structure: Both commands remove data from a table, but the table structure remains intact. To remove the entire table, you would use the DROP TABLE statement.
  • Conditional Deletion: The DELETE command allows for conditional deletion of data using the WHERE clause, while the TRUNCATE command does not.
  • Logging: Both commands are logged operations, but they work differently. The DELETE command logs each deleted row, which can cause the transaction log to grow rapidly. On the other hand, the TRUNCATE command logs the deallocation of data pages, making it faster and using fewer resources.
  • Identity Columns: TRUNCATE resets the counter used by an identity column to the seed value, while DELETE does not reset the counter.
  • DDL vs. DML: TRUNCATE is a DDL (data definition language) operation, while DELETE is a DML (data manipulation language) operation. This means that TRUNCATE puts a “Schema modification (Sch-M)” lock on the table, while DELETE does not.
  • Triggers: TRUNCATE does not fire triggers, while DELETE does. However, the DELETE trigger will only be fired for the DELETE command, not for TRUNCATE.

It’s important to note that both commands have their own use cases and limitations. For example, TRUNCATE cannot be used on tables with foreign key constraints or indexed views, while DELETE may fail if it violates a trigger or constraint.

Understanding the differences between TRUNCATE and DELETE can help you choose the right command for your specific needs when deleting data from a table in SQL Server.

Feel free to leave a comment or ask any questions about this article.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.