Primary and foreign keys

The previous section chose keys by reading rows and checking types. Most tables make the choice for you, because they are designed around 2 kinds of key column. Learn to spot them and a schema tells you how its tables join.

Primary key: one value, one row

A primary key is a column whose value identifies exactly one row of its table. That takes 2 promises: no value repeats, and no value is NULL. In customers, the primary key is customer_id. There is one row with customer_id 1, and it is Alice's. 2 shoppers can share the name Alice, which is why name could never do this job.

Foreign key: a pointer to another table

A foreign key is a column that stores primary key values from another table. orders.customer_id is a foreign key: each value in it points at one row of customers. A foreign key is free to repeat. Orders 101 and 102 both hold customer_id 1, because Alice ordered twice.

Schema
  • customers: customer_id INTEGER PK, name VARCHAR
  • orders: order_id INTEGER PK, customer_id INTEGER FK, total INTEGER
  • orders.customer_id references customers.customer_id (N:1)

The line in the diagram runs from the foreign key to the primary key it points at, and that line is the join. The test compares the column at one end with the column at the other: orders.customer_id = customers.customer_id. A foreign key and its primary key already agree on meaning and type, so 2 of the 3 questions from the last section answer themselves. The N:1 label says many orders can point at one customer, which the next section turns into row counts.

Primary key
  • Identifies a row of its own table
  • Every value appears once
  • Never NULL
  • One per table
Foreign key
  • Points at a row of another table
  • Values may repeat
  • Each value should exist as a primary key value in the other table
  • A table can hold several

A foreign key value that exists nowhere in the other table's primary key is called an orphan. An order carrying customer_id 9 would be one, because no customer has that value. Orphans usually mean a row was deleted or loaded late, and the last section shows what a join does with them.

Finding keys when nobody drew the diagram

Many tables arrive with no diagram. Names help: a column called order_id in a table called orders is probably its primary key, and an _id column named after a different table is probably a foreign key. You can test the guess with 2 counts you already know. If COUNT(*) and COUNT(DISTINCT column) return the same number, no value repeats.

orders
order_idcustomer_idtotal
101150
102130
103275
104320

order_id

Tests whether order_id could be the primary key of orders.

SELECT
  COUNT(*) AS row_count,
  COUNT(DISTINCT order_id) AS distinct_values
FROM orders
Result
row_countdistinct_values
44

customer_id

Runs the same test on customer_id.

SELECT
  COUNT(*) AS row_count,
  COUNT(DISTINCT customer_id) AS distinct_values
FROM orders
Result
row_countdistinct_values
43

order_id gives 4 rows and 4 distinct values, so it can identify a row. customer_id gives 4 rows and 3 distinct values. A value repeats, so it cannot be the primary key of orders, and it behaves the way a foreign key does. To test the second promise, compare COUNT(*) with COUNT(column): equal numbers mean the column holds no NULL.

One table, several relationships

A table has one primary key, and it can hold several foreign keys. order_items points at 2 tables at once: each line belongs to an order and names a product.

Schema
  • customers: customer_id INTEGER PK, name VARCHAR
  • orders: order_id INTEGER PK, customer_id INTEGER FK, total INTEGER
  • order_items: item_id INTEGER PK, order_id INTEGER FK, product_id INTEGER FK, quantity INTEGER
  • products: product_id INTEGER PK, name VARCHAR, price INTEGER
  • orders.customer_id references customers.customer_id (N:1)
  • order_items.order_id references orders.order_id (N:1)
  • order_items.product_id references products.product_id (N:1)

Each line is one relationship and one possible join. To put a product's price beside an order line, follow the line from order_items.product_id to products.product_id. No line connects customers to products, and they share no key, so no single test relates those 2 tables.

A payments table has 1,000 rows. COUNT(DISTINCT payment_id) returns 1,000. COUNT(order_id) returns 1,000 and COUNT(DISTINCT order_id) returns 940. What do these counts tell you?

Find each primary key and the foreign keys that point at it, and you have the list of sensible joins: one for each line in the diagram.

About this lesson

This section is part of the Joins: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.