Difference Between DELETE and TRUNCATE

Difference Between DELETE and TRUNCATE

7 mins read191 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Dec 8, 2023 14:59 IST

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.

2023_04_MicrosoftTeams-image-6.jpg

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

Recommended online courses

Best-suited Database and SQL courses for you

Learn Database and SQL with these high-rated online courses

– / –
2 months
4.24 K
6 weeks
– / –
30 hours
– / –
– / –
– / –
1 hours
– / –
80 hours
– / –
30 hours
– / –
31 hours

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.
What are Different Types of Databases?
What are Different Types of Databases?
Databases have diverse types: RDBMS (structured with SQL), NoSQL (flexible for unstructured data), Hierarchical (tree-like structure), Network (complex data relationships), and OODBMS (suited for complex structures). Choice depends on data...read more
MongoDB Vs. MySQL: Applications and Differences
MongoDB Vs. MySQL: Applications and Differences
MongoDB is a NoSQL database that uses a document data model, which allows for more flexible and scalable data organization.MySQL is a relational database that uses a table data model....read more
MariaDB vs MySQL – What’s the Difference?
MariaDB vs MySQL – What’s the Difference?
Maria DB and MySQL are open-source relational database management systems (RDBMS). MariaDB vs MySQL: MariaDB is relatively faster than MySQL and does not support data masking and dynamic column. On...read more

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

  1. 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.
  2. Implementing data retention policies: you might need to delete old employee data after a certain period has elapsed.
  3. 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.
  4. 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.
  5. 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

  1. 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.
  2. 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. 
  3. 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.
  4. 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

What is the Difference Between SQL and MySQL?
What is the Difference Between SQL and MySQL?
SQL is query programming language to manage RDBMS while MySQL is RDBMS that uses SQL.
In this article, we will discuss the key differences between SQL (Structured Query Language) and...read more
Difference between SQL and NoSQL
Difference between SQL and NoSQL
This article designed for beginners will teach you difference between SQL and NoSQL. The tutorial includes concepts, such as what is a SQL, what is NoSQL, and more.
Difference Between DELETE and TRUNCATE
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...read more
How to Use TRUNCATE Command in SQL?
How to Use TRUNCATE Command in SQL?
SQL TRUNCATE TABLE command is used to remove the record from the table. Confused, the SQL DELETE statement also removes the record from the table. So, why use another command...read more
Delete Statement in SQL
Delete Statement in SQL
Delete statement is very important statement in SQL.This article covers delete statement with condition and without condition( with proper syntax and examples)
SQL ALTER TABLE : ADD, DROP, MODIFY, RENAME
SQL ALTER TABLE : ADD, DROP, MODIFY, RENAME
ALTER TABLE in SQL is used to change the structure of the existing table. In this article, we will briefly discuss how to add, modify, drop, rename columns, constraints, and...read more
How to use GROUP BY in SQL?
How to use GROUP BY in SQL?
The GROUP BY clause in sql is used to group the rows that have identical values by one or more columns. In this article, we will briefly discuss how GROUP...read more
How to use DROP command in SQL?
How to use DROP command in SQL?
In SQL, the DROP command is used to delete a database, table, index, or view. To use the DROP command, you must be very careful because it permanently deletes the...read more
SQL RIGHT JOIN – Example and Syntax
SQL RIGHT JOIN – Example and Syntax
Right Join in the sql returns all the rows from the right table and the matching entries from both tables. In this article, we will discuss how to use right...read more
Master SQL in No Time: The Ultimate Tutorial for Beginners!
Master SQL in No Time: The Ultimate Tutorial for Beginners!
Forget years of struggle; conquer SQL in weeks! This beginner-friendly guide cuts through the jargon and outlines a crystal-clear path to SQL mastery. No prior coding experience? No problem! Dive...read more
FOREIGN KEY IN SQL
FOREIGN KEY IN SQL
Foreign key concept is very important concept in SQL. This article explains creation, addition and deletion of foreign key. This article explains foreign key in SQL. It also explains the...read more
Cross Join in SQL
Cross Join in SQL
SQL Cross Join combines each row of one table with each row of another table and the number of rows in the result set is the product of number of...read more

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?

  1. 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.
  2. Implementing data retention policies: you might need to delete old employee data after a certain period has elapsed.
  3. 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.
  4. 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.
  5. 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?

  1. 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.
  2. 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. 
  3. 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.
  4. 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.
About the Author
author-image
Vikram Singh
Assistant Manager - Content

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