The relational model
Tables, rows, columns, and keys — the simple, powerful idea under every SQL database.
- Describe data as tables of rows and columns
- Explain primary keys and foreign keys
- Read a small schema and see how tables relate
A relational database stores data in tables — like spreadsheets with rules. A table has named columns (each with a type) and any number of rows, where each row is one record. That's the whole foundation, and it's been quietly running the world's data for fifty years.
Tables, rows, columns
The examples in this track use a small online shop. Here's one of its tables — run it to see the rows (it executes on a real SQLite database in your browser):
customers has columns id, name, and country; each row is one customer.
Keys connect tables
Two kinds of key give the model its power:
- A primary key uniquely identifies each row — here,
customers.id. No two customers share an id. - A foreign key points at another table's primary key, linking rows together.
orders.customer_idis a foreign key referencingcustomers.id— it records which customer placed each order.
Notice each order's customer_id matches some customers.id. That relationship
is what lets you later join tables to answer questions like "which customer
placed this order?" — coming up in the intermediate track.
The shop schema
Four tables you'll use throughout: customers, products, orders, and
order_items (the line items linking orders to products). Peek at any of them by
changing the table name in the box above.
Where to go next
You can see whole tables. Next: SELECT — choosing exactly the columns you want.