Click here to Skip to main content
6,629,377 members and growing! (18,264 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate License: The Code Project Open License (CPOL)

INT versus GUID on a table's primary key

By Liviu Holhos

This article tries to show the pros and cons of using INT or GUID types on primary keys.
SQL, C# 1.0, C# 2.0, C# 3.0, VB 7.x, VB 8.0, VB 9.0, DBA, Dev
Posted:6 May 2008
Views:14,727
Bookmarked:9 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 2.88 Rating: 2.34 out of 5
3 votes, 17.6%
1
8 votes, 47.1%
2
4 votes, 23.5%
3
1 vote, 5.9%
4
1 vote, 5.9%
5

Introduction

This article shows the pros and cons of using the INT or GUID types on primary keys. Hope it helps you decide which suites your project best.

Using INT on primary keys

Advantages
  • Small amount of storage size (and integer is 4 bytes).
  • Increased readability, practical use in testing, and easy to remember.
  • Chronology of data; if two records are in ascending order, we can deduce that the second record was inserted after the first one.
  • Support for functions that return the last primary key generated (@@IDENTITY, SCOPE_IDENTITY()).
Disadvantages
  • Difficulty in the case of merging tables (the need to make remapping because the primary keys may be duplicated).
  • Hard to work with distributed tables.
  • Primary key in the form of INT/BIGINT is LUID, local unique identifier, which is only used locally in a table.
  • After a large number of operations (insert, delete), the counter for primary key can be reset, bringing the problem of chronology to 1.

Using GUID on primary keys

Advantages
  • Easy to merge tables.
  • Easy to work with distributed tables.
  • The primary key is uniquely identified in the entire system (the number of machines or tables doesn't matter).
  • Don't have problems when inserting a large number of operations.
Disadvantages
  • Bigger storage size (16 bytes - four times the size of an integer).
  • Don't have the chronology assumption.
  • Hard to remember and use in testing.
  • Don't benefit from a mechanism to obtain the last generated primary key (MS SQL Server).

My decision

I had a hard time deciding which solution suites best for my project, but I finally used GUIDs because of the advantages mentioned. But this happened only once, the rest of my projects have databases with INT primary keys. Hope someone finds this useful. Special thanks to Iancu Caputa for helping with this article.

License

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

About the Author

Liviu Holhos


Member
Living in Cluj-Napoca, a beautiful city on Somesul Mic river. Programming since 2001 with almost every language out there. Interested especially in user interfaces and arhitectual designs. Can find me on SourceForge http://sourceforge.net/users/jayliviu with two active projects. Personal website: http://liviuholhos.com/index.php
Occupation: Software Developer
Location: Romania Romania

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generalok but PinmemberDonsw16:30 14 Jun '09  
GeneralComposite PK on two integer fields? Pinmemberpashaper23:22 11 Nov '08  
GeneralGUIDs fragmentation PinmemberGeyzerskiy Dmitriy2:45 8 May '08  
GeneralRe: GUIDs fragmentation [modified] PinmemberLiviu Holhos21:42 8 May '08  
GeneralRe: GUIDs fragmentation PinmemberMike Lang3:54 13 May '08  
GeneralRe: GUIDs fragmentation PinmemberLiviu Holhos21:05 13 May '08  
GeneralRe: GUIDs fragmentation PinmemberGeyzerskiy Dmitriy23:48 13 May '08  
QuestionWhat about Performance PinmemberDoug K. Wilson18:07 7 May '08  
AnswerRe: What about Performance PinmemberLiviu Holhos20:46 7 May '08  
GeneralRe: What about Performance Pinmemberastanton19782:50 20 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 May 2008
Editor: Smitha Vijayan
Copyright 2008 by Liviu Holhos
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project