How do I reset sequence numbers to become consecutive?

前端 未结 5 1429
清歌不尽
清歌不尽 2020-12-19 06:50

I\'ve got a mysql table where each row has its own sequence number in a \"sequence\" column. However, when a row gets deleted, it leaves a gap. So...

1
2
3
         


        
5条回答
  •  再見小時候
    2020-12-19 07:43

    Assuming that this is an ID field, you can do this when you insert:

    INSERT INTO yourTable (ID)
    SELECT MIN(ID)
    FROM yourTable
    WHERE ID > 1
    

    As others have mentioned I don't recommend doing this. It will hold a table lock while the next ID is evaluated.

提交回复
热议问题