Database Relational Databases
What you will learn
- What "relational" means in practice
- What relational databases are very good at
- Where they are a weaker fit
- Why they are the default choice for most business data
1. The idea in one paragraph
A relational database stores data in tables (rows and columns), links tables with keys, asks questions in SQL, and enforces rules (types, NOT NULL, UNIQUE, CHECK, foreign keys). You met every one of these ideas in chapters 2 to 4. The other database kinds in this chapter change one or more of those choices.
2. A small demo
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). Two tables, linked by a key, with rules:
SELECT c.customer_id, c.last_name, COUNT(o.order_id) AS orders
FROM customers c JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id
ORDER BY orders DESC, c.customer_id
LIMIT 3;
customer_id last_name orders
----------- --------- ------
72 Silva 17
292 Ahmed 17
134 Kamau 15
(3 rows)
The database refuses an order for customer 999999, who does not exist:
INSERT INTO orders (customer_id, order_date, status, ship_city) VALUES (999999, '2026-01-01', 'pending', 'Leeds');
IntegrityError: FOREIGN KEY constraint failed
SELECT c.customer_id, c.full_name, COUNT(a.account_id) AS accounts, SUM(a.balance_cents) AS total_cents
FROM customers c JOIN accounts a ON a.customer_id = c.customer_id
GROUP BY c.customer_id
ORDER BY total_cents DESC, c.customer_id
LIMIT 3;
customer_id full_name accounts total_cents
----------- ----------- -------- -----------
96 Rosa Kumar 7 1189699
260 Arjun Sato 4 1066703
133 Leo Alvarez 4 862817
(3 rows)
The database refuses an account for customer 999999, who does not exist:
INSERT INTO accounts (customer_id, branch_id, account_type, opened_on, balance_cents) VALUES (999999, 1, 'savings', '2026-01-01', 0);
IntegrityError: FOREIGN KEY constraint failed
SELECT s.student_id, s.last_name, COUNT(e.section_id) AS sections
FROM students s JOIN enrollments e ON e.student_id = s.student_id
GROUP BY s.student_id
ORDER BY sections DESC, s.student_id
LIMIT 3;
student_id last_name sections
---------- --------- --------
37 Chowdhury 8
16 Garcia 7
74 Reyes 7
(3 rows)
The database refuses an enrollment for student 999999, who does not exist:
INSERT INTO enrollments (student_id, section_id, enrolled_on) VALUES (999999, 1, '2026-01-01');
IntegrityError: FOREIGN KEY constraint failed
The join added up each row's children, and the database refused a row that pointed at nothing.
3. Why it is the default
Most business data is exact (a payment is either 500 or it is not) and connected (a payment belongs to an order, which belongs to a customer). A relational database was designed for exactly that: the rules and transactions keep it correct, and SQL lets you ask questions nobody planned for when the tables were created. Modern relational systems have also grown: PostgreSQL, for example, can store JSON in json and jsonb columns and index inside them (checked in its documentation), so a relational database can often cover a little "document" need too. That is why the sensible question is usually not "why relational?" but "is there a clear reason not to?"
You will often hear the umbrella word NoSQL for databases that are not relational. It describes what they are not rather than what they are, and some of them now offer SQL-like languages, so treat it as a loose label. The kinds in lessons 9.2 to 9.4 are the main ones.
4. Strengths
| Strength | Why it matters |
|---|---|
| Exact, connected data | orders, payments, students and results all link to each other |
| Rules enforced by the database | every program that uses the data gets the same checks |
| Flexible questions | SQL answers questions you did not plan for when you designed the tables |
| Transactions | several changes succeed or fail together (chapter 6) |
| Mature tools | decades of tools, teaching and drivers for every language |
5. Weaker spots
- The shape is fixed in advance. Adding a column to a very large table is a planned change (lesson 8.7).
- Data that does not fit rows and columns well, such as deeply nested documents or a network of friends, needs extra work (lessons 9.2 and 9.4).
- One machine is the simple case. Spreading a relational database over many machines is possible but harder (lesson 9.6).
Worked example: which data fits?
| Data | Relational fit? |
|---|---|
| Customers, orders and payments of a shop | strong: exact, connected, must be correct |
| School students, classes and marks | strong |
| Each user's free-form notes with different fields | possible, but a document database may be easier |
| Millions of short-lived "who is online" flags | possible, but a key-value store may be simpler |
All the data in this example is made up.
Common mistakes
- Thinking "relational" means "has relationships between tables". It comes from the mathematical word relation, which is the formal name for a table.
- Dropping it only because it is old. Age is not a weakness; most business data is still exact and connected.
- Skipping the rules and then blaming the database for bad data.
Key takeaways
- Relational databases use tables, keys, SQL and enforced rules.
- They are the best default for exact, connected data that must stay correct.
- Their weaker spots are very flexible shapes and spreading across many machines.
Practice
1. Name four features of a relational database.
Show answer
Tables, keys, SQL and enforced rules (also transactions).
2. In the demo, why was the order for customer 99 refused?
Show answer
The foreign key says customer_id must match an existing customer, and no customer 99 exists.
3. Why do rules in the database help when several programs use the same data?
Show answer
Every program gets the same checks, so one careless program cannot store bad data.
4. Give an example of data that fits a relational database well.
Show answer
For example bank accounts and transfers, or a shop's customers, orders and payments.
5. A friend says "relational databases are old, so use something modern". Give two reasons why age alone is not a reason.
Show answer
Most business data is still exact and connected, which is what relational databases handle best; their rules, transactions and SQL are mature and widely known; and modern relational systems have added features such as JSON columns. A different kind should be chosen for a clear need, not for novelty.
6. Where does the word "relational" come from?
Show answer
From the mathematical term relation, the formal name for a table.