Click here to Skip to main content
15,885,546 members
Articles / Database Development / SQL Server
Tip/Trick

Using ROWGUIDCOL in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
8 Nov 2010CPOL 55.7K   11  
Using ROWGUIDCOL in SQL Server

Introduction


This artcile explains the use of ROWGUIDCOL in SQL Server.


Background


Normally, when we use UNIQUEIDENTIFIER column for PRIMARY KEY, we assign NEWID() as the DEFAULT value for the column. SQL Server offers a more efficient and convenient way of achieving it.



Using the Code


For example, this is what we would normally use:


//
// Using UNIQUEIDENTIFIER with NEWID()
//
CREATE TABLE MyTable (ID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID()...

Instead of the above, we can use the below definition:


//
// Using UNIQUEIDENTIFIER with ROWGUIDCOL and NEWSEQUENTIALID()
//
        
CREATE TABLE MyTable (ID UNIQUEIDENTIFIER ROWGUIDCOL PRIMARY KEY DEFAULT NEWSEQUENTIALID()...


Points of Interest


We have the below advantages with this approach:


1) The UNIQUEIDENTIFIER along with ROWGUIDCOL NEWSEQUENTIALID() is far more efficient than normal UNIQUEIDENTIFIER along with NEWID().

2) The Unique Ids generated with the second approach are sequential in nature, similar to IDENTITY values.

3) There can be max of one ROWGUIDCOL for a table.

4) You can query the table for ROWGUIDCOL. Example: SELECT ROWGUIDCOL FROM MyTable

History


Keep looking for updates, please rate this tip/trick.

License

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


Written By
India India
Ravikiran Malladi is a senior software professional in Microsoft technologies. He has global IT experience of over 10 years and has compulsive habit of writing tools, particularly for the projects he works on. He has vast experience in the roles of senior developer, technical analyst, technical architect besides in technical lead roles.

His hobbies include chess, music, cricket, web sites along side writing technical articles and technical blogs.

Comments and Discussions

 
-- There are no messages in this forum --