Difference between DELETE and TRUNCATE
1. DELETE :
DELETE is a DML(Data Manipulation Language) command and is used when we specify the row(tuple) that we want to remove or delete from the table or relation. The DELETE command can contain a WHERE clause. If WHERE clause is used with DELETE command then it remove or delete only those rows(tuple) that satisfy the condition otherwise by default it removes all the tuples(rows) from the table.
Syntax of DELETE command :
DELETE FROM TableName WHERE condition;
2. TRUNCATE :
TRUNCATE is a DDL(Data Definition Language) command and is used to delete all the rows or tuples from a table. Unlike the DELETE command, TRUNCATE command does not contain a WHERE clause. In the TRUNCATE command, the transaction log for each deleted data page is recorded. Unlike the DELETE command, the TRUNCATE command is fast. We cannot rollback the data after using the TRUNCATE command.
Syntax of TRUNCATE command:-
TRUNCATE TABLE TableName;
Let’s see the difference between DELETE and TRUNCATE command:-
S.NO Delete Truncate 1. The DELETE command is used to delete specified rows(one or more). While this command is used to delete all the rows from a table. 2. It is a DML(Data Manipulation Language) command. While it is a DDL(Data Definition Language) command. 3. There may be WHERE clause in DELETE command in order to filter the records. While there may not be WHERE clause in TRUNCATE command. 4. In the DELETE command, a tuple is locked before removing it. While in this command, data page is locked before removing the table data. 5. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log. 6. DELETE command is slower than TRUNCATE command. While TRUNCATE command is faster than DELETE command. 7. To use Delete you need DELETE permission on the table. To use Truncate on a table we need at least ALTER permission on the table. 8. Identity of column retains the identity after using DELETE Statement on table. Identity of the column is reset to its seed value if the table contains an identity column. 9. The delete can be used with indexed views. Truncate cannot be used with indexed views.
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.