Primary key and foreign key

A primary key uniquely identifies each row in a relational table; a foreign key is a column whose value refers to the primary key of a row in another table, linking the two.

In the relational model introduced by E. F. Codd in 1970, a primary key is a column, or a set of columns, whose value uniquely identifies each row of a table. Of the candidate keys that could do this, one is chosen. A foreign key is a column in one table whose values refer to a key in another table, or occasionally the same one. An Orders table might hold a CustomerID column that points to the CustomerID primary key of a Customers table.

Each key has an integrity rule attached. Entity integrity requires every primary key value to be unique and not null. Referential integrity requires every foreign key value to match an existing key or be null, so no row points at a record that is not there. Keys also matter in multilevel databases: if an insert is rejected because its key already exists at a higher classification, the rejection itself can reveal the hidden record. Polyinstantiation closes that inference channel by letting the classification level form part of the key.

Exam relevance: candidates are expected to link the primary key to entity integrity and the foreign key to referential integrity. A scenario in which a duplicate-key error discloses a classified record is likely to point to polyinstantiation as the control.