自动生成的标识列到用户生成的标识列 [英] Auto Generated Identity column to User generated Identity Column

查看:81
本文介绍了自动生成的标识列到用户生成的标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





设置标识栏的正确方法是什么?



方式1:



自动生成的身份

Hi,

Which is the right way to set identity column?

Way 1:

Auto-generated identity

identity(1,1)





方式2:



手动生成的身份



Way 2:

Manual generated Identity

select @Identity = max(isnull(pkColumn,0))+1 from table





请澄清..

问候,

SJ



Please clarify..
Regards,
SJ

推荐答案

第一种方式是正确的方式,因为它保证了数据的完整性!

阅读: IDENTITY() [ ^ ]获取解释;)



第二种方式是伪身份并且保证数字和数据的完整性。为什么?

想象一下这种情况:你的 MAX(PK)= 500 。您添加了1条记录,因此您的身份相同: MAX + 1 = 501 。删除最后一条记录并重新添加。你会得到什么号码(身份)?如果在其他表中存在外键 501 ,会发生什么?您将失去数据完整性。
First way is right way, because it guarantee data integrity!
Read this: IDENTITY()[^] to get explanation ;)

Second way is pseudo-identity and does NOT guarantee unical numbers and data integrity. Why?
Imagine this situation: Your MAX(PK)=500. You have added 1 record, so your "identity" is equal: MAX+1=501. Delete last record and add it again. What number ("identity") you'll get? What happend if in other table exists foreign key with 501? You'll lose data integrity.


方式1:

[Id] [int] IDENTITY(1,1)NOT NULL
Way 1:
[Id] [int] IDENTITY(1,1) NOT NULL


这篇关于自动生成的标识列到用户生成的标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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