What a database management system does
A database management system (DBMS) sits between programs and the files on disk. It decides how data is laid out, who may read or change it, and how to recover after a crash. Programs send requests, and the DBMS returns results.
In a relational database, data lives in tables. A table has columns, which name each kind of fact, and rows, which each hold one record. A primary key is a column, or set of columns, that identifies each row uniquely. Other kinds exist, such as document and key-value stores, but the relational model is the usual starting point.
How a query is answered
Relational systems usually take requests in SQL, a declarative language. The request says what result is wanted, not how to find it. A query planner then chooses a plan, for example scanning every row or using an index.
An index is a separate structure, often a balanced tree, that maps column values to row locations. It works like a book index. Lookups on an indexed column take far fewer steps than a full scan, but the index uses space and must be updated on every write.
Worked example: a small orders table
Suppose a table called orders has 1,000,000 rows and a query asks for the order with id 734,215. Without an index, the system may read all 1,000,000 rows in the worst case. With a balanced tree index that branches 100 ways at each level, the depth is about 3, since 100 x 100 x 100 = 1,000,000. The lookup then needs roughly 3 index steps plus one row read.
Now take a transfer of 50 from account A, holding 200, to account B, holding 100. The transaction subtracts 50 from A, giving 150, and adds 50 to B, giving 150. The total stays 300. If the system crashes between the two steps, the transaction is rolled back, so neither change is kept.
Transactions and common misconceptions
A transaction groups several operations into one unit. Relational systems usually promise the ACID properties: atomicity (all or nothing), consistency (rules such as keys and constraints hold), isolation (concurrent transactions do not interfere in visible ways) and durability (committed data survives a crash).
A database is not just a spreadsheet. It enforces constraints, supports many simultaneous users and recovers from failure. Another misconception is that more indexes always speed things up. Indexes help reads but slow writes. NoSQL systems also do not all give up transactions, since guarantees differ by product.
Where databases appear
Databases sit behind most web applications, banking systems, inventory tools and booking services. A web server usually asks a database server for data over a network, as in client-server designs. Phones and browsers also use small embedded databases to store local data.
Key terms
- DBMS
- Software that stores, retrieves and protects data in a database.
- Table
- A set of rows that share the same columns.
- Primary key
- A column or set of columns that uniquely identifies each row.
- SQL
- A declarative language for querying and changing relational data.
- Index
- A structure that speeds lookups on a column at the cost of extra space and slower writes.
- Transaction
- A group of operations that succeed or fail as one unit.
- ACID
- Atomicity, consistency, isolation and durability, the usual guarantees of transactions.
