填补 IDENTITY 列值的空白 [英] Filling the gaps in values of IDENTITY column

查看:32
本文介绍了填补 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.

这篇关于填补 IDENTITY 列值的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆