sql-server主键(ID)不能正确递增 [英] sql-server primary key (ID) does not increment correctly
问题描述
我正在使用C#和SQL Server 2016进行项目.
I am working on a project with C# and SQL Server 2016.
在SQL Server Management Studio中,通过右键单击表并选择设计表,将主键添加到现有表中,它可以正常工作.
In SQL Server Management Studio I add a primary key to an existing table, by right clicking the table and choosing design table, it worked fine.
但是,一旦我添加了一些新用户并删除了一些用户,它就开始表现奇怪,我的意思是在 id = 4
之后,下一个用户应该具有 id = 5
,但是而是给它 id = 7
.
But as soon as I add some new users and deleted some users, it started behaving strange, I mean after id=4
next user should have id=5
but instead it was given id=7
.
SSMS的屏幕截图
推荐答案
这听起来令人难以置信,但是如果您想要递增的记录号,则SQL Server不支持您.回滚的事务或服务器重新启动会在数字中留下漏洞.
This may sound unbelievable, but if you want an incrementing record number, SQL Server has no support for you. A transaction that is rolled back or a server restart can leave holes in the numbers.
如果删除一行,则必须手动实现.假设记录1、2和3存在.您删除记录2.新订单应获得什么号码?如果您说2,请记住这意味着订单2是在订单3之后创建的,这会使很多人感到困惑.
If you delete a row, you'll have to implement that manually. Say that records 1, 2 and 3 exist. You delete record 2. What number should the new order get? If you say 2, remember that means order 2 is created after order 3, which would confuse a lot of people.
这篇关于sql-server主键(ID)不能正确递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!