CMD Guide
HomeDatabases

Foundations

Step 4 in the Databases path · 2 concepts · 0 problems

0 / 2 complete

📘 Learn Foundations from zero

First principles. A program manipulates data in memory (RAM), but RAM is volatile — cut the power and the data is gone. To make data persist, you write it to durable storage (disk). The earliest, simplest way to do this was to dump records into ordinary files and write application code that opens, reads, and parses them. This is the era "before the advent of the database" — what the textbooks call a file-processing system, typically files manipulated by conventional programs (COBOL, C) each owning its own format.

Analogy — the shoebox of index cards. A small business stores every customer on index cards in shoeboxes. Sales has a shoebox, Billing has its own, Shipping has another. A customer moves house, so someone must update the address in three boxes. Miss one and the boxes disagree — Billing ships to the old address. If two clerks grab the Sales box at the same time, one clerk's edit overwrites the other's. And nobody can quickly answer "which customers bought product X and live in Texas," because the cards were never organized for that question — each box was built for one program's needs.

Worked example. A 1970s payroll program keeps employees.dat; the benefits team keeps a separate benefits.dat that also stores each employee's name and department. When someone changes department, payroll updates its file but benefits is never told — the two files now disagree (inconsistency arising from redundancy). Worse, each program hard-codes the byte offset and field width where "salary" lives, so widening that field breaks every program that reads the file — there is no data independence (program–data dependence).

Key insight: file-based storage works until data is shared. The instant multiple programs and multiple users touch the same data, files force each application to re-invent redundancy control, integrity, querying, concurrency, and recovery — independently, partially, and inconsistently. The DBMS was invented to provide all of these once, behind a single shared interface and data model.

✨ Added by the guide to build intuition — not from the source course.

🎯 Guided practice

  1. Easy — Spot the redundancy. An online store keeps orders.csv with columns (order_id, customer_name, customer_email, customer_address, item, price). A customer places 50 orders, then changes their email. What goes wrong, and why?
    Reasoning: (1) The email is stored once per orderdata redundancy, 50 copies of one fact. (2) Changing it means rewriting all 50 rows; miss any and you get data inconsistency (the same customer shows two emails, and queries can't tell which is true). (3) The fix the relational model teaches: store each fact once — split into a customers table and an orders table that references it by a foreign key customer_id. This is precisely the motivation for normalization (Codd): a non-key fact should depend on the key, the whole key, and nothing but the key. Internalize the pattern: "a fact stored more than once is an inconsistency waiting to happen."
  2. Medium — The lost update. Two cashier terminals both read inventory.txt, which says widgets = 100. Terminal A sells 10, Terminal B sells 5, at nearly the same instant. Each does: read into memory, subtract, write back. What final value can the file hold, and what should it be?
    Reasoning, step by step: The correct result is 100 - 10 - 5 = 85. But trace one interleaving: A reads 100; B reads 100 (A hasn't written yet); A computes 90 and writes 90; B computes 95 from its stale 100 and writes 95. Final value: 95 — the sale of 10 widgets vanished. This is the textbook lost-update anomaly: a read-modify-write race with no concurrency control. (A different interleaving where B finishes first yields 90; either way one update is lost.)
    How a DBMS fixes it: it wraps each read-modify-write in a transaction and enforces isolation via locking (e.g. two-phase locking) or an atomic UPDATE ... SET stock = stock - 10, so the two updates are serializable — the result is equivalent to running one entirely before the other. This is the bridge to ACID: Atomicity (all-or-nothing), Consistency, Isolation (concurrent txns don't interfere), Durability (committed data survives crashes). The general lesson: shared mutable state demands coordinated, atomic, isolated updates — and flat files give you none of them.

✨ Added by the guide — work these before the full problem set.

Lessons in this topic

🧠 Review & recall

Active recall is what moves a topic into long-term memory. Flip each card before revealing, then test yourself — your results are saved on this device.

Flashcard
What was the dominant way organizations stored data 'before the advent of the database', and how was it structured?
tap to reveal →
The traditional file-processing system: data lived in ordinary files (text, CSV, binary) and directories on disk, with each application or department owning its own files and custom programs (e.g. COBOL, C) to read and write them. Access was sequential or, in some systems, indexed.
💡 Shoebox of index cards — every department has its own box.
Flashcard
Why does file-based storage break down the moment data is shared across programs and users?
tap to reveal →
Each application must independently re-invent redundancy control, integrity, querying, concurrency, and recovery — partially and inconsistently. The DBMS was invented to provide all of these once, behind a single shared interface and data model.
💡 Files work until data is SHARED; then everyone reinvents the same five wheels badly.
Flashcard
Name the major disadvantages of the traditional file system that motivated databases.
tap to reveal →
Data redundancy and inconsistency, data isolation (poor integration of related data), difficulty of data access (programming dependency, slow sequential search), lack of security/centralized control, limited data integrity and reliability (error-prone, corruption-prone under concurrent access), and poor scalability.
💡 RISKS: Redundancy, Isolation, Slow-access, Killed-security, Scalability (+ integrity).
Flashcard
In the shoebox/files analogy, why does redundancy cause inconsistency when a customer changes their email after 50 orders?
tap to reveal →
The email is stored once per order (50 copies of one fact). Changing it means rewriting all 50 rows; miss any and the same customer shows two emails and queries can't tell which is true. The fix is to store each fact once: split into customers and orders tables linked by a foreign key.
💡 A fact stored more than once is an inconsistency waiting to happen.
Flashcard
Describe the lost-update anomaly using two terminals editing inventory.txt (widgets=100; A sells 10, B sells 5).
tap to reveal →
Correct result is 85, but in a read-modify-write race: A reads 100, B reads 100, A writes 90, B writes 95 from its stale 100 — final value 95, and the sale of 10 vanishes. It happens because flat files give no concurrency control.
💡 Two readers, one stale write — an update silently disappears.
Flashcard
How does a DBMS prevent lost updates, and what does ACID stand for?
tap to reveal →
It wraps each read-modify-write in a transaction and enforces isolation via locking (e.g. two-phase locking) or an atomic UPDATE ... SET stock = stock - 10, making the updates serializable. ACID = Atomicity (all-or-nothing), Consistency, Isolation (concurrent txns don't interfere), Durability (committed data survives crashes).
💡 ACID: A-tomic, C-onsistent, I-solated, D-urable.
Flashcard
What goals were databases designed to achieve over file systems?
tap to reveal →
Reduce data redundancy via centralized storage, improve consistency with a single source of truth, enhance access through structured query languages like SQL, support concurrent access without integrity loss, strengthen security/authorization by user role, and ensure integrity via automatically enforced constraints and relationships.
💡 Centralize, single-source-of-truth, SQL, concurrency, security, constraints.
Q1. A 1970s payroll program stores employees.dat while the benefits team keeps benefits.dat that also stores each employee's name and department. A department change is applied to payroll but not benefits. Which file-system problem is this?
Q2. Two cashier terminals each read inventory.txt (widgets=100), one sells 10 and one sells 5 at nearly the same instant using read-modify-write. Which final value reflects the lost-update anomaly, and why?
Q3. According to the lesson, why was retrieving related data hard in traditional file systems?
Q4. Which capability did databases introduce that file systems lacked for multiple users working at once?
Q5. An online store keeps orders.csv with (order_id, customer_name, customer_email, customer_address, item, price). What is the relational-model fix for the redundancy this creates?