Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating GUID

I am thinking to use a GUID in my .net app which uses SQL Server. Should I be writing a stored procedure which generates the GUID on each record entered or should I be directly generating it from the application.

Reasons for asking the question (If am wrong correct me in this):

I (as/pre)sume:

When generating the GUID from the database, you can assume that the DB remembers the previous generated GUID where as the application remembering it is difficult.

like image 882
Chaitanya Avatar asked Jul 05 '10 09:07

Chaitanya


People also ask

How is a GUID generated?

A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.

How do I create a GUID file?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.

What is a GUID example?

A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example GUID shows the groupings of hexadecimal digits in a GUID: 6B29FC40-CA47-1067-B31D-00DD010662DA.

How many GUIDs can be generated?

Not guaranteed, since there are several ways of generating one. However, you can try to calculate the chance of creating two GUIDs that are identical and you get the idea: a GUID has 128 bits, hence, there are 2128 distinct GUIDs – much more than there are stars in the known universe.


1 Answers

SQL Server has the creation of GUID's built in. There is no need to write a separate stored procedure for this.

You can use

  • NEWID()
  • NEWSEQUENTIALID()

The key difference between both procedures would be that the sequential GUID should be used if it is for a primary clustered key.

I'm not sure why you would want the database engine to remember the previous generated GUID.

like image 179
Lieven Keersmaekers Avatar answered Sep 22 '22 16:09

Lieven Keersmaekers