如何重新排序主键?

如何重新排序主键?

问题描述:

我在表类别"中删除了一行(第 20 行),请告诉我如何重新排序 catid(主键)?此时是19点后的21点.

I have deleted one row(row 20) in my "table category" ,please let me know that how can i reorder the catid (primary key)? at this time it is 21 after 19.

谢谢

你不能.你能得到的最接近的是 truncate table,它会删除表并重新创建它,这意味着你会丢失其中的所有数据,并且 ID 计数器重置为 0.除此之外,ID 将始终从最后插入的记录开始递增 1,无论该记录是否仍然存在.当然,您可以编写一个查询来修复所有当前的 ID,但是在下一次插入时,它仍然会创建一个新的间隙.更重要的是:如果没有间隙的顺序排序是您想要的,那么自动增量 ID 不是实现这一目标的正确方法.添加另一个 int 字段,您可以在其中手动跟踪此排序.

You cannot. The closest you can get is truncate table, which will drop the table and recreate it, which means you lose all data in it, and the ID counter is reset to 0. Other than that, ID will always increment by one from the last inserted record, no matter if that record still exists or not. You can write a query to fix all your current IDs, of course, but upon next insert, it'll still create a new gap. More to the point: if a sequential ordering without gaps is what you want, auto incremental ID is not the proper way to achieve that. Add another int field where you manually keep track of this ordering.