身份增量中的主键问题 [英] Primary Key Problem in identity increment

查看:93
本文介绍了身份增量中的主键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我创建了一个表ID int是我放置的那个表的主键

身份规范:是
IsIdentity:是
身份增量:1
身份种子:1

我的问题是

当我插入记录时,它的工作正常

但是当我删除记录时,身份不会被贬低

例如,我有15条记录,我删除了第15条记录,并插入了新记录,但是现在需要16条记录作为新记录的15.

上面的实验是在asp.net网站上的Gridview控件上完成的,我是从Grideview启用删除属性中完成的

Hi
i created a table ID int is a primary key for that table i put

Identity Specification :yes
IsIdentity :yes
Identity Increment :1
Identity Seed :1

my problem is

when i inserting records its working fine

but when i deleting records the identity is not decrimented

for example i have 15 records i deleted the 15 th record and inserted the new record but now it takes 16 as new record insted of 15.

The Above experiment is done on Gridview control in asp.net website i did this from Grideview Enable Deleting Property
how to solve this

推荐答案

不,不会.原因很简单:
如果您有第二个表,其中包含与第一个表相关的信息,该怎么办?

例如,如果您的第一个表是客户",而第二个表是购买",则第二个表中将有一个数字字段,用于将购买与第一个表中的特定客户相关联.如果您不告诉SQL,它仍然可以正常工作,但是,如果SQL对任何身份字段重新排序,那么购买将不会受到影响,并会反映错误的客户.


否:SQL不会对身份字段进行重新排序:实际上,它不会两次生成相同的数字-这就是整个想法.如果确实必须使用有序编号,则不要使用标识字段,而是从给定排序顺序的每条记录的顺序位置开始工作.
No, it won''t be. The reason is simple:
What if you have a second table, which contains information related to the first?

For example, if your first table is Customers, and your second is Purchases, then there would be a numeric field in your second table to relate the purchase to a specific customer in the first. If you do not tell SQL about it, it will still work fine, but if SQL re-ordered any identity fields then purchases would not be affected and would reflect on the wrong customer.


No: SQL does not re-order identity fields: in fact, it will not generate the same number twice - that''s the whole idea. If you really must work with ordered numbers, then do not use an identity field, but work from the ordinal position of each record for a given sort order.


这是Incriment的关键所在.除非您手动输入ID而不是通过自动编号输入,否则无法伪装.

您将获得MAX ID并添加1并将其插入新记录.
This is the point of Incriment there is no way to decriment unless you enter the ID manualy and not by autonumber.

You get the MAX ID and add 1 and insert it to the new record.


您好,
删除时身份永远不会减少.它可以递增或递减.您可以模拟删除时递减的行为(通过重置种子),但是强烈建议不要这样做.
问候
Hi,
The identity is never decremented on delete. It either increments or decrements. You can simulate the behavior of decrement on delete (by resetting the seed), but its strongly discouraged.
Regards


这篇关于身份增量中的主键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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