SQL Server 和 Identity 列中的间隙
我刚刚注意到,如果表中有一个标识列,当我插入新行时,如果存在不连续性,SQL Server 2008 会自动填充序列。我的意思是,如果在我的身份列中我有 1,2,5,6,如果我在表中插入其他两行,系统会自动将 3,7 放入身份列中。
你知道如何控制这种行为吗?
谢谢
I just noticed that if I have an identity column in a table, when I insert new rows SQL Server 2008 is automatically filling up the sequence if there are discontinuity. I mean, if in my identity column I have 1,2,5,6 if I insert other two rows in the table the system puts automatically 3,7 in the identity column.
Do you know how to control this behavior?
THANKS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是已定义和记录的 SQL Server 行为,您实际上无能为力对其进行更改。你想改变什么?
IDENTITY 列将保证唯一的、不断增加的 ID(只要您不乱用它们)——它们不保证其他任何内容。
SQL Server不会费力去发现序列中的“间隙”并填补它们。无论如何,我认为这不是一个好主意 - 如果您确实有 ID=3 的记录,然后将其删除怎么办?你真的想要下一条记录突然“回收”那个ID吗?我认为这不是一个好主意。
That is the defined and documented SQL Server behavior, and there's really not much you can do about changing it. What did you want to change about it??
IDENTITY columns will guarantee unique, ever-increasing ID's (as long as you don't mess around with them) - they don't guarantee anything else.
SQL Server will not go through the trouble of spotting "gaps" in your sequence and filling them up. I don't think that would be a good idea, anyway - what if you did have a record with ID=3, and then deleted it? Do you really want a next record to suddenly "recycle" that ID?? Not a good idea, in my opinion.