Auto Increment after delete in MySQL

后端 未结 17 2150
醉酒成梦
醉酒成梦 2020-11-22 09:50

I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I\'ve noticed people with the same problem and with varied answer

17条回答
  •  生来不讨喜
    2020-11-22 10:20

    I got a very simple but tricky method.

    While deleting a row, you can preserve the IDs into another temporary table. After that, when you will insert new data into the main table then you can search and pick IDs from the temporary table. So use a checking here. If the temporary table has no IDs then calculate maximum ID into the main table and set the new ID as: new_ID = old_max_ID+1.

    NB: You can not use auto-increment feature here.

提交回复
热议问题