System Design Academy

Saving…

Lesson 2 of 10 in Module 30 of 54 lessons complete

Relational Databases & SQL

Module 3, lesson 2Concept12 min

Definition

Relational database

In plain English
A relational database stores data in tables of rows and columns and links tables using shared values.
Think of it like
It is like a set of spreadsheets where one sheet's customer number points to a row in the customers sheet.

SQL

In plain English
SQL is the standard language for asking a relational database to read and change data.
Think of it like
It is like a precise order form every relational database understands.

Why it exists

You copy a customer’s name into every car booking. After a name change, yesterday’s copies disagree with today’s.

Keep customer details in one table. Store a link in each booking so every lookup reaches the same name.

How it works

Primary key

In plain English
A primary key is a column whose value uniquely identifies each row.
Think of it like
Like a passport number for a person.

Foreign key

In plain English
A foreign key is a column that points to a primary key in another table, and the database refuses links to rows that don't exist.
Think of it like
Like a cloakroom ticket that must match a real hook.

Join

In plain English
A join combines rows from two tables using matching values.
Think of it like
Like matching receipts to customers by customer number.

Schema

In plain English
A schema is the declared structure of tables, columns and their types.
Think of it like
Like the printed layout of a form.

The customers table links to bookings through customer_id, and the cars table links through car_id.

Bookings link customers and cars through their identifying values.

The customers table links to bookings through customer_id, and the cars table links through car_id.

REFERENCES declares foreign keys; NOT NULL requires a value.
SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);
CREATE TABLE cars (
  id INTEGER PRIMARY KEY,
  model VARCHAR(100) NOT NULL
);
CREATE TABLE bookings (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  car_id INTEGER NOT NULL REFERENCES cars(id),
  pickup_date DATE NOT NULL
);
SQL verbMeaning
SELECTRead matching rows.
INSERTCreate rows.
UPDATEChange matching rows.
DELETERemove matching rows.

Examples

List today’s pickups

You need the customer name and car model for each pickup today.

AS gives each table a short name within this query.
SQL
SELECT c.name, r.model
FROM bookings AS b
JOIN customers AS c ON c.id = b.customer_id
JOIN cars AS r ON r.id = b.car_id
WHERE b.pickup_date = CURRENT_DATE;

The join follows stored IDs to the customer and car rows. Updating one customer name changes future query results.

The insight

Each fact lives once; joins combine facts when needed.

Trade-offs

Pros

  • Declared rules protect relationships.
  • SQL combines and summarizes records.
  • Mature tools support operations.

Cons

  • Structure changes require planning.
  • Large joins need careful queries and indexes.
  • Spreading writes across machines adds work.

Use when

  • Structured records relate to one another.
  • Balances and bookings need declared rules.

Avoid when

  • Highly varied records fit another model better.
  • Measured write demands exceed the chosen relational setup.

Quick check

What does a foreign key prevent?
Bookings store customer_id instead of the customer's full name. Why?

Takeaways

  • Relational databases store tables linked by keys.
  • SQL reads and changes rows.
  • Keep each fact in one place and join when needed.

Try the quick check above whenever you feel ready.

Previous: What Is a Database?