Relational Databases
A relational database stores data in tables (relations) made of rows and typed columns, links tables through keys, and lets you ask questions in SQL. The database, not your application, enforces the rules: types, uniqueness, references between tables, and ACID transactions so a half-finished change is never visible. The model comes from E. F. Codd’s 1970 paper and still runs most of the world’s business data: PostgreSQL, MySQL, SQL Server, Oracle and SQLite are all relational.
The building blocks
- Table: one kind of thing, for example
customersororders. Every row has the same columns. - Primary key: a column (or set of columns) that uniquely identifies a row, such as
customers.id. - Foreign key: a column that points to another table’s primary key, such as
orders.customer_id → customers.id. The database rejects orders for customers that do not exist. - Join: combine rows from several tables by matching keys at query time.
- Schema: the declared structure (tables, columns, types, constraints). It is enforced on write (schema-on-write).
- Two tables. Each customer is stored once; each order stores only the customer's id.
- orders.customer_id is a foreign key: every value must exist in customers.id.
- To answer "how much did each customer spend?", SQL joins the tables on that key.
- For sam (id 2) the engine finds orders 102 and 103. An index on customer_id makes this a lookup, not a full scan.
- The result: one row per customer. ryan has no orders, so an inner join leaves him out (a LEFT JOIN would keep him).
- The database guards integrity: an order for customer 9, who does not exist, is rejected.
How a relational database works inside
- Row storage. Rows are packed into fixed-size pages (8 KB in PostgreSQL, 16 KB in InnoDB). Reading or updating one whole row is cheap; scanning one column across 100 million rows is not. That is why analytics moves to columnar systems.
- Indexes. A B-tree index on
customer_idturns “find all orders of customer 2” from reading every page into a few page reads. Indexes speed up reads and slow down writes. See Indexing. - Write-ahead log (WAL). Every change is first appended to a log, and the log is flushed to disk at commit, before the changed data pages are written. After a crash the database replays the log. The same log feeds replicas and change data capture.
- Transactions.
BEGIN … COMMITgroups changes so they all happen or none do (ACID). Concurrency control (MVCC in PostgreSQL and InnoDB) lets readers and writers work without blocking each other. See ACID. - Query planner. SQL says what you want; a cost-based optimizer decides how: which index, which join order, hash join or nested loop.
EXPLAIN ANALYZEshows the plan.
Normalization
Storing each fact once (the customer’s city lives only in customers) avoids update anomalies: change it in one place and every join sees the new value. Analytics schemas often denormalize on purpose to avoid joins. See Normalization or Denormalization.
Popular relational databases
| Database | License | Notes |
|---|---|---|
| PostgreSQL | Open source (PostgreSQL License) | The default choice in 2026. Rich types (jsonb, arrays), extensions (PostGIS, pgvector, TimescaleDB), strong SQL compliance. |
| MySQL / MariaDB | GPL | Huge install base in web apps. InnoDB storage engine. MariaDB is a community fork. |
| SQL Server | Commercial | Common in Microsoft shops; strong BI tooling. |
| Oracle | Commercial | Large enterprises, finance, telecom. |
| SQLite | Public domain | A library, not a server. Ships in every phone and browser; great for local tools and tests. |
When to use one, and when not
Use a relational database when data has clear structure and relationships, correctness matters (money, inventory, bookings), and you need ad hoc queries you did not plan for.
Look elsewhere when a single node cannot hold the write volume (consider distributed SQL or sharding), when the main workload is scanning billions of rows for analytics (a columnar warehouse), or when the access pattern is purely get(key) at extreme scale (a key-value store).
Common mistakes
- Running heavy analytics on the production primary. Use a read replica, or replicate into a warehouse.
- Missing indexes on foreign keys. Joins and cascading deletes turn into full scans.
SELECT *in pipelines. Schema changes upstream silently break or bloat downstream tables. Name your columns.- Long-running transactions. They hold locks and, in PostgreSQL, block vacuum, so tables bloat.
- Treating “relational” as “doesn’t scale”. A well-indexed PostgreSQL on one modern server handles tens of thousands of transactions per second.
Learning Resources
- A Relational Model of Data for Large Shared Data Banks: E. F. Codd, 1970
- PostgreSQL documentation: the tutorial and “Performance Tips” chapters are excellent
- Use The Index, Luke: SQL indexing and tuning, free online
- CMU 15-445/645 Intro to Database Systems: how storage, indexes, transactions and query execution work
- Database Internals, Alex Petrov: storage engines and distributed databases
- Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini
- PostgreSQL Tutorial