Relational Databases: SQL and ACID
Overview
Almost every application needs to remember things: users, orders, payments. A relational database is the classic, most trusted way to do it, using tables and the SQL language.
In this lesson you learn how tables relate, and what ACID guarantees keep your data correct.
Detailed Notes
What is a relational database
A relational database (RDBMS) stores data in tables made of rows and columns, like a set of spreadsheets that are linked together. You talk to it with SQL (Structured Query Language). Examples: PostgreSQL, MySQL, Amazon RDS, Oracle.
- A row is one record (one user). A column is one field (email).
- A primary key uniquely identifies each row (
user_id). - A foreign key is a column that points to a row in another table, linking the two.
- A schema is the fixed structure of the tables and columns, defined in advance.
The problem it solves
Applications need data that is organised, correct and durable (it survives crashes), even when many users change it at the same time. Keeping the same fact in many places causes mistakes: if a customer's address is copied into every order, changing it means editing hundreds of rows.
Relational databases solve this by normalization: store each fact once, in its own table, and link tables with keys. A join combines rows from linked tables when you ask for them.
How it works
Two tables: users(id, name, email) and orders(id, user_id, total). orders.user_id is a foreign key to users.id.
SELECT users.name, orders.total
FROM orders
JOIN users ON users.id = orders.user_id
WHERE orders.total > 100;
The database also protects your data with transactions: a group of steps treated as one unit. The guarantees are called ACID:
- Atomicity: all steps happen or none do. A money transfer never takes money out without putting it in.
- Consistency: a transaction moves the data from one valid state to another; rules (such as keys and constraints) are never broken.
- Isolation: transactions running at the same time do not see each other's half-finished work.
- Durability: once committed, the data survives a crash or power loss.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
If anything fails before COMMIT, everything rolls back.
Making queries fast on big tables needs indexes, and growing past one machine needs sharding and replication. Those are covered later.
A simple example
Think of a bank ledger. Each customer is listed once in a customer book; each transaction in a ledger refers to the customer by number. Moving money is written as one entry that debits and credits together, or not at all.
System design example
An e-commerce site stores users, products, orders and payments in a relational database. Placing an order is one transaction: reduce stock, create the order, record the payment. If the payment step fails, stock and order are rolled back, so you never sell something you cannot deliver or charge without an order.
Summary
- Relational databases store data in linked tables and are queried with SQL.
- Keys link tables; normalization stores each fact once; joins combine them.
- ACID: atomic, consistent, isolated, durable transactions.
- A fixed schema and strong guarantees make them ideal for orders, payments and accounts.
- Indexes, sharding and replication are covered later.
Architecture Diagram
The app sends SQL; the database enforces ACID transactions.
- 1A client calls the application.
- 2The app opens a transaction and sends SQL statements.
- 3The database applies all steps or none.
- 4On commit, the data is durably stored.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

