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

Free lesson

6.10 (2 of 3): Three ways to organise data

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

One school, three shapes

A school plans a trip. There are 5 students on 2 buses.

You can write this as a tree: trip, then buses, then students. The same students also join clubs, and that is a web of links. And when the school runs three trips, it can keep two lists that point at each other.

Each shape is a data model. Each one is good at something different.

Key words

WordWhat it meansExample
Data modelA plan for how data is organised and linked.A tree, a web or tables
Hierarchical modelData in a tree, where every item has one parent.Folders inside folders
Network modelData in a web, where an item can have many parents.A map of roads
Relational modelData in tables linked by an id.A students table and a trips table
ParentThe item above another item in a tree.Bus A is the parent of Sam
ChildAn item below a parent.Sam is a child of Bus A
RecordOne row of facts about one thing.Sam, 16, trip 1
KeyAn id in a row that links it to a row in another table.trip_id in the students table

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

Hierarchical: a tree

A tree: an arrow goes from "Trip: Alton Towers" to "Bus A" and to "Bus B"; from "Bus A" to "Sam", "Aisha" and "Tom"; and from "Bus B" to "Leo" and "Maya".A tree: an arrow goes from "Trip: Alton Towers" to "Bus A" and to "Bus B"; from "Bus A" to "Sam", "Aisha" and "Tom"; and from "Bus B" to "Leo" and "Maya".

Quick check: the tree

Have a go

Quick check: the tree

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

Network: a web of links

In a tree, an item has one parent. In real life that is not always true.

Sam is in the chess club and in the football club. Each club has many students. So Sam has two parents: the two clubs. In a network model an item can have many parents and many children.

Network: students and clubs

A web: arrows go from "Chess club" to "Sam" and "Leo", from "Football club" to "Sam" and "Aisha", and from "Music club" to "Leo" and "Maya".A web: arrows go from "Chess club" to "Sam" and "Leo", from "Football club" to "Sam" and "Aisha", and from "Music club" to "Leo" and "Maya".

Relational: tables with keys

In a relational model the data lives in tables. A table has rows and columns.

You do not write the trip name next to every student. You store the trip once. Each student holds a key, the id of their trip. That key links the two tables.

Match the example to the model

Have a go

Loading…

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

Watch: three ways to organise data

Press Play on the short film below. It shows the same students in three shapes, then one myth.

Film: three ways to organise data

Three ways to organise data

Every scene, as text

  1. One school, three shapes. 5 students on 2 buses: trip, then buses, then students (shape: a tree). The same students also join clubs (shape: a web of links). When the school runs three trips, it can keep two lists that point at each other (shape: tables). Each shape is a data model: a plan for how data is organised and linked.
  2. Hierarchical: a tree. Trip: Alton Towers, with arrows down to Bus A and Bus B. Bus A, with arrows down to Sam, Aisha and Tom (Bus A: parent of Sam, Aisha and Tom; Sam, Aisha and Tom: children of Bus A). In a tree, an item has one parent.
  3. Network: a web of links. Chess club and Football club (the clubs: parents of many students), each with an arrow to Sam (Sam: child of two clubs). An item can have many parents and many children.
  4. Relational: tables with keys. A table has rows and columns. A customers table: id, name. An orders table: id, customer_id (key: links it to a row in another table), with an arrow to the customers table's id. The id links the two tables.
  5. Myth (false): a network model is a computer network. That is not true: a network model is a data model. A map where many roads meet at one town (network model: an item has many links).

Relational: two tables

TableColumnsExample row
tripsid, place, price1, Alton Towers, 32.50
studentsid, name, trip_id (the key)1, Sam, 1
The linkstudents.trip_id matches trips.idSam goes to Alton Towers

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

Try it: walk down a tree

Code: python
trip = {
    "place": "Alton Towers",
    "buses": {"Bus A": ["Sam", "Aisha"], "Bus B": ["Leo"]},
}

print(trip["place"])
for bus in trip["buses"]:
    print(bus, "has", trip["buses"][bus])

# Add "Maya" to Bus B and press Run again.

Make a free account to save your work and track your progress.

Your turn: walk the tree

The trip is stored as a tree. The trip has buses. Each bus has a list of students. Write three functions that walk down the tree. Press Run Tests to check.

Code: python
trip = {
    "place": "Alton Towers",
    "buses": {
        "Bus A": ["Sam", "Aisha", "Tom"],
        "Bus B": ["Leo", "Maya"],
    },
}


def count_students(trip):
    # Count every student on every bus.
    return -1


def bus_of(trip, student):
    # Give back the name of the bus the student is on.
    # If the student is not on the trip, give back None.
    return ""


def students_on(trip, bus):
    # Give back the list of students on one bus.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Follow the link

Studenttrip_id in the students tablePlace in the trips tablePrice in the trips table
Sam1Alton Towers32.50
Aisha2Science Museum8.00
Tom1Alton Towers32.50
Leo3Cinema6.50
Maya2Science Museum8.00

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

Your turn: who pays more?

Look at the table Follow the link in the lesson. The trips table gives the place and the price of each trip. The students table has a trip_id for each student. Write the names of the students whose trip costs more than 7 pounds, and the place each one goes to. Then say in one sentence why we store the place once and not next to every student.

A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.

Make a free account to save your work, or press Start lab to work through the whole lesson.

Extra: Primary key, foreign key and join

A database is an organised store of data in tables. Database people use three more words for this.

A primary key is a value that is different in every row. The id of a trip is its primary key.

A foreign key is a column that holds the primary key of a row in another table. trip_id is a foreign key.

A join puts rows from two tables side by side by matching the keys. A database does it with SQL (a language for asking a database questions).

Extra: Try it: a join in SQL

Code: sql
SELECT students.name, trips.place
FROM students
JOIN trips ON students.trip_id = trips.id;

-- Add: ORDER BY trips.place; at the end and run it again.

Make a free account to save your work and track your progress.

Final check

Have a go

Final check

5 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

Remember

Important

Hierarchical is a tree: one parent. Network is a web: many parents. Relational is tables linked by keys. Same students, three shapes.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.