Blog

Filter posts by Category Or Tag of the Blog section!

Which one should I use as a primary key, GUID or int ?

Wednesday, 19 December 2012

About choosing integer or GUID as a primary key, Well both implementations have their advantages and disadvantages. It' completely dependent on your database design, migration needs, and overall architecture. let's discuss...

When to use GUID?

You can use GUID as primary key if your application's database is too complex and you use replication and distributed tables or you want to merge tables of your database. GUID is unique in the world! and it generates randomly (not by mac address of the network like in the past), so it seems good if you want to move data from your database to another. GUID can be used to create primary key before inserting the value to the database(it is so interesting). 

When Not to Use GUID?

But working with GUID is a little bit hard because the value generates by random and cannot accept any meaningful pattern. There is another issue with GUID that it is 4 times larger than INT so GUID is slower than int and it makes a performance problem. it's overall hard to work, hard to remember, and also appearing GUID in web URL doesn't make any good sense especially when CEO is important to your application.

When using Integer?

If your database is simple and small, it’s better to use the integer as your tables primary key, the integer is not big in a small database, anyway, you can use bigInt instead if your data is big. integer is easy to remember, easy to work and readable.

When not to use integer?

When you want to merge tables of your database, even if your database is small or simple DON’T use the integer as primary key, using integer would be also hard to work if your database contains distributed tables.

Category: Software

Tags: C#

comments powered by Disqus