Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using ROWGUIDCOL in SQL Server

, 8 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

mravikiran

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 --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 8 Nov 2010
Article Copyright 2010 by mravikiran
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid