Reputation: 32758
My SQL Server 2012 table has the following column definition:
[GlobalId] UNIQUEIDENTIFIER DEFAULT (newid()) NULL,
Is there any advantage in this being defined as a UNIQUEIDENTIFIER or could I just define it as a string. The reason I ask is because at a later time I might want to not store the full value of the GUID and I might want to generate my own Id.
Now it's stored as a UNIQUEIDENTIFIER can I change the definition to a string and still have the DEFAULT work?
Here's an example of what I have stored in the GlobalId column:
6ffcac5e-88f1-4207-bbbe-0fc14265c01f
Upvotes: 2
Views: 632
Reputation: 69
I am not sure of the question you are asking. But one seems to be how to convert a uniqueidentifer to a string. To convert a uniqueidenitifier to a string convert(char(36), newid()) will return a string.
Upvotes: 1
Reputation: 14599
As said in the link provided by valex,
A GUID is a unique binary number; no other computer in the world will generate a duplicate of that GUID value. The main use for a GUID is for assigning an identifier that must be unique in a network that has many computers at many sites.
So the interest is this famous unique fact.
If you want string values, use string values, but the built-in functions in sql server allowing the generation of new values is NEWID and NEWSEQUENTIALID, which gives UNIQUEIDENTIFIERs.
You are not obliged to use UNIQUEIDENTIFIER for identity columns, it will depend on your datamodel and servers: if only one database server generates the keys, a sequence ID is fine, and is somewhat more efficient (and a heck of a lot easier to read) than a GUID. Iif multiple servers generate the keys then go with a GUID
Upvotes: 3