Primary key
A primary identifier (PK) is the column, or combination of columns, whose value identifies each row of a database table uniquely and is never empty. Other tables refer to a row through its PK value.
What it is
A primary identifier, abbreviated PK, is the column or set of columns that identifies each row of a database table uniquely. Its value is different for every row and is never missing. A table has at most one PK, which may be built from one column or from several columns together.
ISO/IEC 9075 (SQL) specifies how the integrity constraints of tables are defined. A PK is declared as such a constraint, so the database itself refuses a duplicate or empty value.
A PK can be:
- A natural identifier, a value that already exists in the business, such as an invoice number.
- A surrogate identifier, a number generated by the database with no business meaning.
- A composite identifier, made of several columns, such as an account number with a date.
Other tables refer to a row by storing its PK value in a column called a foreign identifier (FK). A NIST data glossary shows this pattern: a study identifier column in one table is an FK that matches the PK of the study table.
Why it matters
Joins rely on identifiers. If the PK is unique, a join returns one matching row per reference. If it is not, the join repeats rows and totals are inflated.
A PK also prevents duplicate records, so one customer or one trade is stored once.
Worked example
Illustrative numbers. An invoices table has the invoice number as its PK. It holds 1,000 rows with numbers 1 to 1,000.
An attempt to insert a second row with number 500 is rejected because the value already exists. An attempt to insert a row with no invoice number is also rejected.
A payments table stores the invoice number in an FK column. A payment that carries invoice number 500 therefore points to exactly one invoice row.
Who uses it
- Data analysts join tables on PK and FK columns and check that the PK is unique before summing.
- Master data specialists maintain unique identifiers for customers, products and accounts.
- Business intelligence analysts choose the PK when they model reporting tables.
- Regulatory reporting analysts use the PK to trace a filed figure to one source record.
Common confusions
- A PK is not the same as an FK. The PK identifies a row in its own table; the FK refers to a row elsewhere.
- A name or a date is rarely a safe PK, because values can repeat or change.
- A unique column is not automatically the PK. A table can have several unique columns but only one PK.
- A PK does not prove that the record is correct. It only guarantees that the row is identifiable.