I have a user_info
table in five different locations. Now I need to integrate all the rows into a central user_info
table. To do this I need a unique id for each row in source tables. Because when all the rows come into the central table, then each user must have a unique ID.
Now my questions are:
if I use uniqueidentifier
NEWID()
for each source table then is it will be unique for globally & life time or have any chance to be duplicate?
How does SQL Server create the NEWID()
? I need to know the key generation structure.
Yes, there are a number of ways you can auto-generate key values for your tables. The most common ways are via the use of the IDENTITY column property or by specifying a uniqueidentifier (GUID) data type along with defaulting with either the NEWID() or NEWSEQUENTIALID() function.
-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.
Yes, there is no chance of a duplicate between machines.
NEWID()
is based on a combination of a pseudo random number (from the clock) and the MAC address of the primary NIC.
However, inserting random numbers like this as the clustered key on a table is terrible for performance. You should consider either NEWSEQUENTIALID()
or a COMB-type function for generating GUIDs that still offer the collision-avoidance benefits of NEWID()
while still maintaining acceptable INSERT
performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With