liveBook · Manning

Candidate key

As explained in Grokking Relational Database Design

A minimal combination of table attributes whose values uniquely identify a row, with no removable attribute retaining that property.

A candidate key is the smallest set of one or more table attributes that uniquely identifies a row. “Smallest” matters: if any attribute can be removed and the remaining attributes still identify every row, the original combination is not a candidate key. Candidate keys give a database designer the options from which one primary key is selected. section 4.3 section 6.2

A candidate key is a minimal superkey. A superkey can uniquely identify a row but may include extra columns; a candidate key has no unnecessary columns. One candidate key is chosen as the table’s primary key, which uniquely identifies rows, helps prevent duplicate data, and supports relationships between tables. section 4.3 section 6.2

To check a possible candidate key, work through these questions:

  • Can its value, or combination of values, identify one row and no other row? If not, it is not a superkey. section 6.2
  • If it identifies a row, can any included attribute be removed while keeping that uniqueness? If yes, it has unnecessary columns and is not a candidate key. section 4.3 section 6.2
  • Compare the available candidate keys and select the best fit as the primary key; if none is suitable, create a new attribute for that role. section 4.3

Testing keys in a user table

Suppose username, email, and phone_number must each be unique for users. Each attribute can identify a user row by itself, so each is a candidate key. The pair username and first_name can also identify a row, but it is not a candidate key: username alone already does the job, making first_name removable. section 4.3

Use the diagram to distinguish the nested categories: superkeys may contain extras, candidate keys are minimal, and one candidate key becomes the primary key. section 6.2

Candidate keys also matter during normalization. They clarify which columns identify a row before selecting a primary key and evaluating table design against normal forms such as BCNF. section 6.2

How Grokking Relational Database Design covers it

How deep it goes
Defines minimal superkeys, contrasts them with superkeys and primary keys, and demonstrates minimality with a user-table combination.
Examples
Uses unique username, email, and phone_number values, plus the nonminimal username and first_name combination.
What it assumes
Introduces candidate keys through primary-key identification and row uniqueness without requiring prior key terminology.
What you can do after
Evaluate/understand whether attributes uniquely identify rows without unnecessary columns and choose a primary-key candidate.
How it connects
Connects candidate keys with primary keys, superkeys, data integrity, normalization, and BCNF.

Also explained in

Compare how 2 books explain candidate key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Relational Database Design. The section links point to the parts of the book it draws on.