Click here to Skip to main content
15,890,185 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Generate auto row number for a table

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Sep 2011CPOL 11.7K   1   3
If you have the ability to change it, you may want to consider moving away from the 36 digit alphanumeric created by GUID() as a Key. This is often misused in SQL-Server by developers who have not had proper training/education in SQL Server and or RDBMS. The GUID() is not intended to be a...

If you have the ability to change it, you may want to consider moving away from the 36 digit alphanumeric created by GUID() as a Key. This is often misused in SQL-Server by developers who have not had proper training/education in SQL Server and or RDBMS. The GUID() is not intended to be a Primary Key but a way to generate a unique value when working across multiple systems. Most databases are not setup like this, they are self-contained. Even if they are using data from outside sources, they still are self-contained in that all their tables are in a single DB on a single SQL Server instance. While using the GUID() will create a unique value, it carries overhead with it that makes it a non-optimal choice for a Primary Key when you don’t need to create a unique value that is unique across all databases on all your systems.


As for how to generate a unique key, if you are using SQL Server as your tag as your post indicates, you can let SQL Server do this for you by setting the column of the table that is your primary key to auto-increment a unique value.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: 1. I said an identity int field is a great PK, not a guid. 2... Pin
KP Lee3-Oct-11 22:34
KP Lee3-Oct-11 22:34 
GeneralI beg to differ. Uniqueidentifier was originally DESIGNED to... Pin
KP Lee30-Sep-11 17:55
KP Lee30-Sep-11 17:55 
GeneralRe: No offense intended but your wrong about this being a great ... Pin
YSLGuru3-Oct-11 7:58
YSLGuru3-Oct-11 7:58 
No offense intended but your wrong about this being a great PK. Here is a post by Microsoft employee Umachandar Jayachandran (works on the SQL Team) on the subject where he details why you shouldn’t consider a GUID for this kind of scenario. Thats not to say you would never use a GUID but that you need to understand the pros and cons of using it so you know when to and when not to use it and in the scenario the poster describes the GUID is NOT the better choice.

For the record – I manage databases with row counts in the hundreds of millions of rows so I have a little bit of experience working with keys and I have yet to see where we’d be better off with a 36 digit alphanumeric key then a standard incrementing numeric only key.

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/e77fef16-a697-4bf9-affd-8189957a4d63/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.