What exactly is GUID? Why and where I should use it?
Asked Answered
W

16

98

What exactly is GUID? Why and where I should use it?
I've seen references to GUID in a lot of places, and in wikipedia, but it is not very clear telling you where to use it. If someone could answer this, it would be nice. Thanks

Wendolynwendt answered 16/12, 2008 at 16:7 Comment(0)
H
141

GUID technically stands for globally unique identifier. What it is, actually, is a 128 bit structure that is unlikely to ever repeat or create a collision. If you do the maths, the domain of values is in the undecillions.

Use guids when you have multiple independent systems or clients generating ID's that need to be unique.

For example, if I have 5 client apps creating and inserting transactional data into a table that has a unique constraint on the ID, then use guids. This prevents having to force a client to request an issued ID from the server first.

This is also great for object factories and systems that have numerous object types stored in different tables where you don't want any 2 objects to have the same ID. This makes caching and scavenging schemas much easier to implement.

Hudnut answered 16/12, 2008 at 16:14 Comment(2)
Very clean/concise answer thank you. However just a side to this (Might already be another question I realise), when create say a javascript/HTML based app, and you need to return records say for messages or something, is it better to only return a GUID to the frontend rather than the actual ID? reasoning is of course if it is the ID (auto-generated prim.key), then it would be fairly easy to guess another one - whereas a GUID is obviously a lot harder to guess??Knutson
“domain of values is in the quintillions” – No, the domain is vastly larger than that. The total number of possible version 4 UUIDs is 2ʌ122, or 5.3x10ʌ36 (5.3 undecillion). See Wikipedia.Housekeeping
T
32

A GUID is a "Globally Unique IDentifier". You use it anywhere that you need an identifier that guaranteed to be different than every other.

Usually, you only need a value to be "locally unique" -- the Primary Key identity in a database table,for example, needs only be different from the other rows in that table, but can be the same as the ID in other tables. (no need for a GUID here)

GUIDs are generally used when you will be defining an ID that must be different from an ID that someone else (outside of your control) will be defining. One such place in the Interface identifier on ActiveX controls. Anyone can create an ActiveX, and not know with what other control someone will be using them with --- and there's nothing to stop everyone from giving their controls the same name. GUIDs keep them distinct.

GUIDs are a combination of the time (in very small fractions of a second) (so it assured to be different from any GUID defined before or later), and a number defining your location (sometimes taken from the MAC address of you network card) (so it's assured to be different from any other GUID defined right now by someone else).

They are also sometimes known as UUIDs (universally unique ID).

Tungsten answered 16/12, 2008 at 16:11 Comment(4)
Using a Guid as a primary key will slow down the database as indexing a number is way faster than indexing a varcharAdrianaadriane
Yes, that's why I used it as an example of a place where you DIDN't need a GUID.Tungsten
It's a trade-off between reads & writes. There is overhead in complex unique integer key brokering as well. On most smaller databases using guid keys will effectively not hurt performance. On larger databases, it can become an issue. Also, guids are varchars and are not indexed as such.Hudnut
Actually, a GUID is particular variant of the standard UUID. Specifically “Variant 2” identified by a three-bit pattern (110x N=c..d, 3 bits). In other words, all GUIDs are UUIDs, but not all UUIDs are GUIDs. GUIDs used mostly on Microsoft platforms. Microsoft documentation sometimes uses “GUID” when referring to all types of UUIDs, so the term can be confusing.Housekeeping
R
19

As addition to all the other answers, here is an online GUID generator:

http://www.guidgenerator.com/

What is a GUID?

GUID (or UUID) is an acronym for 'Globally Unique Identifier' (or 'Universally Unique Identifier'). It is a 128-bit integer number used to identify resources. The term GUID is generally used by developers working with Microsoft technologies, while UUID is used everywhere else.

How unique is a GUID?

128-bits is big enough and the generation algorithm is unique enough that if 1,0000,000,000 GUIDs per second were generated for 1 year the probability of a duplicate would be only 50%. Or if every human on Earth generated 600,000,000 GUIDs there would only be a 50% probability of a duplicate.

How are GUIDs used?

GUIDs are used in software development as database keys, component identifiers, or just about anywhere else a truly unique identifier is required. GUIDs are also used to identify all interfaces and objects in COM programming.

Rompers answered 16/12, 2008 at 16:22 Comment(2)
In Windows world a simple way to generate a GUID is to use Powershell command: [Guid]::NewGuid().ToString().ToUpperInvariant()Inventory
See my comment on Answer by Curran. Strictly speaking, all GUIDs are UUIDs, but not all UUIDs are GUIDs.Housekeeping
D
12

A GUID is a "Globally Unique ID". Also called a UUID (Universally Unique ID).

It's basically a 128 bit number that is generated in a way (see RFC 4112 http://www.ietf.org/rfc/rfc4122.txt) that makes it nearly impossible for duplicates to be generated. This way, I can generate GUIDs without some third party organization having to give them to me to ensure they are unique.

One widespread use of GUIDs is as identifiers for COM entities on Windows (classes, typelibs, interfaces, etc.). Using GUIDs, developers could build their COM components without going to Microsoft to get a unique identifier. Even though identifying COM entities is a major use of GUIDs, they are used for many things that need unique identifiers. Some developers will generate GUIDs for database records to provide them an ID that can be used even when they must be unique across many different databases.

Generally, you can think of a GUID as a serial number that can be generated by anyone at anytime and they'll know that the serial number will be unique.

Other ways to get unique identifiers include getting a domain name. To ensure the uniqueness of domain names, you have to get it from some organization (ultimately administered by ICANN).

Because GUIDs can be unwieldy (from a human readable point of view they are a string of hexadecimal numbers, usually grouped like so: aaaaaaaa-bbbb-cccc-dddd-ffffffffffff), some namespaces that need unique names across different organization use another scheme (often based on Internet domain names).

So, the namespace for Java packages by convention starts with the orgnaization's domain name (reversed) followed by names that are determined in some organization specfic way. For example, a Java package might be named:

com.example.jpackage

This means that dealing with name collisions becomes the responsibility of each organization.

XML namespaces are also made unique in a similar way - by convention, someone creating an XML namespace is supposed to make it 'underneath' a registered domain name under their control. For example:

xmlns="http://www.w3.org/1999/xhtml"

Another way that unique IDs have been managed is for Ethernet MAC addresses. A company that makes Ethernet cards has to get a block of addresses assigned to them by the IEEE (I think it's the IEEE). In this case the scheme has worked pretty well, and even if a manufacturer screws up and issues cards with duplicate MAC addresses, things will still work OK as long as those cards are not on the same subnet, since beyond a subnet, only the IP address is used to route packets. Although there are some other uses of MAC addresses that might be affected - one of the algorithms for generating GUIDs uses the MAC address as one parameter. This GUID generation method is not as widely used anymore because it is considered a privacy threat.

One example of a scheme to come up with unique identifiers that didn't work very well was the Microsoft provided ID's for 'VxD' drivers in Windows 9x. Developers of third party VxD drivers were supposed to ask Microsoft for a set of IDs to use for any drivers the third party wrote. This way, Microsoft could ensure there were not duplicate IDs. Unfortunately, many driver writers never bothered, and simply used whatever ID was in the example VxD they used as a starting point. I'm not sure how much trouble this caused - I don't think VxD ID uniqueness was absolutely necessary, but it probably affected some functionality in some APIs.

Dungdungan answered 16/12, 2008 at 16:41 Comment(0)
T
11

GUID or UUID (globally vs Universally) Unique IDentifier is, well, a unique ID :) When you need something really unique machine generated, there are libraries to get you one.

See GUID on wikipedia for details.

As to when you don't need a GUID, it is when a counter that you control (one way or another, like a SERIAL SQL type or a sequence) gets incremented. Indexing a "text" value (GUID in textual form) or a 128 bit binary value (which a GUID is) is far more expensive than an integer.

Tsimshian answered 16/12, 2008 at 16:11 Comment(0)
T
6

For me it's easier to think of them as simply "128-bit random values". Which is essentially what they are. There are some algorithms for including a bit of information in a few digits of your GUID (thus the random part gets a bit smaller), but still they are pretty large almost-random values.

Since they are so large, it is extremely unlikely that two GUIDs will ever be generated that are the same. For all practical purposes, every GUID ever generated is unique in the world.

I'll leave it to you to figure out where to use them, but other answers already have some examples. Let your imagination run wild. :)

Thrippence answered 16/12, 2008 at 16:22 Comment(3)
It may be easier to think of them as "128-bit random values" but that can lead to some problems if it is abused. Raymond Chan has a good write up on them blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspxRotten
That link is broken, so here's the updated one: devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823Thrippence
I'll note however that this day and age the most common GUID is the version 4 GUID which is indeed almost all random bits.Thrippence
W
6

Someone said they are conceptually 128-bit random values, and that is substantially true, but having done a little reading on UUID (GUID usually refers to Microsoft's implementation of UUID), I see that there are several different UUID versions, and most of them are not actually random. So it is possible to generate a UUID for a machine (or something else) and be able to reliably repeat that process to obtain the same UUID down the road, which is important for some applications.

Wiliness answered 16/12, 2008 at 16:46 Comment(1)
That's something people usually don't realize when talking about GUIDsStovall
T
4

Can be a hard thing to understand because of all the maths that goes on behind generating them. Think of it as a unique id. You can get Visual Studio to generate one for you, or .NET if you happen to be using C# or one of the many other applications or websites. They are considered unique because there is such a silly small chance you'll see the same one twice that it isn't worth considering.

Trever answered 16/12, 2008 at 16:15 Comment(0)
I
4

128-bit Globally Unique ID. You can generate GUIDs from now until sunset and you never generate the same GUID twice, and neither will anyone else. They are used a lot with COM.

As for example of something you would use them for, we use them in one of our products. Our users can generate categories and cards on various devices. We want to make sure that we don't confuse a category made on one device with a category created on a different one, so it's important that IDs are unique no matter who generates them, where they generate them, and when they generate them. So we use GUIDs (actually we use our own scheme using 64-bit numbers but they are similar to GUIDs).

Interposition answered 16/12, 2008 at 16:16 Comment(0)
A
4

I worked on an ACD call center system a few years back where we wanted to gather call detail records from multiple call processors into a single database. I setup a column in MS SQL to generate a GUID for the database key rather than using a system-generated sequential ID (identity column). Back then, this required setting the default value to NewID (or generating it in the code, but the NewID() function was safer). Of course, having a large value for a key may raise a few eyebrows, but I would rather give up the space than risk a collision.

I didn't see anyone address using a GUID as a database key so I thought it might help to know you could do that too.

Amazon answered 19/12, 2008 at 23:59 Comment(0)
M
3

GUID stands for "Globally Unique Identifier" and you use it when you want to have, erm, a Globally Unique Identifier.

In RSS feeds, for example, you should have a GUID for each item in the feed. That way, the feed reader software can keep track of whether you have read that item or not. Without a GUID, it would be impossible to tell.

A GUID differs from something like a database ID in that no matter who creates an object -- you, me, the guy down the street -- our GUIDs will always be different. There should be no collisions using a GUID.

You'll also see the term UUID, which stands for "Universally Unique Identifier." There is essentially no difference between the two. UUID is the more appropriate term. GUID is the term used by Microsoft.

Monro answered 16/12, 2008 at 16:16 Comment(0)
U
3

If you need to generate an identifier that needs to be unique during the whole lifetime of your application, you use a GUID.

Imagine you have a server with sessions, if you give each session a GUID, you are certain that it will be unique for every session ever created by your server. This is useful for tracing bugs.

Umlaut answered 16/12, 2008 at 16:16 Comment(0)
D
3

One particularly useful application of GUIDs that I've found is using them to track unique visitors in webapps where the visitors are anonymous (i.e. not logged in or registered).

Dimple answered 16/12, 2008 at 16:19 Comment(0)
M
2

GUID = Global Unique IDentifier.

Use it when you want to uniquely identify something in a global context.

This generator can be handy.

Mauromaurois answered 16/12, 2008 at 16:11 Comment(0)
K
1

The Wikipedia article on GUIDs is pretty clear on what they are used for - maybe rephrasing your question would help - what do you need a GUID for?

Kex answered 16/12, 2008 at 16:12 Comment(1)
"what do you need a GUID for?" I think that was the question.Selfrevealing
S
0

To actually see what it looks like on a windows computer, go to cmd or powershell.

  1. Powershell => [guid]::NewGuid()

  2. CMD => powershell [guid]::NewGuid()

enter image description here

Sacramentalism answered 8/6, 2021 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.