liveBook · Manning

Candidate key

As explained in 100 SQL Server Mistakes and How to Avoid Them

A candidate key is a minimal set of attributes that uniquely identifies every row in a relation.

A candidate key is the smallest set of columns that uniquely identifies each row. The common mistake is choosing a column that looks descriptive without checking whether duplicates exist. A super key identifies every row; it becomes a candidate key only when removing any column breaks that uniqueness. section 4.1.3

Check a proposed key before relying on it:

  • Confirm that its values distinguish every row. section 4.1.3
  • Remove each column in turn; if uniqueness remains, the original set was not minimal. section 4.1.3
  • Use one column when it suffices; otherwise use a composite key. section 4.1.3

Product names need a second attribute

Sales Order Number and Purchase Order Number each identify their respective rows alone. Product Name does not: supplier parts can share names with products sold to customers. Product Name plus Product Type Name therefore forms the candidate key. section 4.1.3

Sales
Sales Order Date
Sales Order Number
Sales Person Name
Sales Person Email
Sales Area Name
Sales Area Manager
Customer Company Name
Customer Contact Name
Customer Contact Email
Customer Invoice Address
Customer Delivery Addresses
Sales Order Delivery Due Date
Sales Order Delivery Actual Date
Sales Order Item
Sales Order Quantities
Currier Used for Delivery
 
Purchasing
Supplier Name
Supplier Contact Name
Supplier Contact Email
Supplier Address
Purchase Order Date
Purchase Order Number
Purchase Order Items
Purchase Order Quantities
 
Products
Product Name
Product Stock Level
Product Next Manufacture Date
Product Next Manufacture Quantity
Back Order Manufacturing ID
Product Type Name
Product Type Description
Product Category Name
Product Category Description
Product Subcategory Name
Product Subcategory Description

Inspect the original Sales, Purchasing, and Products attributes to see why Product Name alone is unsafe and why Product Type Name is needed. section 4.1.3

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Defines super keys, minimality, single-attribute keys, and composite keys through relation examples.
Examples
Contrasts Sales Order Number, Purchase Order Number, and the Product Name plus Product Type Name pair.
What it assumes
Introduces super keys and composite keys while treating a relation as an entity or table.
What you can do after
Evaluate/understand whether one attribute or a minimal attribute combination uniquely identifies relation rows.
How it connects
Connects candidate keys with first normal form, super keys, composite keys, and prime attributes.

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 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.