0

As I said, I'm currently working in a project which needs to persist data somehow, but isn't needed to do it permanently, I mean, I don't want to create a SQL DB or something like this. So I thought to do it using a Local Database (Entity Data Model) empty and I create a few entities working as tables as I'll show you:

enter image description here

I had declared the marked variables as Guid type. The fact is that I worked just a few times using Guid's and I'm not sure if I understand very well for what purpose are they but it looks like it would fit as an Id for an object. Looking at the definition from Microsoft says that "Such an identifier has a very low probability of being duplicated." and accordance with this it would be enough for me this time. I prefer to use this object that create my own Id as an int value, and being forced to increasing it every time I need to insert a new row.

did you had to face a problem like this? (sorry for my english)

Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
  • Id are usually `ints`, but only because they are "easier", and you can use `identity` to auto-increment the number. It's perfectly ok to use Guids though. – gunr2171 Jul 22 '14 at 16:55
  • Yes, I know that is the common way, but no one can deny that it might be some more effort that this. Thanks for your answer! – Mauro Bilotti Jul 22 '14 at 16:56
  • Do you know "how much low" is that posibility for being duplicated?? – Mauro Bilotti Jul 22 '14 at 16:59
  • "more effort" depends on your situation. I find that using guids is harder, because a human can't read them as easy as an int, and I have to manually create a new guid each time on an insert. Also, I don't understand exactly what you are asking. – gunr2171 Jul 22 '14 at 16:59
  • 1
    [This](http://stackoverflow.com/questions/829284/guid-vs-int-identity) and [this](http://stackoverflow.com/questions/404040/how-do-you-like-your-primary-keys) question may help you out, along with [this article](http://www.sqlskills.com/blogs/kimberly/guids-as-primary-keys-andor-the-clustering-key/). Whatever you decide to use, you should probably have a justifiable reason for not doing the norm. – sgbj Jul 22 '14 at 17:01
  • 1
    @MauroBilotti Low as in if you generated numbers every nanosecond when the universe began you still probably wouldn't have a duplicate to this day – jamesSampica Jul 22 '14 at 17:27
  • And if you use a guid as your primary key do yourself a favor and make sure you use another column as your clustered index. A guid column as the clustered index will exceed 99% fragmentation with as few as a few thousand rows. – Sean Lange Jul 22 '14 at 22:10

2 Answers2

3

Yeah, guids are fine for unique identifiers - it's what they are designed for. Odds of a collision are incredibly low.

Is a GUID unique 100% of the time?

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.

Also, take a look at the following question:

Simple proof that GUID is not unique

This will run for a lot more than hours. Assuming it loops at 1 GHz (which it won't - it will be a lot slower than that), it will run for 10790283070806014188970 years. Which is about 83 billion times longer than the age of the universe.

Assuming Moores law holds, it would be a lot quicker to not run this program, wait several hundred years and run it on a computer that is billions of times faster. In fact, any program that takes longer to run than it takes CPU speeds to double (about 18 months) will complete sooner if you wait until the CPU speeds have increased and buy a new CPU before running it (unless you write it so that it can be suspended and resumed on new hardware).

If you want to use an integer as an identifier, you will want to make sure that there is a singleton in place in order to prevent two threads simultaneously performing an insert using the same ID. Otherwise you need to handle the exception that occurs when you insert a record with a non-unique ID.

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27
2

You can use GUIDs for identifiers and there are several advantages for doing that as well as several disadvantages. Most notably if you have to assign Ids to objects while disconnected from the (main) database, that is probably the easiest way to go. On the downside the GUIDs are harder to read and edit (compared to ints).

Using an int field for Id is actually very easy and you do not need to do any work to increment it yourself. Just declare the field as "Identity" in the db schema and the database will take care of incrementing it.

Z .
  • 12,657
  • 1
  • 31
  • 56