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 | StackCoder


TRUNCATE vs DELETE In MySQL


04th October 2020 2 mins read
Share On     Share On WhatsApp     Share On LinkedIn


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


Syntax

DELETE [ LOW_PRIORITY ] [ QUICK ] [ IGNORE ] FROM table
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
[LIMIT number_rows];



  1. DELETE is a DML command.
  2. After DELETE command, if table has PrimaryKey with AutoIncrement then AutoIncrement counter wont be reset and starts from next id.
  3. DELETE statement is executed using a row lock, each row in the table is locked for deletion.
  4. We can specify filters in WHERE clause
  5. It deletes specified data if where condition exists else it will delete all the data.
  6. Delete activates a trigger because the operation are logged individually.
  7. Transaction safe. Hence all the deleted data can be rolled back.
  8. Slower than truncate because, it keeps logs.
  9. Rollback is possible.

TRUNCATE


Syntax

TRUNCATE TABLE [database_name.]table_name;



  1. TRUNCATE is a DDL command.
  2. After TRUNCATE command, if there is PrimaryKey with AutoIncrement then AutoIncrement counter will be reset to 1.
  3. TRUNCATE TABLE always locks the table and page but not each row.
  4. Cannot use WHERE Condition.
  5. It deletes all the data from the table.
  6. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
  7. Faster in performance wise, because it doesn't keep any logs.
  8. Not Transaction safe. Hence we cannot rollback data.
  9. Rollback is not possible only.

Common


  1. DELETE and TRUNCATE both can be rolled back when used with TRANSACTION (TRUNCATE can be rolled back in SQL Server, but not in MySQL).
  2. DELETE and TRUNCATE both keep table structure intact and only deletes data.







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing