Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how are GUIDs generated in SQL Server?

How are GUIDs generated in SQL Server?

I understand that I should use newid(), but what is the algorithm that function uses? Is it a hash of the time/date?

like image 581
Alex Gordon Avatar asked Jul 13 '10 23:07

Alex Gordon


People also ask

How is a GUID generated?

Basically, a a GUID is generated using a combination of: The MAC address of the machine used to generate the GUID (so GUIDs generated on different machines are unique unless MAC addresses are re-used) Timestamp (so GUIDs generated at different times on the same machine are unique)

How are GUIDs stored in SQL?

The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites.

How do I create a new GUID in SQL Server?

GUIDs can be added to any table. If the table you want to edit participates in replication or offline mapping or contains a GUID, you must insert a unique value to the global ID or GUID column when you insert a new record to the table using SQL. To do this, you can use the newid() function.

How is Newid generated in SQL Server?

In SQL Server, you can use the NEWID() function to create a unique value. More specifically, it's an RFC4122-compliant function that creates a unique value of type uniqueidentifier. The value that NEWID() produces is a randomly generated 16-byte GUID (Globally Unique IDentifier).


1 Answers

The algorithm to create it is described here: http://en.wikipedia.org/wiki/Globally_Unique_Identifier

Algorithm

In the OSF-specified algorithm for generating new (V1) GUIDs, the user's network card MAC address is used as a base for the last group of GUID digits, which means, for example, that a document can be tracked back to the computer that created it. This privacy hole was used when locating the creator of the Melissa worm[2]. Most of the other digits are based on the time while generating the GUID. V1 GUIDs which contain a MAC address and time can be identified by the digit "1" in the first position of the third group of digits, for example {2f1e4fc0-81fd-11da-9156-00036a0f876a}. V4 GUIDs use the later algorithm, which is a pseudo-random number. These have a "4" in the same position, for example {38a52be4-9352-453e-af97-5c3b448652f0}. More specifically, the 'data3' bit pattern would be 0001xxxxxxxxxxxx in the first case, and 0100xxxxxxxxxxxx in the second. Cryptanalysis of the WinAPI GUID generator shows that, since the sequence of V4 GUIDs is pseudo-random; given full knowledge of the internal state, it is possible to predict previous and subsequent values

like image 69
SQLMenace Avatar answered Sep 24 '22 06:09

SQLMenace