Skip to content

Databases in Ten Minutes: Tables, Indexes, and Transactions

6 min reading

Outcome: Explain rows, keys, joins, indexes and transaction boundaries, then choose a data model from the questions an application asks.

Page details

System Design foundations · Chapter 3 · Sources

Published · Last edited: · ~6 min reading · ~17 min with practice

20px

Know first: Numbers Every Engineer Should Know: Units, Latency, and Back-of-the-Envelope Math

System Design foundations glossary

Hypothetical scenario. Northwind stores customers, orders and products in Postgres. Priya needs the customer attached to an order, while Dev needs to change stock without leaving an order half-recorded. “Store some JSON” does not yet answer either question.

Describe the facts before the storage

A relational table organizes records as rows with named columns. A customer row might contain a customer identifier and delivery address. A product row might contain a product identifier and price. An order connects a customer with purchased items. Giving each entity an explicit identity prevents two people with the same name from accidentally becoming the same customer.

A primary key identifies a row uniquely within a table. A foreign key expresses a relationship to another table and can enforce that the referenced identity exists. These constraints are more than documentation: the database can reject a write that would violate them. They still need to describe the business correctly; a valid customer identifier does not prove the customer authorized a purchase.

SQL is a language for asking questions over those records. A selection keeps rows matching a condition. A projection chooses columns. A join combines related rows, such as an order with its customer, according to a declared condition. The important first question is which records are related, not how to memorize a query's punctuation.

Why a lookup structure helps

An index is an additional structure that helps locate records by selected values. A common B-tree index keeps lookup information organized so the database can narrow the search without examining every table row. The table stores the facts; the index provides another route to them. Index design therefore depends on the predicates and ordering the workload needs.

Query conditioncustomer = 872,113Index traversalnarrow the lookupTable rowread the factsTransaction: a separate boundary for coordinated commit or rollback.
The lookup path is not itself a transaction protocol; index and table changes need an appropriate commit boundary.

Illustrative lookup. Suppose one million customer rows are physically unordered by customer identifier, and the request asks for customer 872,113. A full scan may examine all 1,000,000 rows to find matches or prove there are no more. An index can instead navigate a lookup hierarchy and then locate the row. The identifier's numerical value does not mean the record sits at position 872,113 in storage.

To make the hierarchy concrete, choose an idealized tree with fan-out 100 and leaves holding 100 lookup entries each. One million entries need 1,000,000 / 100 = 10,000 leaves. One hundred intermediate pages can point to those leaves, and one root can point to the intermediates. A lookup follows root → intermediate → leaf: three index pages, then potentially a table-page access. These are chosen structural counts, not promised disk reads or timings; cache state and the actual index layout matter.

Writes pay for the other route

An inserted customer changes the table and every relevant index. Updating an indexed address can require additional index maintenance. The extra structure consumes space and work even when no request uses it. An index that helps one rare query can burden a frequent write path. The next storage lesson develops this trade-off rather than treating indexes as a universal speed switch.

In the wild. PostgreSQL 17's index documentation explains that indexes improve retrieval while adding overhead to the database system. This is the relevant contract: an index can change how a result is found, while preserving the meaning of the query. Check an execution plan and a representative workload before making a performance claim.

A transaction draws a boundary

A transaction groups database work into a declared commit or rollback boundary. Atomicity means its changes commit together or not at all. Consistency means supported integrity rules remain satisfied. Isolation governs what concurrent operations can observe. Durability concerns retaining committed changes through the failures the database promises to tolerate. These ACID words describe contracts, not a blanket guarantee that any application workflow is correct.

For a toy order, choose stock of ten units and a purchase of two. A successful atomic transition records the order and changes stock from 10 to 8. If order creation fails before commit, rolling back should not leave stock at eight with no corresponding order. Concurrent buyers still need an appropriate conditional update, locking or isolation strategy; merely placing statements inside a transaction does not explain the contested-stock decision.

PostgreSQL's transaction tutorial describes commit and rollback at the database boundary. A card provider outside that transaction remains a separate participant. Rolling back local rows cannot automatically undo a charge that the external provider has already accepted.

Choose a model for the questions

A key-value store emphasizes retrieving a value through its key. A document store keeps structured documents and offers its own querying and consistency rules. A relational system expresses relationships and constraints through tables and queries. These are different interfaces and models; none removes the need to understand access patterns, concurrent changes and recovery.

Misconception: more indexes always make a database faster. Extra indexes may help reads and slow writes, increase storage and complicate maintenance. Start with the questions that must be answered and the invariants that must hold. Ten minutes is an orientation to the vocabulary, not a claim that schema design or transactions become effortless.

Check yourself

Two customers share a name. Can the name alone be the primary key?

Only if the business actually guarantees uniqueness, which this scenario does not. Use an explicit identity and treat a display name as a separate attribute.

Does the illustrative three-page index path guarantee three physical disk reads?

No. Pages may be cached; the row may require another access; real fan-out, page layout and concurrency differ. The example counts a chosen logical path.

The provider charged the card before the local transaction rolled back. Was the charge rolled back too?

Not by the local transaction. The external effect needs its own identity and reconciliation or compensation contract.

Go deeper