What is the difference between DELETE and TRUNCATE commands in T-SQL?

1 Answers
Answered by suresh

DELETE vs TRUNCATE in T-SQL

DELETE vs TRUNCATE in T-SQL

DELETE and TRUNCATE are two commonly used commands in T-SQL for managing data in a database. While both commands can be used to remove data from a table, they have distinct differences.

DELETE Command

The DELETE command is used to remove specific rows from a table based on a specified condition. It is a DML (Data Manipulation Language) command that can be rolled back using a transaction. DELETE command removes data row by row and also fires triggers associated with the table.

TRUNCATE Command

The TRUNCATE command, on the other hand, is used to remove all rows from a table without specifying any condition. It is a DDL (Data Definition Language) command that cannot be rolled back. TRUNCATE removes all the data in one shot without logging individual row deletions, making it faster than DELETE. TRUNCATE does not fire triggers associated with the table.

Key Differences

  • DELETE removes specific rows based on a condition, TRUNCATE removes all rows.
  • DELETE can be rolled back, TRUNCATE cannot.
  • DELETE triggers associated with the table, TRUNCATE does not.

It is important to choose the appropriate command based on the specific requirements of your database operation to ensure efficient and effective data management.

Answer for Question: What is the difference between DELETE and TRUNCATE commands in T-SQL?