GusDeCooL
GusDeCooL

Reputation: 5761

Must database primary keys be integers?

I always see MySQL database primary keys as integers. Is that because primary keys must be integers, or because of ease of use when setting auto_increment on the column?

I am wondering just in case I want my primary key to be a varchar in the future.

Upvotes: 16

Views: 26668

Answers (6)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84453

Why Integers Make Good Primary Keys

It's often easier to use an integer for indexing, in comparison to a string or composite key, because it lends itself well to treating results (conceptually or in practice) as an array. Depending on the database implementation, integers may also be faster to access, sort, or compare, and the integer type usually offers additional features like auto-incrementing that aren't available for other data types. How would you go about auto-incrementing a composite key, for example?

MySQL has this to say about the primary key:

The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.

SQL allows any non-null, unique column (or set of columns) to be used as a primary key. However, if you don't care about auto-incrementing, you can usually make your primary key any index that is UNIQUE and NOT NULL.

Consider Your Application's Expectations

While not a hard requirement, some frameworks optimize for integer primary keys. For example, Ruby on Rails facilitates the use of an auto-incrementing primary key by default; you have to deliberately work against the convention if you want to use something different.

That doesn't mean one should or shouldn't use integers as primary keys. It just means the choice of primary key is driven in part by your underlying database system and the queries you expect to run against it, and in part by the applications you expect to use to retrieve the data. All of those things should be taken into consideration when considering candidate keys.

Upvotes: 10

Xnoise
Xnoise

Reputation: 532

Basicly a primary key needs to fulfill only 2 conditions: it has to be a not null column and it has to be unique. Any typeof column that respects this 2 conditions can be set as primary keys. In case the primary key is a multiple column one, then both columns need to be not null.

While in theory you can use other fields as primary keys, integers are the easiest to manage, as well as being the fastest indexes available.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382909

You can use varchar as well as long as you make sure that each one is unique. This however isn't ideal (see article link below for more info).

What you are looking for is called natural key but a primary key with auto-increment and handled by the RDBMS is called surrogate key which is preferred way. Therefore you need to have it to be integer.

Learn more:

Upvotes: 20

Sashi Kant
Sashi Kant

Reputation: 13465

For a primary key to be an Integer, is easier to manage, and makes its index more effective. As you know, while the keys are auto indexed, the indexes are stored as Binary tree which is best for integers in traversing. There is no restriction on making a key to be int, you can declare it a varchar too.

Upvotes: 1

Melanie
Melanie

Reputation: 3111

No, the primary key does not have to be an integer; it's just very common that it is. As an example, we have User ID's here that can have leading zeroes and so must be stored in a varchar field. That field is used as a primary key in our Employee table.

Upvotes: 1

Salih Erikci
Salih Erikci

Reputation: 5087

A primary key is unique.int is easy to satisfy that condition with auto increment.If you make it char you have to create a way to make it unique whenever you add a data.

Upvotes: 1

Related Questions