Wednesday, April 8, 2009

MySQL DELETE verus TRUNCATE

There are two ways to remove all the records from a table in MySQL; DELETE and TRUNCATE as follows:
DELETE FROM sometable
TRUNCATE TABLE sometable

These 2 statements are functionally equivalent except for the following:

  1. DELETE usually executes more slowly than TRUNCATE
  2. DELETE returns a row count indicating the number of records deleted while TRUNCATE always returns a row count of zero.
  3. TRUNCATE will reset any AUTO_INCREMENT fields to 1 while DELETE will maintain the current AUTO_INCREMENT value (note that there are exceptions to this depending on the type of database engine and version of MySQL).

No comments:

Post a Comment