数据库主键不连续

数据库主键不连续问题:

分析:
   (1)首次插入数据(正常)

   (2)重启服务后,再次插入(不连续)

   (3)分析:
         谁跟你说过自增字段保证这个字段值连续的?自增字段就是“保证数值不减小”而已。
     如果你以为自增字段是做业务流水号的,那是你理解错误。


     sqlserver的增长机制跟oracle不一样
     oracle用的序列,每次从序列里去取,跟你表里存了什么无关.
     如果你当前序列值是100,你手动插入个101进去,下次自动插入的时候就会报错

     而sqlserver本身按你当前内容最大值累加



Is it really a bug?


Microsoft declares it is a feature rather than a bug and in many scenarios it would be helpful. But in our case, it would not be acceptable because that number is shown to the client and the client will be surprised to see that new number after jump and the new number depends on how many times SQL Server is restarted. If it is not visible to the client, then it might be acceptable so that the number is used internally.


Solutions
If we are not interested in this so called feature, then we can do two things to stop that jump.


Using Sequence
Register -t272 to SQL Server Startup Parameter





官方解释:https://www.codeproject.com/Tips/668042/SQL-Server-2012-Auto-Identity-column-Value-Jump-Is



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值