填补 IDENTITY 列值的空白

问题描述:

我有一个带有 IDENTITY 列的表

I have a table with an IDENTITY column

[Id] int IDENTITY(1, 1) NOT NULL

在添加/删除一些行后,我以 Id 值的差距结束:

After some rows beeing added/removed I end with gaps in Id values:

Id   Name
---------
1    Tom
2    Bill
4    Kate

是否有一种简单的方法可以将值压缩为

Is there an easy way to compress the values to

Id   Name
---------
1    Tom
2    Bill
3    Kate

?

我认为创建一个具有相同模式的第二个表很容易,导入所有数据(当然身份列除外;让第二个表开始renumbering) 从第一个表中删除第一个表并将第二个表重命名为原始名称.

I think it's pretty easy to create a 2nd table with the same schema, import all the data (except for the identity column of course; let the 2nd table start renumbering) from the first table, drop the first table and rename the 2nd to the original name.

如果您有大量的 FK 关系要与其他表等重建,那么容易性可能会受到质疑.

Easiness may be in question if you'd have a ton of FK relationships to rebuild with other tables etc.