Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is uniqueidentifier datatype ? can we use without newid() ? i m confused what is actual use of uniqueidentifier
Posted
Comments
[no name] 26-Apr-13 8:33am    
And what is stopping you from reading the documentation?

A UniqueIdentifier holds a GUID - a Globally Unique IDentification. This is basically a 128 bit number which is randomly generated on the basis that the chances of your ever meeting two identical values in your lifetime are lower than that of you winning the lottery.

Every week.

For the rest of your life.

And it's a good bet - the odds of two identical GUIDS is vanishingly small, and the "address space" of 128 bits is enormously large.

What do you use them for? Row IDs. One of the advantages of using GUID row ids over simple integers is that you can generate new rows on two disconnected systems, and you still won't have identical values when you connect them together and consolidate your tables - which is a true PITA to manage with integers.

Can you use them without newid()? Yes - the GUID can be assigned in the software inserting rows to the SQL table and they still won't clash!
C#
Guid g = Guid.NewGuid();
Will do it in C# for example.
 
Share this answer
 
Uniqueidentifier is sql-servers datatype for storing Guid's. A Guid is globally uniqe and is very hard to guess. That means if you geneerate a uniqueidentifier using newid(), that value only exists in your application/database. A uniqueidentifier has different parts representing time, mac-address, cpuId and more (depending on version) to be sure it's globally unique.
So it's pretty common to use it as a part of a "secret" url at password recovery etc.

You don't have to use newid() to write values to a column with the datatype uniqueidentifier. You can generate a guid in your code (C#: Guid.NewGuid()) and store that value to your database.

So how uniqu is a Uniqueidentifier/Guid. Well it has 2^128 possible values. That means if you fill the whole planet with sand and one grit is a value. Then you will need 22 planets to use all the unique values..

MSDN : Uniqueidentifier (Transact-SQL)[^]
MSDN : Using uniqueidentifier Data[^]
Wikipedia: Globally unique identifier[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900