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

后端 未结 3 1217
佛祖请我去吃肉
佛祖请我去吃肉 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: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

提交回复
热议问题