This question already has an answer here:
One thing struck in my head about mysql/sqlserver i.e delete/ truncate
Which one is better and faster ?
where to use delete?
where to use truncate?
This question already has an answer here:
One thing struck in my head about mysql/sqlserver i.e delete/ truncate
Which one is better and faster ?
where to use delete?
where to use truncate?
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
DELETE
TRUNCATE
The most important difference is DELETE operations are transaction-safe and logged, which means DELETE can be rolled back. TRUNCATE cannot be done inside a transaction and can’t be rolled back. Because TRUNCATE is not logged recovering a mistakenly TRUNCATEd table is a much bigger problem than recovering from a DELETE.
DELETE will fail if foreign key constraints are broken; TRUNCATE may not honor foreign key constraints (it does for InnoDB tables). DELETE will fire any ON DELETE triggers; TRUNCATE will not.
Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.
when table set to empty, and need reset auto-incrementing keys to 1. It's faster than DELETE because it deletes all data. DELETE will scan the table to generate a count of rows that were affected.
need rows to delete based on an optional WHERE clause. need logs and apply foreign key constraints
The DELETE
command is used to remove rows from a table
TRUNCATE
removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE
is faster and doesn't use as much undo space as a DELETE
.
Difference:
1) delete the complete data from the table and next auto increment id will start with 1 Where as delete will start with next id.
2) Both will keep strucutre intact and delete data only.
3) with delete you can use limit where as with truncate you can't.