-1

I wonder about Guid duplication. I am creating a Guid to save database table as entity Primary Key.

Account account = new Account(Guid.NewGuid());

But I am confused. Does this cause a duplication on a database table because I am creating manually a Primary key and inserting it to the database.

Database engine does not generate Ids. After saving myriads of records, is there a possibility to have duplications?

Alex
  • 5,971
  • 11
  • 42
  • 80
barteloma
  • 6,403
  • 14
  • 79
  • 173
  • Chances of Guids conflicting is lower than winning any lottery so don't worry about it. – Jeroen Vannevel Mar 24 '15 at 08:27
  • 1
    "Database engine does not generate Ids" Why? You could also use an `int` value in an `IDENTITY` columns which autoincrements. Then the maximum is 2,147,483,647, if you need more use bigint. You could store the `Guid` in a different column if it's needed. – Tim Schmelter Mar 24 '15 at 08:30
  • @TimSchmelter I assume the answer is "because it's easier". Because, of course, mixing relational and object models is such a good idea :D – Luaan Mar 24 '15 at 08:37
  • Here is a good answer to a question [how C# generate GUID](http://stackoverflow.com/a/2757969/1849444). – teo van kot Mar 24 '15 at 08:38

3 Answers3

4

Not really.

How much of "not really" depends on the GUID type, and on your understanding of probabilities.

A "real" GUID, version 1, the value is guaranteed to be unique. It's formed by combining the MAC address of your network card (unique, unless you change it manually) and a timestamp.

A pseudo-random GUID, version 4, is not guaranteed to be unique, but it is extremely unlikely to get a collision anyway. You have 122 bits to work with, and 2^122 is a very big number. Like, really big. Using Guid.NewGuid() is fine - although it should be noted that the random numbers used to generate the GUID are not crypto-random.

Of course, different implementations of GUIDv4 will have markedly different entropies. If you just use Random to generate the numbers, you're nowhere near the 122-bit maximum. So don't think you can just write your own code to generate GUIDs, most of such attempts and with nothing more unique than just Random.Next() - by far not good enough for a primary key in a database.

Note that GUIDs are commonly used in scenarios like replication, which are completely built on two generated GUIDs being unique.

Luaan
  • 62,244
  • 7
  • 97
  • 116
0

the total number of unique such GUIDs is 2122 (approximately 5.3×1036). This number is so large that the probability of the same number being generated randomly twice is negligible

From Wiki

Richard
  • 106,783
  • 21
  • 203
  • 265
A_Sk
  • 4,532
  • 3
  • 27
  • 51
0

For your information SQL SERVER Generates Guid.
Make the data type of column ID as uniqueidentifier then in the properties bar, go to RowGuid then change it to yes.
P.S.
Make sure your ID is Primary Key.

JC Borlagdan
  • 3,318
  • 5
  • 28
  • 51
  • This is true, but in my experience, SQL Server basically auto increments the last digit of the GUID by one. If you need to expose this GUID to the user, it isn't random enough. – Maarten B Jul 05 '18 at 07:10
  • really? well until now, i'm still using this stuff, and it doesn't increment by one, it really generates a new guid.. not unless you placed something in identity specification. which i'm not sure will work cause i never tried. – JC Borlagdan Jul 05 '18 at 08:09