Topic 058 / Core
Database fundamentals
Chapter 5.1 · Data Modeling and Relational Databases
What and why
A program can keep data in plain files, but files give no help with concurrent access, structure, or surviving a crash mid-write. A database is a system that stores data durably, lets many clients read and write it concurrently without corrupting it, and answers queries faster than scanning raw bytes — through a query interface, a query engine, and explicit guarantees about durability and concurrency. Builds on: → 9 (CPU, RAM, storage and I/O).
Diagram
client ──query──► query interface (SQL / API)
│
query engine (parse, plan)
│
storage engine (pages or log)
│
memory buffer ◄──► disk
How it works
- A client sends a query through a driver using the database's wire protocol.
- The query engine parses it and chooses an execution plan (→ 66).
- The storage engine reads or writes pages, using an in-memory buffer to avoid disk I/O on every access.
- Writes are made durable before being acknowledged (→ 68).
- The result is returned to the client.
Practical example
Finding one customer by id in a flat CSV file means reading every line until a match (illustrative: O(n) scan of a growing file). The same lookup against an indexed table (→ 64) costs a handful of page reads regardless of table size — the reason "just use files" stops working once data or concurrency grows past a single process.
When to use it
- Use when: data needs structure, concurrent readers/writers, and durability guarantees stronger than "whatever the filesystem happened to flush."
- Avoid when: it's genuinely one process with tiny, rarely-touched data — a file is simpler and has no server to run.
- Failure scenarios: treating a database table as a message queue and polling it (→ 106) instead of using one; assuming "it's in the database" means "it's safe" without checking durability settings (→ 68).
- Simpler alternative: a flat file or embedded key-value store for single-process, low-concurrency, low-stakes data.
Practice
(10–15 min, lab Postgres). Connect with psql -h localhost -U lab lab,
create a small table, insert a few rows, and run a SELECT. Done when you can connect,
write, and read back the same data without looking anything up.
Key takeaway
A database adds structure, concurrency safety, and durability over raw files — pick it because you need those, not by default. Done when you can: name the three things a database gives you that a file doesn't. Used next in → 59, 60, 61.
References
None — this topic makes no external claims.
Source directory