How do I reset sequence numbers to become consecutive?

前端 未结 5 1425
清歌不尽
清歌不尽 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:39

    If this is your PK then you shouldn't change it. PKs should be (mostly) unchanging columns. If you were to change them then not only would you need to change it in that table but also in any foreign keys where is exists.

    If you do need a sequential sequence then ask yourself why. In a table there is no inherent or guaranteed order (even in the PK, although it may turn out that way because of how most RDBMSs store and retrieve the data). That's why we have the ORDER BY clause in SQL. If you want to be able to generate sequential numbers based on something else (time added into the database, etc.) then consider generating that either in your query or with your front end.

提交回复
热议问题