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

Preview

AC 2.8: Tables, keys and the shape of a schema

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

60 minutes

The plan before the building

Nobody builds a house without a plan. A school wants to store pupils, forms and clubs. If it starts typing before it plans, the same facts end up in five places.

The plan for a store of data is a picture, and the facts it needs are the keys that link the parts.

Key words

WordWhat it means
Entity relationship diagrama picture of the tables in a database and how they link, called an ERD
Databasean organised store of tables that you can ask questions
Schemathe plan of a database: its tables, fields and links
Entitya thing we keep facts about, such as a pupil; it becomes a table
Recordone row: all the facts about one entity
Attributeone fact about an entity; it becomes a field, or column
Primary keythe field that gives every record its own unique id
Foreign keya field that holds the primary key of a record in another table
Composite keya primary key made of two or more fields together

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

By the end of this lesson

Note

I can say what a schema and an ERD are for, and name tables, records, fields and the three kinds of key.

Starter

Have a go

Starter

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.

Same idea, different words

In a diagramIn the databaseExample
EntityTablepupil
One instance of an entityRecord, or rowAmy, form 1
AttributeField, or columnname
Primary keyThe field that is uniquepupil_id
Foreign keyA field that points to another tableform_id in pupil
Composite keyTwo fields that are unique togetherpupil_id with club_id

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

Try it: read a real schema

Code: sql
-- The tables this schema made.
SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;

-- A record from pupil. form_id is the foreign key.
SELECT * FROM pupil;

-- membership has a composite key: pupil_id and club_id together.
SELECT * FROM membership;

Why three kinds of key

A primary key must be unique, so each record can be found. A foreign key links a record to one in another table, so a fact is stored once. In the pupil table, form_id points at the form table.

A composite key uses two fields together. In membership, Amy can join Chess and Choir, and Chess can have Amy and Bo. Only the pair is unique.

How the exam draws keys

Important

On paper, underline each primary key and put a star after each foreign key, such as form_id*. The picture above uses the tags PK and FK because that is how the drawing tool shows them.

The lesson goes on

5 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.