Tables, rows, keys and joins: the classic way to store connected data (PostgreSQL, MySQL).
A relational database stores data in tables. A table has rows (one record each) and columns (one fact each). Tables are linked by keys, and you ask questions with SQL.
Think of a spreadsheet with strict rules. Every row in a table has the same columns, and each column has a type (number, text, date). One column, the primary key, is unique for each row, so you can always find exactly one row.
Real data is connected. A customer has many orders. An order has many items. Instead of copying the customer details onto every order, we keep a customers table and an orders table. An order stores only a customer_id. That column is a foreign key: it must match a real row in the other table. This way of splitting data is called normalization.
| Piece | What it is | Example |
|---|---|---|
| Table, row, column | The grid that holds the data. | customers(id, name, city) |
| Primary key | A column (or columns) that is unique for each row. | id |
| Foreign key | A column that must match a primary key in another table. | orders.customer_id |
| Index | An extra sorted structure that finds rows fast, usually a B-tree. | index on orders(customer_id) |
| Join | Combines rows of two tables where keys match. | INNER JOIN, LEFT JOIN |
| Constraint | A rule the database enforces for you. | NOT NULL, UNIQUE, CHECK |
| Transaction | A group of changes that is all or nothing (ACID). | BEGIN ... COMMIT |
| Shape | Meaning | How it is stored |
|---|---|---|
| One to one | A user has one profile. | Foreign key with a unique rule, or the same primary key. |
| One to many | A customer has many orders. | Foreign key on the "many" side. |
| Many to many | Students and courses. | A third "join table" with two foreign keys. |
SELECT o.id, o.item, c.name, c.city FROM orders o JOIN customers c ON c.id = o.customer_id WHERE c.id = ?| id | name | city |
|---|---|---|
| 1 | Asha | Pune |
| 2 | Ben | Leeds |
| 3 | Chen | Oslo |
| id | customer_id | item | price |
|---|---|---|---|
| 101 | 1 | Book | $12 |
| 102 | 1 | Pen | $3 |
| 103 | 2 | Lamp | $30 |
| 104 | 1 | Mug | $8 |
| 105 | 3 | Desk | $120 |
| 106 | 2 | Cable | $5 |
A simplified cost model: it counts rows touched, not milliseconds. Real databases use indexes, so a join on a foreign key is usually fast. The point is where each fact is stored and who guards it.
CREATE TABLE customers ( id BIGINT PRIMARY KEY, name TEXT NOT NULL, city TEXT ); CREATE TABLE orders ( id BIGINT PRIMARY KEY, customer_id BIGINT NOT NULL REFERENCES customers(id), item TEXT NOT NULL, price NUMERIC(10,2) NOT NULL ); CREATE INDEX idx_orders_customer ON orders(customer_id); SELECT o.id, o.item, c.name FROM orders o JOIN customers c ON c.id = o.customer_id WHERE c.id = 1;
orders.customer_id turns that into a quick lookup. (PostgreSQL does not create it for you.)| Term | Meaning in easy words |
|---|---|
| Table, row, column | A grid of data. A row is one record. A column is one field |
| Schema | The declared shape of the tables, columns and types |
| Primary key | A unique id for each row in a table |
| Foreign key | A column that points to a row in another table and is checked by the database |
| Join | Combining rows from two tables where a key matches |
| SQL | The language used to ask the database questions and change data |
| Normalization | Splitting data so each fact is stored once |
| Index | A sorted helper structure that finds rows fast |
| Transaction | A group of changes that happens fully or not at all |
| ACID | Atomicity, Consistency, Isolation, Durability: the transaction promises |
| Query planner | The part that decides how to run a query (which index, which join order) |
| Migration | A script that changes the schema safely over time |
| Choice | We gain | We lose |
|---|---|---|
| Normalize more | Clean data, small updates | More joins on reads |
| Denormalize | Faster, simpler reads | Copies to keep in sync, risk of mismatch |
| More indexes | Faster reads | Slower writes, more disk |
| Strong isolation | Safer results | More locking or retries, less throughput |
| Single primary | Simple and consistent | One machine takes all the writes |
| Read replicas | More read capacity | Replicas can lag behind (stale reads) |