SQLUSA

Microsoft SQL Server 2000

Articles

 

Globally Unique Identifier
By Kalman Toth, M.Phil., M.Phil., MCDBA
February 1, 2005
Principal Trainer at: http://www.sqlusa.com

 

If you are a big multi-national company and don't want your unit in England assign the same part number as your unit in Australia, you have to use the globally unique identifier datatype. Also, even for smaller firms there may be conflict in row identification if replication is being used. One way to avoid that is using globally unique identifiers.

GUID formed by taking 8 bytes from your server network id (supposedly unique worldwide) and adding another 8 bytes to it. For example, in hexadecimal form 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid uniqueidentifier value.

A GUID column normally populated by the newid() funtion.

It is used in a table create like:

PartID uniqueidentifier NOT NULL DEFAULT newid()

There is a second use for the newid() function: randomize the order in a select:

Select top 10 * from TableA
Order by newid()

The statement above would return random selection of rows whenever executed. When would you need to return random results? For example, in online dating when displaying photos and profiles, you may not want to show the same predictable first page over and over again.


Best in the World in SQL Server Training
 
SQLUSA.com Home Page