Relational Databases & SQL
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.
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 verb | Meaning |
|---|---|
| SELECT | Read matching rows. |
| INSERT | Create rows. |
| UPDATE | Change matching rows. |
| DELETE | Remove matching rows. |
Examples
List today’s pickups
You need the customer name and car model for each pickup today.
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.
Reject a missing drill
A loan must point to a tool that your library actually owns.
CREATE TABLE tools (id INTEGER PRIMARY KEY);
CREATE TABLE loans (
id INTEGER PRIMARY KEY,
tool_id INTEGER NOT NULL REFERENCES tools(id)
);
INSERT INTO loans (id, tool_id) VALUES (1, 999);
-- Rejected: tool 999 does not exist in the empty tools table.The rule applies even if an application forgets to check. A foreign key can also reference a declared unique value. Keys may span multiple columns.
The insight
The database itself guards the links.
Total miles for each member
You want total miles earned by each member during 2026.
SELECT member_id, SUM(miles) AS total_miles
FROM mileage_earnings
WHERE earned_on >= DATE '2026-01-01'
AND earned_on < DATE '2027-01-01'
GROUP BY member_id;You ask a new question using existing rows. You do not need another stored total for every possible date range.
The insight
SQL answers new questions using the data you already have.
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
Takeaways
- Relational databases store tables linked by keys.
- SQL reads and changes rows.
- Keep each fact in one place and join when needed.