重新排序/重置自动增量主键

重新排序/重置自动增量主键

问题描述:

我有一个自动增量主键的MySQL表。我删除了表中间的一些行。现在我有,例如,ID列中的这样:12,13,14,19,20.我删除了15,16,17和18行。

I have a MySQL table with an auto increment primary key. I deleted some rows in the middle of the table. Now I have, for example, something like this in the ID column: 12, 13, 14, 19, 20. I deleted the 15, 16, 17 and 18 rows.

我想重新分配/重置/重新排序主键,以便我有连续性,即使19 a 15,20 a 16等。

I want to reassign / reset / reorder the primary key so that I have continuity, i.e. make the 19 a 15, the 20 a 16, and so on.

您可以删除主键列并重新创建它。所有的ids应该按顺序重新分配。

You could drop the primary key column and re-create it. All the ids should then be reassigned in order.

但是这在大多数情况下可能是一个坏主意。如果你有其他表有外键的表,那么它肯定不会工作。

However this is probably a bad idea in most situations. If you have other tables that have foreign keys to this table then it will definitely not work.