Technical Articles

Difference between TRUNCATE, DELETE and DROP commands?

0 0

Difference Between TRUNCATE, DELETE, and DROP Commands

In SQL, TRUNCATE, DELETE, and DROP are commands that deal with data removal or deletion, but they work in very different ways. Understanding these differences is important when managing databases efficiently.

Real-Time Analogy to Understand

  • TRUNCATE: Imagine cleaning out the inside of a box but leaving the box itself intact. You remove everything inside the box (i.e., the rows) but keep the structure (i.e., the table).
  • DELETE: This is like carefully selecting individual items inside the box and removing them one by one. The box stays, and only the items you specify are deleted.
  • DROP: This is like destroying the entire box along with its contents. Both the data and the structure (the table itself) are removed permanently.

Summary Table of Key Differences

Aspect TRUNCATE DELETE DROP
Operation Removes all rows, keeps the table Removes specified rows based on condition Removes the entire table, including its structure
WHERE Clause Support No Yes No
Rollback Possible No (in most cases) Yes (if within a transaction) No
Locking Table-level lock Row-level lock Table-level lock
Performance Fast, as it deallocates data pages Slower, especially with large data sets Fast, since it deletes the structure

Examples with SQL Queries

TRUNCATE Example

TRUNCATE TABLE Employees;

This command will remove all rows from the Employees table but will keep the table structure intact for future use.

DELETE Example

DELETE FROM Employees WHERE EmployeeID = 1;

This command removes a specific row from the Employees table where the EmployeeID is 1. You can use a WHERE clause to filter which rows to delete.

DROP Example

DROP TABLE Employees;

This command will remove the entire Employees table, including both its data and structure.

5 Important Interview Questions and Answers

1. Can TRUNCATE be rolled back?

Answer: TRUNCATE can only be rolled back if it’s within a transaction. Otherwise, it’s a permanent operation.

2. Which is faster: DELETE or TRUNCATE?

Answer: TRUNCATE is faster because it doesn’t log individual row deletions and instead deallocates data pages directly.

3. Can TRUNCATE activate triggers?

Answer: No, TRUNCATE does not activate triggers, whereas DELETE can activate them.

4. Can you use a WHERE clause with the TRUNCATE command?

Answer: No, TRUNCATE removes all rows from a table without the option to filter using a WHERE clause.

5. Can we use the TRUNCATE command on a table that has a foreign key?

Answer: No, you cannot TRUNCATE a table that is referenced by a foreign key constraint unless the foreign key is disabled or removed.

5 Tricky Interview Questions and Answers

1. What happens to the identity column when a TRUNCATE command is issued?

Answer: When you TRUNCATE a table, the identity column is reset, meaning the next insertion will start from the seed value again.

2. Can DELETE and TRUNCATE both be rolled back if used in a transaction?

Answer: Yes, both can be rolled back if used inside a transaction.

3. Why is TRUNCATE considered a DDL (Data Definition Language) command while DELETE is considered a DML (Data Manipulation Language) command?

Answer: TRUNCATE modifies the structure of the table (by deallocating pages), making it a DDL operation, while DELETE only modifies the data in the table, making it a DML operation.

4. Can we DELETE all rows without using a WHERE clause? What would be the difference from TRUNCATE?

Answer: Yes, you can delete all rows using DELETE FROM table without a WHERE clause, but unlike TRUNCATE, DELETE will log each row deletion individually, making it slower and it will not reset the identity column.

5. How does DROP differ from TRUNCATE in terms of data recovery?

Answer: DROP removes the table structure completely, making data recovery more complex. TRUNCATE keeps the structure intact, so it’s easier to reinsert data.

 

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?