Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

What databases are used for

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

A shop that never forgets

You buy a hoodie from an online shop. You log in. The shop knows your name. It takes the hoodie out of stock. It remembers your order. Next week it shows you a matching cap.

A database is an organised store of data. One holds all of this memory.

Key words

WordWhat it means
DatabaseAn organised store of data that programs can search and change.
TableA grid in a database. Each row is one record and each column is one fact.
QueryA question you ask a database.
SQLThe language used to ask a database questions and to change its data.
User managementKeeping track of who can log in and what each person may do.
StockHow many of each item you have to sell.
PersonalisationChanging what a page shows to fit one person.
DiagnosticsFinding out what went wrong, using records of what happened.
MillisecondOne thousandth of a second. 1,000 milliseconds is 1 second.
LogA list of events with the time they happened.

The cells students fill in are left blank in this preview.

Reading a query

You seeSay it in words
SELECT name, roleShow me the name and role columns.
FROM usersLook in the users table.
WHERE role = 'admin'Keep only the rows where the role is admin.
ORDER BY stockSort the rows, smallest first.
JOIN products ON ...Put two tables side by side where the ids match.
SUM(...), AVG(...)Add up a column, or find its average.
GROUP BY pageGive one answer for each page.
INSERT, UPDATEAdd a new row, or change a row.
-- some wordsA note for people. The database skips it.

The cells students fill in are left blank in this preview.

Try it: user management

Code: sql
-- Who can log in as an admin?
SELECT name, role FROM users WHERE role = 'admin';

-- Change the query to list all customers.

Why not a text file?

A file is fine for ten names. For ten thousand customers, a database is much better.

A database finds one record fast. It stops two people changing the same data at once. It keeps the data safe if the power fails. It can answer many different questions about the same data.

Quick check

Have a go

Quick check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Four jobs for a database

JobWhat the database holdsExample question
User managementAccounts, roles and passwords (stored scrambled, so nobody can read them)Is this person an admin?
E-commerceProducts, stock, ordersHow many mugs are left?
DiagnosticsLogs of errors and eventsWhich page had errors yesterday?
Performance analysisHow long each page took to loadWhich page is slowest?

The cells students fill in are left blank in this preview.

Try it: stock and orders

Code: sql
-- Which products have fewer than 5 left?
SELECT name, stock FROM products WHERE stock < 5;

-- How many mugs has each customer ordered?
SELECT users.name, SUM(orders.quantity)
FROM orders
JOIN users ON orders.user_id = users.user_id
WHERE orders.product_id = 2
GROUP BY users.name;

Try it: process an order

Code: sql
-- Ben buys one Cap (product 3). Two things must happen: record the order, and take one off the stock.
INSERT INTO orders VALUES (5, 2, 3, 1);
UPDATE products SET stock = stock - 1 WHERE product_id = 3;

-- How many caps are left? It was 2.
SELECT name, stock FROM products WHERE product_id = 3;

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.