Ask the Expert

Integer or character primary key

I have a table with a primary key (PK), which was naturally defined as an integer number from my business model. Do I have to keep this PK as an integer or could I use a char/varchar data type? Considering that the values for PK will not be greater than 500000, using varchar(6) could save some space. Does it affect indexing or querying?

    Requires Free Membership to View

It's not terribly clear what "naturally defined" means, but if it's an existing number scheme already in use, you should definitely use it rather than some arbitrary autonumber or identity column. You are probably not referring to the whole "natural versus surrogate" debate anyway.

As for whether to use INTEGER or VARCHAR, use INTEGER. Integer operations (indexing, sorting, comparing) are, in general, faster than character. And an INTEGER column requires only 4 bytes, even for numbers as large as 2 billion, so you may save some space (if a good portion of the numbers actually require more than 4 digits).

Finally, there is the issue of sorting, which becomes clear when you sort VARCHAR values and get a result like this:


This is obviously not very useful if in fact you wanted numerical order.

This was first published in August 2004

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: