rjmcb
rjmcb

Reputation: 3745

INT vs VARCHAR datatype for primary keys

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?

Upvotes: 5

Views: 8328

Answers (3)

Sanjay Goswami
Sanjay Goswami

Reputation: 1386

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

Upvotes: 5

huanyang
huanyang

Reputation: 11

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.

Upvotes: 1

Sam Dufel
Sam Dufel

Reputation: 17608

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.

Upvotes: 3

Related Questions