Are Guids guaranteed to be unique?

后端 未结 3 778
后悔当初
后悔当初 2021-01-21 08:31

I\'m using Guids as primary keys in my database and was wondering if it is ever possible that a duplicate Guid might be generated. Are Guids guaranteed to be unique?

相关标签:
3条回答
  • 2021-01-21 08:50

    While each generated GUID is not guaranteed to be unique, the total number of unique keys (2^128 or 3.4×10^38) is so large that the probability of the same number being generated twice is very small. For example, consider the observable universe, which contains about 5×10^22 stars; every star could then have 6.8×10^15 universally unique GUIDs.

    From Wikipedia.

    0 讨论(0)
  • 2021-01-21 08:54

    They are guaranteed to be unique if you use NEWSEQUENTIALID() to generate one. Still, Microsoft claims[1] that it will be unique and that

    no other computer in the world will generate a duplicate of that GUID value.

    Wikipedia states[2] that

    Microsoft added a function to the Transact-SQL language called NEWSEQUENTIALID(),[8] which generates GUIDs that are guaranteed to increase in value, but may start with a lower number (still guaranteed unique) when the server restarts

    In short: in practice, they're going to be unique.

    [1] http://msdn.microsoft.com/en-us/library/ms190215(SQL.105).aspx
    [2] http://en.wikipedia.org/wiki/Globally_unique_identifier#Sequential_algorithms

    0 讨论(0)
  • 2021-01-21 09:02

    While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is very small.

    You can check more info on that here.


    A possible solution to avoid duplicate guids (if you still want one) is to use the UNIQUE constraint.

    0 讨论(0)
提交回复
热议问题