-3

which datatype should be usually preferred ?

We mostly used integer as datatype of primary key why ?

Pankaj katiyar
  • 464
  • 10
  • 26
  • I am voting to close because you are asking for an opinion. I would suggest that you ask another question, with two table structure, one with the strings and another with integers. Then ask a specific question, such as "which will have better performance", "which will have more maintainable queries", or something specific. – Gordon Linoff Jul 17 '15 at 12:42
  • 1
    possible duplicate of [Is there a REAL performance difference between INT and VARCHAR primary keys?](http://stackoverflow.com/questions/332300/is-there-a-real-performance-difference-between-int-and-varchar-primary-keys) – user2314737 Jul 17 '15 at 12:43
  • 1
    _"primarily opinion-based Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise."_ – Nickz2 Jul 17 '15 at 12:46

2 Answers2

1

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.

FreudianSlip
  • 2,870
  • 25
  • 24
0

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.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279