What Is the Purpose of a Primary Key in a Database?

David/Taxi/Getty Images

Primary keys are used to uniquely identify a row in a database table; no two rows can have the same primary key. Identification numbers are often used as primary keys. Any field that is not necessarily unique, such as a person’s name, is not suitable for being a primary key.

Primary keys are always unique keys; if a user or program tries to enter a duplicate primary key in a new row, the database does not accept the new row. Primary keys provide ways for database administrators and programmers to reference a particular row without regard to the other information in that row. Other tables in the database may reference the primary key; when it is referenced from a different table, it is known as a foreign key.

In most cases, an automatically incrementing number is used as the primary key. Every time a new row is entered, a primary key is generated for it.

In the United States, people are often identified by their Social Security number, because it is guaranteed to be unique. Other nations often rely on similar techniques. While this has some security disadvantages, it provides a means of reliably identifying people, and it ensures that records do not mistakenly identify people with the same name, address or phone number.