Good content takes time and effort to come up with.
Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.
TRUNCATE vs DELETE In MySQL
04th October 2020
2 mins read
Most of the time you might be wondering the difference between TRUNCATE vs DELETE in MYSQL, where to use, and when to use it. In this article, I will cover as much as possible.
DELETE [ LOW_PRIORITY ] [ QUICK ] [ IGNORE ] FROM table
[ORDER BY expression [ ASC | DESC ]]
- DELETE is a DML command.
- After DELETE command, if table has PrimaryKey with AutoIncrement then AutoIncrement counter wont be reset and starts from next id.
- DELETE statement is executed using a row lock, each row in the table is locked for deletion.
- We can specify filters in WHERE clause
- It deletes specified data if where condition exists else it will delete all the data.
- Delete activates a trigger because the operation are logged individually.
- Transaction safe. Hence all the deleted data can be rolled back.
- Slower than truncate because, it keeps logs.
- Rollback is possible.
TRUNCATE TABLE [database_name.]table_name;
- TRUNCATE is a DDL command.
- After TRUNCATE command, if there is PrimaryKey with AutoIncrement then AutoIncrement counter will be reset to 1.
- TRUNCATE TABLE always locks the table and page but not each row.
- Cannot use WHERE Condition.
- It deletes all the data from the table.
- TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
- Faster in performance wise, because it doesn't keep any logs.
- Not Transaction safe. Hence we cannot rollback data.
- Rollback is not possible only.
- DELETE and TRUNCATE both can be rolled back when used with TRANSACTION (TRUNCATE can be rolled back in SQL Server, but not in MySQL).
- DELETE and TRUNCATE both keep table structure intact and only deletes data.