Stop 01 · SQL & Database FoundationsTopics · 4/10

Primary Keys and Foreign Keys

Part of the SQL Roadmap

In short

Two customers can both be called Rahul Sharma from Delhi. Keys are how a database tells rows apart, and how it links an order back to exactly the right customer.

A primary key uniquely identifies each row in a table, and a foreign key is a column that points to a primary key in another table, which is how tables relate.

The jaadu carries a numbered brass key to a matching numbered door in a long hostel corridor.

Think of it like this

Hostel rooms and mess slips

Every hostel room has a unique number, like A-204. That is the primary key: no two rooms share it. A mess slip says "Room A-204" to show who ate. That reference is a foreign key, and the warden won't accept a slip for a room that doesn't exist.

Watch it happen

The database guards the link

App
orders table
customers table

INSERT order for customer 7

1/5The app tries to save an order with customer_id = 7.

The big ideas

What you need to know

Primary key is unique

Each table gets one primary key. Its value is unique and never NULL, so it points to exactly one row.

Foreign key points to a parent

orders.customer_id refers to customers.customer_id. That shared value is how two tables relate.

Integrity is enforced

With a foreign key, the database blocks orders for customers that don't exist, so bad links never get in.

Prefer simple surrogate IDs

Phone numbers and emails change. An auto-generated number that means nothing makes a stable key.

See it in code

Walk through the code

sql
1CREATE TABLE customers (2  customer_id  INTEGER PRIMARY KEY,3  name         TEXT NOT NULL,4  city         TEXT5);6 7CREATE TABLE orders (8  order_id     INTEGER PRIMARY KEY,9  customer_id  INTEGER NOT NULL REFERENCES customers (customer_id),10  amount       NUMERIC(10, 2)11);12 13INSERT INTO customers VALUES (7, 'Meera Nair', 'Kochi');14INSERT INTO orders VALUES (1, 7, 1499.00);   -- works15INSERT INTO orders VALUES (2, 99, 799.00);   -- fails: no customer 99

1/4customers is the parent. customer_id is its primary key.

The parent table must exist first. MySQL needs the InnoDB engine (the default) for foreign keys to be enforced, and prefers the FOREIGN KEY (...) REFERENCES ... form.

Save yourself the pain

Common mistakes

Oops

Using a customer's name or phone number as the primary key.

Do this instead

Names repeat and phones change. Use a generated ID, and add a UNIQUE constraint on phone if needed.

Oops

Linking tables by a matching column but never declaring a foreign key.

Do this instead

Add REFERENCES so the database rejects orphan rows instead of you finding them months later.

Your mission

Trace the links

~20 min

Quick check

Which statement about a primary key is true?

Go deeper

  1. 1Find the primary key of every table in your sample database.
  2. 2Trace one foreign key from a child table back to its parent table.
  3. 3Try inserting a row with a foreign key value that doesn't exist and read the error.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge