增量GUID和唯一标识符 [英] Incremental GUID and unique identifier
问题描述
我想使用C#和SQL Server按顺序生成Guid
例如
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000001
00000000-0000-0000-0000-000000000002
...................................
00000000-0000-0000-0000-00000000000F
00000000-0000-0000-0000-000000000010
.................
.............
4FF2EAFF-32BF-445D-8A61-2A9303828C2D
我需要能够在SQL Server中做到这一点(例如为int设置身份规范)
我需要像Guid.NextGuid()这样在C#中执行此操作,并且它会递增.
Thanx
I would like to generate Guids in Order using C# and SQL Server
Eg
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000001
00000000-0000-0000-0000-000000000002
...................................
00000000-0000-0000-0000-00000000000F
00000000-0000-0000-0000-000000000010
.................
.............
4FF2EAFF-32BF-445D-8A61-2A9303828C2D
I need to be able to do this in SQL server (like setting identity spec for int)
and I need to do this in C# like Guid.NextGuid() and it increments.
Thanx
推荐答案
我同意Kieth Barrow提出的解决方案".这样做确实是在滥用GUID字段.
话虽这么说,您有几种选择比尝试做的要容易.您需要多少条记录才能唯一标识?您可以使用BigInt,它允许的数字介于-9,223,372,036,854,775,808和9,223,372,036,854,775,807之间.
该表定义将允许18,446,744,073,709,551,615个唯一键.
I agree with the "solution" proposed by Kieth Barrow. It''s you''re really abusing the GUID field by doing this.
That being said you have a few options easier than what you''re trying to do. How many records are do you need to uniquely identify? Chances are you can use BigInt which allows numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.
This table definition would allow for 18,446,744,073,709,551,615 unique keys.
Create table Test (
Key bigint identity(-9223372036854775808, 1)
)
您可以使用十进制,这将允许-10e + 38到10e + 38
You could use decimal which would allows -10e+38 to 10e+38
create table Test (
Key test1 decimal(38,0) identity(-10000000000000000000000000000000000000,1)
)
这将为您提供2e + 38的值,这仍然比一个Guid所能达到的〜3.4e + 38值略短,但是如果您需要那么多,我会感到震惊.
2e + 38 = 2十亿= 20,000,000,000,000,000,000,000,000,000,000,000,000,000,000
That would give you 2e+38 values, which is still a little short of the ~3.4e+38 values you could have with a guid, but I''d be shocked if you need that many.
2e+38 = 2 Undecillion = 20,000,000,000,000,000,000,000,000,000,000,000,000
这超出了GUID的观点.他们应该是随机的,以至于它们非常独特.对于 G 全局 U nique ID 读取"通用唯一 ID(几乎可以保证:))"
通过像这样顺序生成值,就打破了它们的意义:如果从头开始运行代码,您将获得两次相同的"GUID".
您应该选择使用GUID或自动递增索引,但不能同时使用两者.如果您要实现的目标背后有原因,可以将其添加到您的问题中,并针对您已对其进行更新的回复发表评论.我再看一遍.
That defeats the point of a GUID. They are supposed to be random to the point that they are pretty much unique. for Globally Unique ID read "Universally Unique Id (almost guaranteed :))"
By generating values sequentially like this you are breaking the point of them: if you run your code from scratch you will get the same "GUID" twice.
You should settle on either a GUID or a Auto incrementing index, you can''t have both. If there is a reason behind what you are trying to achieve, can you add it to your question, and comment on this reply that you have updated it. I''ll take another look.
这篇关于增量GUID和唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!