Databases & SQL
Lesson 4 of 4 9 min +60 XP

Indexes & Transactions

Fast queries and all-or-nothing safety.

What you'll learn

  • Explain what a database index does
  • State the trade-off indexes carry
  • Describe a transaction and atomicity
The book's index, and the bank transfer

An index at the back of a book jumps you to a topic without reading every page — a database index does the same for queries. A transaction is like a bank transfer: the money leaves one account and lands in the other, all-or-nothing, never half-done.

Speed and safety

An index is a data structure (often a B-tree) that lets the database find rows by a column's value without scanning the whole table — turning a slow linear search into a fast lookup. A transaction groups several operations so they succeed or fail together, keeping data consistent.

Indexes aren't free

Indexes speed up reads but slow down writes (each insert/update must maintain the index) and use extra storage. You index the columns you filter or join on, not every column.

Transferring money: debit one account and credit another inside one transaction. If either step fails, the whole transaction rolls back — you never lose or double the money (atomicity).
Lab · Why wrap in a transaction?
  1. A bank transfer debits account A, then credits account B.
  2. Imagine the system crashes right after the debit.
  3. Explain how a transaction prevents money vanishing.

What you should see: Because the two operations are one atomic transaction, a crash rolls back the debit — either both happen or neither does.

Knowledge Check

+18 XP / correct

1. A database index primarily improves…

2. The 'atomicity' of a transaction means…