which datatype should be usually preferred ?
We mostly used integer as datatype of primary key why ?
which datatype should be usually preferred ?
We mostly used integer as datatype of primary key why ?
Integers are usually smaller, easier to work with, have a natural sequence and don't suffer from charset conversions. I'm sure there are other reasons too.
Well, a native sized integer is probably the most optimized data type in any environment. So things like comparing and calculating and sorting are going to be fast.
Integers are also inherently sequential. So generating a new one is easy. Other data types don't provide that.
In a more semantic sense, it's also a matter of separating different data elements which represent different things. In most cases a primary key is an identifier. And it should do just that, identify the record. Nothing more. Trying to merge that with other data can lead to problems.
For example, if you have a Person
table and want to use the person's name as the primary key. That's going to cause problems when two people have the same name, or when a person's name changes, or when a person has multiple names, etc.
You can require that other values be unique, can place constraints on data elements, etc. But as an identifier all you want it to do is uniquely identify the record. And, depending on the needs of the system, an integer or GUID are perfectly capable of doing exactly that.