Lesson 212 of 296
What a database is: tables, rows and keys
What you will learn
- What a database and a DBMS are
- Relational databases: tables, rows and columns
- Data types
- Primary keys and foreign keys
- SQL and its parts
- The sample database used in this part
On this page
What is a database?
A database is an organised collection of data that is stored permanently and can be searched and changed safely by many users at once. A database management system (DBMS) is the software that does this: PostgreSQL, MySQL, SQL Server, Oracle, SQLite.
Why not just save JSON files? Because a DBMS gives you:
- Fast searching over millions of records (indexes, Lesson 220).
- Safe concurrent changes by many users (transactions, Lesson 223).
- Rules that keep data valid (constraints, Lesson 226).
- Durability: data survives crashes.
- A powerful query language.
Relational databases
A relational database stores data in tables, like spreadsheets with strict rules:
- A table holds one kind of thing:
customers,orders. - A row (record) is one item: one customer.
- A column (field) is one property, with a fixed data type:
name,email.
| id | name | city | |
|---|---|---|---|
| 1 | Aisha Khan | aisha@example.com | London |
| 2 | Ben Smith | ben@example.com | Leeds |
Tables are related to each other through keys, hence "relational".
Data types (PostgreSQL)
| Type | Holds | Notes |
|---|---|---|
integer, bigint | Whole numbers | Use bigint for IDs of very large tables |
numeric(10,2) | Exact decimals | For money if you do not use integer pence |
real, double precision | Approximate decimals | Never for money (Lesson 30) |
text, varchar(n) | Text | In PostgreSQL text is as fast as varchar |
boolean | true / false | |
date | A calendar date | |
timestamptz | A moment in time | Always use timestamptz (stored as UTC) rather than timestamp without a time zone |
uuid | A 128-bit unique ID | Good for public IDs |
jsonb | JSON data | Flexible fields; can be indexed |
text[] | Arrays |
Keys
Primary key (PK): a column (or set of columns) that uniquely identifies each row. It can never be NULL or duplicated. Usually an id.
- A surrogate key is an artificial ID (
id1, 2, 3 or a UUID) with no business meaning. Most tables use one. - A natural key is real data that is unique (an email, an ISBN). Natural keys can change (people change emails), so they make poor primary keys, but they should still have a unique constraint.
Foreign key (FK): a column that refers to the primary key of another table. orders.customer_id refers to customers.id. The database then refuses an order for a customer that does not exist, and refuses deleting a customer who still has orders (unless told otherwise). This is called referential integrity.
SQL
SQL (Structured Query Language) is the standard language for relational databases. Its parts:
| Part | Commands | Purpose |
|---|---|---|
| DDL (Data Definition) | CREATE, ALTER, DROP | Define tables and structure |
| DML (Data Manipulation) | SELECT, INSERT, UPDATE, DELETE | Read and change data |
| DCL (Data Control) | GRANT, REVOKE | Permissions |
| TCL (Transaction Control) | BEGIN, COMMIT, ROLLBACK | Transactions |
SQL keywords are not case-sensitive (select works), but writing them in capitals makes queries easier to read. Text values use single quotes: 'London'. Double quotes are for identifiers (table and column names) with special characters.
The sample database
All examples in this part use this small shop:
CREATE TABLE customers (
id integer PRIMARY KEY,
name text NOT NULL,
email text NOT NULL UNIQUE,
city text,
created_at date NOT NULL
);
CREATE TABLE products (
id integer PRIMARY KEY,
name text NOT NULL,
category text NOT NULL,
price_pence integer NOT NULL CHECK (price_pence > 0)
);
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer NOT NULL REFERENCES customers(id),
status text NOT NULL CHECK (status IN ('pending', 'paid', 'cancelled')),
ordered_at date NOT NULL
);
CREATE TABLE order_items (
order_id integer NOT NULL REFERENCES orders(id),
product_id integer NOT NULL REFERENCES products(id),
quantity integer NOT NULL CHECK (quantity > 0),
unit_price_pence integer NOT NULL,
PRIMARY KEY (order_id, product_id)
);The data:
customers
| id | name | city | created_at | |
|---|---|---|---|---|
| 1 | Aisha Khan | aisha@example.com | London | 2026-01-05 |
| 2 | Ben Smith | ben@example.com | Leeds | 2026-02-11 |
| 3 | Chen Li | chen@example.com | London | 2026-03-02 |
| 4 | Dara Patel | dara@example.com | NULL | 2026-04-20 |
| 5 | Eve Jones | eve@example.com | Manchester | 2026-05-15 |
products
| id | name | category | price_pence |
|---|---|---|---|
| 1 | Mug | Kitchen | 899 |
| 2 | Notebook | Stationery | 450 |
| 3 | Desk Lamp | Home | 2999 |
| 4 | Pen Set | Stationery | 1250 |
| 5 | Kettle | Kitchen | 3499 |
orders
| id | customer_id | status | ordered_at |
|---|---|---|---|
| 101 | 1 | paid | 2026-06-01 |
| 102 | 1 | paid | 2026-06-15 |
| 103 | 2 | cancelled | 2026-06-20 |
| 104 | 3 | paid | 2026-07-02 |
| 105 | 4 | pending | 2026-07-10 |
| 106 | 3 | paid | 2026-08-01 |
order_items
| order_id | product_id | quantity | unit_price_pence |
|---|---|---|---|
| 101 | 1 (Mug) | 2 | 899 |
| 101 | 2 (Notebook) | 1 | 450 |
| 102 | 3 (Desk Lamp) | 1 | 2999 |
| 103 | 4 (Pen Set) | 1 | 1250 |
| 104 | 1 (Mug) | 1 | 899 |
| 104 | 4 (Pen Set) | 2 | 1250 |
| 105 | 2 (Notebook) | 3 | 450 |
| 106 | 3 (Desk Lamp) | 1 | 2999 |
| 106 | 1 (Mug) | 1 | 899 |
Notice the deliberate details: Dara has no city (NULL), Eve has no orders, the Kettle was never ordered, and order_items stores the price at the time of the order (unit_price_pence), because product prices change later. order_items has a composite primary key (two columns together).
How to say it in an interview
"A relational database stores data in tables of rows and typed columns, managed by a DBMS like PostgreSQL that gives fast queries, transactions, constraints and durability. Each table has a primary key that uniquely identifies rows, usually a surrogate ID, and foreign keys reference other tables' primary keys so the database enforces referential integrity. SQL has DDL for structure, DML for data, DCL for permissions and TCL for transactions. I use timestamptz for times, integers or numeric for money, and keep natural identifiers like email unique."
Key words
| Word | Meaning |
|---|---|
| Database / DBMS | Organised stored data / the software that manages it |
| Table, row, column | A kind of thing, one item, one property |
| Primary key | Uniquely identifies each row |
| Foreign key | References a primary key in another table |
| Referential integrity | References always point to rows that exist |
| Surrogate vs natural key | Artificial ID vs meaningful real-world value |
| Composite key | A key made of several columns |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.