Insert after truncate start from 1; but insert after delete resumes from previous value

后端 未结 3 1215
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 09:07

In my table there is column name id which is auto increment integer. When I truncate table and then try to perform insert query it starts from 1, but if I do delete operation an

相关标签:
3条回答
  • 2021-01-28 09:17

    The current max value for the autoincrement function is stored in the table definition (you can see it when you run show create table idle. When you delete all rows from the table, the autoincrement value will stay the same. When you truncate the table you basically drop the table and recreate it, which resets the the autoincrement value to 0.

    Hope this helps.

    0 讨论(0)
  • 2021-01-28 09:18

    you give AUTO INCREMENT in id that's why it is behave like this.whenever you truncate its reset the identity while in case of delete operation its start from last value of your id. if don't want this behavior remove AUTO INCREMENT index from id field.

    0 讨论(0)
  • 2021-01-28 09:22

    I think you are about to reset auto increment, run this query after delete query:

     ALTER TABLE tablename AUTO_INCREMENT = 1 ;
    

    NOTE: be careful about duplicate keys, if you delete some ids and reset ids to 1, while id=2 or n exists!

    Note that you cannot reset the counter to a value less than or equal to any that have already been used. For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one. For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.

    some other actions and notes mentioned in this topic

    0 讨论(0)
提交回复
热议问题