Code of the Day
BeginnerQuerying basics

The relational model

Tables, rows, columns, and keys — the simple, powerful idea under every SQL database.

SQLBeginner8 min read
By the end of this lesson you will be able to:
  • 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 — 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):

SQL — editable, runs 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 uniquely identifies each row — here, customers.id. No two customers share an id.
  • A points at another table's primary key, linking rows together. orders.customer_id is a foreign key referencing customers.id — it records which customer placed each order.
SQL — editable, runs in your browser

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.

Finished reading? Mark it complete to track your progress.

On this page