Lesson 2 of 24 in SQL for Beginners · 14m
Tables, rows and keys
Before you can ask good questions of a database, you need a picture of what is in it. In this lesson you will meet the sample shop that every example in the course uses, and learn the vocabulary for describing any database you come across: tables, columns, rows, data types and keys.
The sample shop
The shop sells a handful of products to a handful of customers. It has four tables:
-
customers: one row per person who has signed up.
-
products: one row per item the shop sells.
-
orders: one row per order a customer has placed.
-
order_items: one row per product within an order (an order with three different products has three rows here).
This is the script that creates the tables. You do not need to understand every word yet; the last section of the course explains CREATE TABLE and constraints in detail. For now, notice the column names and the words PRIMARY KEY and REFERENCES.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
city VARCHAR(100),
signup_date DATE NOT NULL,
referred_by INTEGER REFERENCES customers (customer_id)
);
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
category VARCHAR(50) NOT NULL,
price NUMERIC(10, 2) NOT NULL CHECK (price >= 0)
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date DATE NOT NULL,
status VARCHAR(20) NOT NULL CHECK (status IN ('pending', 'shipped', 'cancelled')),
shipped_date DATE
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders (order_id),
product_id INTEGER NOT NULL REFERENCES products (product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
unit_price NUMERIC(10, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
Columns and data types
Every column has a name and a data type. The type decides what values the column can hold and how they behave:
-
INTEGER holds whole numbers such as IDs and quantities.
-
NUMERIC(10, 2) holds exact decimal numbers with up to 10 digits, 2 of them after the decimal point. Use it for money; never store money in floating-point types, which cannot represent values like 0.10 exactly.
-
VARCHAR(50) holds text up to 50 characters.
-
DATE holds a calendar date. Dates are written in the ISO format '2025-03-03' (year, month, day), which every major database understands.
Types matter in practice. Because order_date is a real date, the database can compare dates correctly, find everything in April, or calculate how many days shipping took. If dates were stored as text like "3/4/25", none of that would be reliable.
Some columns also say NOT NULL, meaning a value is required. Others, such as city and shipped_date, allow NULL, which means "no value" or "unknown". Elena has no city on file, and orders that have not shipped have no shipped date. NULL behaves in surprising ways, so it gets its own lesson in the next section.
SELECT * FROM customers;
| customer_id | first_name | last_name | city | signup_date | referred_by | |
|---|---|---|---|---|---|---|
| 1 | Ana | Silva | ana@example.com | Lisbon | 2025-01-05 | NULL |
| 2 | Ben | Carter | ben@example.com | London | 2025-01-20 | NULL |
| 3 | Chloe | Martin | chloe@example.com | Paris | 2025-02-11 | 1 |
| 4 | David | Kim | david@example.com | London | 2025-03-02 | 1 |
| 5 | Elena | Rossi | elena@example.com | NULL | 2025-03-15 | 2 |
| 6 | Farid | Haddad | farid@example.com | Lisbon | 2025-04-01 | NULL |
Primary keys: one row, one identity
A primary key is a column (or set of columns) whose value uniquely identifies each row. In customers it is customer_id: no two customers can share an ID, and every customer must have one. The database enforces both rules.
Why not use the name? Because two customers can have the same name, and people change their names and emails. A good primary key is unique, never empty, and never changes. Most tables use a meaningless number that the database generates, often called a surrogate key.
The order_items table has a composite primary key: (order_id, product_id). Neither column is unique on its own (order 101 has two items; the notebook appears in several orders), but the pair is. The same product cannot appear twice in the same order; you would increase the quantity instead.
Foreign keys: how tables point at each other
A foreign key is a column whose values must match a primary key in another table. In orders, the column customer_id REFERENCES customers (customer_id). That means every order points to exactly one existing customer.
SELECT * FROM orders;
| order_id | customer_id | order_date | status | shipped_date |
|---|---|---|---|---|
| 101 | 1 | 2025-03-03 | shipped | 2025-03-05 |
| 102 | 2 | 2025-03-10 | shipped | 2025-03-13 |
| 103 | 1 | 2025-03-21 | shipped | 2025-03-22 |
| 104 | 3 | 2025-04-02 | cancelled | NULL |
| 105 | 4 | 2025-04-15 | shipped | 2025-04-18 |
| 106 | 2 | 2025-04-28 | pending | NULL |
| 107 | 5 | 2025-05-06 | shipped | 2025-05-08 |
| 108 | 1 | 2025-05-19 | pending | NULL |
Order 102 has customer_id 2. To find out who placed it, look up customer 2:
SELECT customer_id, first_name, last_name
FROM customers
WHERE customer_id = 2;
| customer_id | first_name | last_name |
|---|---|---|
| 2 | Ben | Carter |
Doing this lookup by hand for every row would be tedious, which is exactly what joins automate; they get a whole section later in the course. The foreign key also protects the data. If you try to add an order for a customer that does not exist, PostgreSQL refuses:
INSERT INTO orders (order_id, customer_id, order_date, status)
VALUES (109, 99, '2025-06-01', 'pending');
The error reads: ERROR: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey", with the detail Key (customer_id)=(99) is not present in table "customers". This rule is called referential integrity: references always point at something real. (SQLite only enforces foreign keys after you run PRAGMA foreign_keys = ON;.)
Relationships between tables
Keys create relationships, and most relationships are one of two kinds:
-
One-to-many. One customer can place many orders, but each order belongs to one customer. The "many" side (orders) holds the foreign key.
-
Many-to-many. One order can contain many products, and one product can appear in many orders. You cannot model this with a single foreign key, so the shop uses a linking table, order_items, with a foreign key to each side. Each row says "this order contains this product, in this quantity, at this price".
There is also a relationship from customers to itself: referred_by holds the ID of the customer who recommended the shop. Chloe and David were both referred by Ana (customer 1). You will query this when you learn self joins.
Here are the items in order 105:
SELECT *
FROM order_items
WHERE order_id = 105;
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 105 | 1 | 5 | 4.50 |
| 105 | 4 | 1 | 9.50 |
| 105 | 6 | 1 | 15.00 |
Notice that order_items stores unit_price even though products already has a price. That is deliberate: prices change over time and discounts happen. Order 107 bought headphones for 75.00 while the list price is 79.00. The order must remember what the customer actually paid, so the price is copied at the moment of sale.
- Relational database
- A database that stores data in tables (relations) linked by key values.
- DBMS
- Database management system: the software, such as PostgreSQL or MySQL, that stores data and runs SQL.
- SQL
- Structured Query Language, the standard language for querying and changing relational databases.
- Query
- A SELECT statement that asks the database for data and returns a result table.
- Data type
- The kind of value a column holds, such as INTEGER, NUMERIC, VARCHAR or DATE.
- NULL
- A marker meaning a value is missing or unknown. It is not zero and not an empty string.
- Primary key
- The column or columns that uniquely identify each row of a table.
- Foreign key
- A column that must match a primary key in another table, enforcing referential integrity.
- Clause
- A part of a statement that starts with a keyword, such as FROM, WHERE or ORDER BY.
- Predicate
- A condition that is true, false or unknown for each row, such as price > 10.
- Alias
- A temporary name given to a column or table in a query with AS.
- Result set
- The table of rows and columns that a query returns.
The complete sample data
Below is the second half of the setup script: the rows themselves. Run the CREATE TABLE statements above first, then these. Keep this lesson open as a reference while you work through the course; being able to check a result against the raw data is the fastest way to build intuition.
INSERT INTO customers (customer_id, first_name, last_name, email, city, signup_date, referred_by) VALUES
(1, 'Ana', 'Silva', 'ana@example.com', 'Lisbon', '2025-01-05', NULL),
(2, 'Ben', 'Carter', 'ben@example.com', 'London', '2025-01-20', NULL),
(3, 'Chloe', 'Martin', 'chloe@example.com', 'Paris', '2025-02-11', 1),
(4, 'David', 'Kim', 'david@example.com', 'London', '2025-03-02', 1),
(5, 'Elena', 'Rossi', 'elena@example.com', NULL, '2025-03-15', 2),
(6, 'Farid', 'Haddad', 'farid@example.com', 'Lisbon', '2025-04-01', NULL);
INSERT INTO products (product_id, name, category, price) VALUES
(1, 'Notebook', 'Stationery', 4.50),
(2, 'Pen set', 'Stationery', 12.00),
(3, 'Desk lamp', 'Home', 35.00),
(4, 'Coffee mug', 'Home', 9.50),
(5, 'Headphones', 'Electronics', 79.00),
(6, 'USB-C cable', 'Electronics', 15.00),
(7, 'Phone stand', 'Electronics', 22.00);
INSERT INTO orders (order_id, customer_id, order_date, status, shipped_date) VALUES
(101, 1, '2025-03-03', 'shipped', '2025-03-05'),
(102, 2, '2025-03-10', 'shipped', '2025-03-13'),
(103, 1, '2025-03-21', 'shipped', '2025-03-22'),
(104, 3, '2025-04-02', 'cancelled', NULL),
(105, 4, '2025-04-15', 'shipped', '2025-04-18'),
(106, 2, '2025-04-28', 'pending', NULL),
(107, 5, '2025-05-06', 'shipped', '2025-05-08'),
(108, 1, '2025-05-19', 'pending', NULL);
INSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES
(101, 1, 3, 4.50),
(101, 2, 1, 12.00),
(102, 5, 1, 79.00),
(102, 6, 2, 15.00),
(103, 3, 1, 35.00),
(104, 4, 2, 9.50),
(105, 1, 5, 4.50),
(105, 4, 1, 9.50),
(105, 6, 1, 15.00),
(106, 3, 1, 35.00),
(106, 2, 2, 12.00),
(107, 5, 1, 75.00),
(108, 4, 3, 9.50),
(108, 1, 2, 4.50);
Key takeaways
- Each table describes one kind of thing; each row is one instance; each column is one typed fact.
- Choose data types deliberately: NUMERIC for money, DATE for dates, INTEGER for IDs and counts.
- A primary key uniquely identifies each row; a composite key uses several columns together.
- A foreign key must match a primary key elsewhere, so references can never point at missing rows.
- One-to-many relationships use a foreign key on the "many" side; many-to-many relationships need a linking table.
Sources
- PostgreSQL documentation: Constraints
- PostgreSQL documentation: Data Types
- SQLite documentation: Foreign Key Support
This lesson also has interactive exercises. Open the course to try them and track your progress.