5

Which out of the INTEGER and VARCHAR datatypes is better for use as primary keys and why? I am used to making my primary keys INTEGER, will using VARCAHRs have a performance penalty?

Chris
  • 44,602
  • 16
  • 137
  • 156
rjmcb
  • 3,595
  • 9
  • 32
  • 46

3 Answers3

5

INT is faster for clustor index and if we want to join with other table.

You will get idea if you have understanding of Clustor Index and JOIN

Sanjay Goswami
  • 1,386
  • 6
  • 13
3

Integers:

  • Takes less room to store them
  • You can set up an auto-increment to automatically get a unique id

Varchar:

  • You can store non-numeric data

Speed-wise, they're almost identical.

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
1

It depends on your business need. primary key is also a column, so the type really depends on the column. For example, consider a "student" table. student_id is the primary key. If you use integer to represent the id, the primary column should be defined as INT. Or if you use something like "G12345" (G indicates graduate student), then you should use varchar.

huanyang
  • 11
  • 1