Why integers make good primary keys
Most often it is easier to use an integer for indexing compared to a string or compound key, since it is well suited for processing results (conceptually or in practice) as an array. Depending on the database implementation, integers can also be faster for access, sorting, or comparison, and an integer type usually offers additional functions, such as auto-increment, that are not available for other data types. How could you make an auto-increment of a composite key, for example?
MySQL has to say about the primary key:
The primary key for the table is the column or set of columns that you use in your most important queries. It has a linked index for fast query performance. Request performance depends on NOT NULL optimization, as it cannot contain NULL values.
SQL allows you to use any non-zero, unique column (or set of columns) 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 expectations
Although this is not a strict requirement, some frameworks optimize for whole primary keys. For example, Ruby on Rails simplifies the use of a primary key with automatic addition by default; you must consciously work against the convention if you want to use something else.
This does not mean that integers should or should not be used as primary keys. It just means that the choice of the primary key depends partly on your underlying database system and the queries you expect from it, and partly on the applications you expect to use to retrieve the data. All of these things should be considered when considering potential keys.
Todd A. Jacobs
source share