Difference Between DELETE and TRUNCATE
The DELETE command in SQL deletes specific rows from a table based on a specific condition using the WHERE clause, whereas the TRUNCATE command deletes all the rows from the table. In this article, we will discuss how these two commands in SQL differ from each other with the help of examples.
When working with relational databases, it’s essential to know the difference between “delete” and “truncate.” Both commands can be used to remove data from a table, but they work differently and have different use cases. This article will explore the differences between delete and truncate and when to use each command.
Table of contents
Best-suited Database and SQL courses for you
Learn Database and SQL with these high-rated online courses
Difference between DELETE and TRUNCATE
Parameter | DELETE | TRUNCATE |
Operation | Deletes specific rows from a table based on a specified condition using the WHERE clause. | Deletes all rows from a table. |
Rollback | It can be rolled back using the transaction log or a backup. | It cannot be rolled back. |
Identity | Does not reset the identity of the table’s primary key or any associated indexes. | Resets the identity of the table’s primary key and any associated indexes. |
Triggers | Triggers any associated triggers and constraints on the table. | Does not trigger any associated triggers or constraints on the table. |
Speed | Slower than TRUNCATE as it deletes one row at a time. | Faster than DELETE as it deallocates all data pages at once. |
Resources | Consumes more transaction log space and generates more locks and blocking. | Consumes less transaction log space and generates fewer locks and blocking. |
WHERE | Includes a WHERE clause to specify the condition for which rows to delete. | Does not support a WHERE clause. |
Also, explore: Mysql Online Courses & Certifications
Must Check: SQL Online Course and Certification
What is the DELETE command in SQL?
The DELETE command removes one or more rows from a table.
DELETE is a DML (Data Manipulation Language) command that deletes rows/tuples from a specified table or relation in SQL. You can delete any number of rows using the DELETE command. It is mostly combined with a WHERE clause to delete specific rows that match a condition. If you don’t add a WHERE clause to your query, the DELETE command deletes all rows and empties the table. You must have delete permission on the table to use this command. When using the DELETE command, you must specify which rows you want to delete using a WHERE clause. If you omit the WHERE clause, the command will delete all rows in the table, which is rarely what you want to do.
Syntax and example of DELETE
Syntax: DELETE FROM table_name
WHERE condition;
Example: DELETE FROM customers WHERE id = 1;
In this example, the DELETE statement will remove all rows from the customers’ table where the id column equals 1.
Want to master the concepts of SQL, then check out the video tutorial.
Also, explore: Top MySQL Interview Questions and Answers by Shiksha Online
Also, explore the following: What is the difference between SQL and MySQL?
Use Cases of DELETE
- Removing unwanted or duplicate data: Sometimes the table is no longer needed, or duplicate lows need removal. That can be removed using a delete statement.
- Implementing data retention policies: you might need to delete old employee data after a certain period has elapsed.
- Cleaning up after testing or development: You may create temporary data in a table that needs to be removed when you’re done during testing or development. The DELETE statement can be used to remove this data.
- Maintaining data integrity: If you have foreign key constraints between tables, you may need to delete rows from one table when corresponding rows are deleted from another. The DELETE statement can be used to implement this behaviour.
- Undoing mistakes: If you make a mistake while inserting or updating data, you can use the DELETE statement to remove the incorrect data and start over.
What is the TRUNCATE command in SQL?
The TRUNCATE command is used to remove all rows from a table.
SQL Truncate is a DDL (Data Definition Language) command. Delete all rows in the table. SQL Server stores table data in pages. The truncate command deallocates pages and deletes rows. Make an entry in the transaction log to free the page. Not all row deletions are recorded in the transaction log. Unlike the DELETE command, the TRUNCATE command does not use a WHERE clause, removing all data from the table in one operation. When you use the TRUNCATE command, the table structure is preserved, but all data is lost.
Syntax and example of TRUNCATE
Syntax: TRUNCATE TABLE table_name;
truncate query in SQL
Example: TRUNCATE TABLE customers;
In this example, the TRUNCATE statement will delete all rows from the customer’s table, leaving an empty table with the same columns and primary key.
Use Case of TRUNCATE
- Resetting Data: In the Case of table resetting, you need to remove the data and then use the truncate command. For example, you might want to clear the contents of a log table periodically to free up space.
- Removing Large Data Sets: If a large data set is no longer needed, you can quickly use the truncate command to remove it from your database quickly and efficiently.
- Removing Test Data: While testing the application in a test environment, you can use a truncate command to remove all the test data. This ensures that you start with a clean slate each time you run a test.
- Resetting auto-increment counters: While using an auto-increment column in a table to generate primary keys, truncating the table resets the counter for that column to the initial value.
Conclusion
Now that you know the main difference between the truncate and delete commands in SQL queries, remember that TRUNCATE is a DDL command. On the other hand, the DELETE command is a DML command. This distinction between delete vs truncate helps us assess these commands’ usage and broader meaning. Therefore, although you can use the DELETE command to remove one or more rows from a table, you may need to reset the table using the TRUNCATE command in certain circumstances.
Keep Learning!!
Keep Sharing!!
Related Reads
FAQs on Difference Between DELETE and TRUNCATE
What is the DELETE command in SQL?
DELETE is a DML (Data Manipulation Language) command that deletes rows/tuples from a specified table or relation in SQL. You can delete any number of rows using the DELETE command. It is mostly combined with a WHERE clause to delete specific rows that match a condition.
What are the use cases of DELETE command in SQL?
- Removing unwanted or duplicate data: Sometimes the table is no longer needed, or duplicate lows need removal. That can be removed using a delete statement.
- Implementing data retention policies: you might need to delete old employee data after a certain period has elapsed.
- Cleaning up after testing or development: You may create temporary data in a table that needs to be removed when you’re done during testing or development. The DELETE statement can be used to remove this data.
- Maintaining data integrity: If you have foreign key constraints between tables, you may need to delete rows from one table when corresponding rows are deleted from another. The DELETE statement can be used to implement this behaviour.
- Undoing mistakes: If you make a mistake while inserting or updating data, you can use the DELETE statement to remove the incorrect data and start over.
What is the TRUNCATE command in SQL?
SQL Truncate is a DDL (Data Definition Language) command. Delete all rows in the table. SQL Server stores table data in pages. The truncate command deallocates pages and deletes rows. Make an entry in the transaction log to free the page.
What are the use cases of TRUNCATE command in SQL?
- Resetting Data: In the Case of table resetting, you need to remove the data and then use the truncate command. For example, you might want to clear the contents of a log table periodically to free up space.
- Removing Large Data Sets: If a large data set is no longer needed, you can quickly use the truncate command to remove it from your database quickly and efficiently.
- Removing Test Data: While testing the application in a test environment, you can use a truncate command to remove all the test data. This ensures that you start with a clean slate each time you run a test.
- Resetting auto-increment counters: While using an auto-increment column in a table to generate primary keys, truncating the table resets the counter for that column to the initial value.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio