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
| Word | What it means | Example |
|---|---|---|
| Data model | A plan for how data is organised and linked. | A tree, a web or tables |
| Hierarchical model | Data in a tree, where every item has one parent. | Folders inside folders |
| Network model | Data in a web, where an item can have many parents. | A map of roads |
| Relational model | Data in tables linked by an id. | A students table and a trips table |
| Parent | The item above another item in a tree. | Bus A is the parent of Sam |
| Child | An item below a parent. | Sam is a child of Bus A |
| Record | One row of facts about one thing. | Sam, 16, trip 1 |
| Key | An 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
Quick check: the tree
Have a go
Quick check: the tree
3 questionsNetwork: 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
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
- 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.
- 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.
- 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.
- 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.
- 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
| Table | Columns | Example row |
|---|---|---|
| trips | id, place, price | 1, Alton Towers, 32.50 |
| students | id, name, trip_id (the key) | 1, Sam, 1 |
| The link | students.trip_id matches trips.id | Sam goes to Alton Towers |
The cells students fill in are left blank in this preview.
Try it: walk down a tree
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.
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
| Student | trip_id in the students table | Place in the trips table | Price in the trips table |
|---|---|---|---|
| Sam | 1 | Alton Towers | 32.50 |
| Aisha | 2 | Science Museum | 8.00 |
| Tom | 1 | Alton Towers | 32.50 |
| Leo | 3 | Cinema | 6.50 |
| Maya | 2 | Science Museum | 8.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
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 questionsRemember
Important
Hierarchical is a tree: one parent. Network is a web: many parents. Relational is tables linked by keys. Same students, three shapes.