What is the Difference Between DELETE and TRUNCATE in SQL?

By Aina Parasher|Updated : May 13th, 2022

Difference Between DELETE and TRUNCATE in SQL: The primary difference between DELETE and TRUNCATE is that the delete statement deletes data without resetting a table's identification, whereas the truncate command resets the identity of a specific table.

Both DELETE and TRUNCATE commands can both be used to erase table data. TRUNCATE is a DDL command, while DELETE is a DML command. TRUNCATE can be used to erase all of the table's data while keeping the table's integrity. This article describes the difference between the DELETE and TRUNCATE commands, which are commonly used interchangeably yet are completely different.

Table of Content

What is DELETE in SQL?

It's a data manipulation language (DML) command that deletes records from a table that is no longer needed in the database. It deletes the entire row from the table and returns the number of rows that have been deleted. It specifies that we have a database backup before running this operation, as this query will not be able to restore deleted records.

As a result, database backups enable us to restore data at any time in the future. A WHERE clause can be included in the DELETE command. When the WHERE clause is used with the DELETE command, only those rows(tuples) that satisfy the criterion are removed or deleted; otherwise, all tuples(rows) are removed by default.

What is TRUNCATE in SQL?

In SQL, the TRUNCATE statement is a DDL (data definition language) command that removes all of the data from a table while leaving the structure intact. Because we can't utilize the WHERE clause with this operation, we can't filter records. Because the log is not kept while doing this action, we cannot revert the erased data after running this command.

The TRUNCATE command, unlike the DELETE command, does not have a WHERE clause. The transaction log for each destroyed data page is not recorded when using the TRUNCATE command. The TRUNCATE command is faster than the DELETE command. After invoking the TRUNCATE command, we are unable to reverse the data.

What is the Difference Between DELETE and TRUNCATE in SQL?

We have provided the difference between DELETE and TRUNCATE commands listed in the table below.

Difference Between DELETE and TRUNCATE in SQL

DELETETRUNCATE
When we wish to remove some or all of the records from a table, we use the DELETE statement. TRUNCATE is a SQL statement that deletes whole rows from a table.
DELETE is a DML command because it just changes the data in the table. TRUNCATE is a DDL command.
It can be used with the WHERE clause.It cannot be used with the WHERE clause. 
It is slower.It is faster.

Before deleting a table row, it locks it.

It locks the whole table.
It allows us to restore the deleted data using the COMMIT or ROLLBACK statement.After executing this command, we cannot restore the deleted data.

Comments

write a comment

FAQs on Difference Between DELETE and TRUNCATE

  • The main difference between DELETE and TRUNCATE commands in SQL is that a DELETE command is used if we want to delete records from the table whereas, a TRUNCATE command is used when we want to delete all the data from the table.

  • DELETE is a DML command in SQL. In order to delete any data from the table, we need DELETE permission on that particular table. It is slower than the TRUNCATE command because it maintains the log.

  • A TRUNCATE command is a DDL command in SQL. It cannot be executed with a WHERE clause. It is used to delete all the rows from a particular table. Hence, it is faster than the DELETE command.

  • Based on the space required, the difference between DELETE and TRUNCATE commands is that the DELETE command maintains a log for each deleted row. Hence, it requires more space than TRUNCATE.

  • Based on the table identity, the difference between DELETE and TRUNCATE commands is that the TRUNCATE resets the table identity. However, the DELETE command deletes the data and does not reset the table identity.

Follow us for latest updates