Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL primary key, INT or GUID or..? [duplicate]

Is there any reason why I should not use an Integer as primary key for my tables?

Database is SQL-CE, two main tables of approx 50,000 entries per year, and a few minor tables. Only two connections will exist constantly open to the database. But updates will be triggered through multiple TCP socket connections, so it will be many cross threads that access and use the same database-connection. Although activity is very low, so simultanous updates are quite unlikely, but may occur maybe a couple of times per day max.

Will probably use LINQ2SQL for DAL, or typed datasets.

Not sure if this info is relevant, but that's why I'm asking, since I don't know :)

like image 622
bretddog Avatar asked Dec 29 '10 20:12

bretddog


2 Answers

You should use an integer - it is smaller, meaning less memory, less IO (disk and network), less work to join on.

The database should handle the concurrency issues, regardless of the type of PK.

like image 125
Oded Avatar answered Oct 31 '22 03:10

Oded


The advantage of using GUID primkey is that it should be unique in the world, such as whether to move data from one database to another. So you know that the row is unique.

But if we are talking about a small db, so I prefer integer.

Edit:

If you using SQL Server 2005++, can you also use NEWSEQUENTIALID(), this generates a GUID based on the row above.Allows the index problem with newid() is not there anymore.

like image 34
eriksv88 Avatar answered Oct 31 '22 02:10

eriksv88