Question

I have to define an integer primary key for a table that I am trying to build for a web app. I can define it as a normal integer or make it autoIncrement. Both are OK for my table and I know that autoIncrement saves me the trouble of explicitly setting the value.

But, are there any advantages other than this? Or are there any disadvantages of using normal integer? For some reason I am inclined to use normal integer as the values can be random and hard to predict (being for a webapp). But I am worried if this causes performance issues while querying the table.

PS: This question is not about integer vs GUIDs. Just AutoIncrement vs Normal Int.

Was it helpful?

Solution

If you use a normal integer key, you assume the burden of generating unique keys. If there is more than one thread/process adding records to the table, this is non-trivial.

Another consideration is performance: the database engine can generate auto-incremented keys much faster than user-level code can come up with unique keys.

The upside is that you could make the keys hard(er) to predict. Whether this actually buys you anything is open to debate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top