A road map, a shop and a company
You must store three things: a map of roads, the orders of an online shop, and the staff of a company.
Do you use the same shape for all three? No. A map has many roads meeting at many towns. A company has one boss at the top. A shop has customers, orders and products that all link.
This lesson helps you choose.
Key words
| Word | What it means | Example |
|---|---|---|
| Hierarchical model | A tree where every item has one parent (one item above it). | A company chart |
| Network model | A web where an item can link to many others. | A road map |
| Relational model | Tables linked by keys. | Customers and orders |
| Efficient | Using little time or space. | Finding a row in a second |
| Storage | The space the data takes on a disk. | 3000 characters |
| Complexity | How hard something is to build and understand. | A model with many links |
| Index | A list that helps the computer find a row fast. | Like the index of a book |
| Key | An id in a row that links it to a row in another table. | customer_id |
| Duplicate | The same fact written more than once. | One address in 100 rows |
The cells students fill in are left blank in this preview.
The three models side by side
| Model | Finding one item | Storage | Building it | Best for |
|---|---|---|---|---|
| Hierarchical | Fast if you start at the top and go down | Good, but a shared item is copied | Easy | Folders, company charts |
| Network | Fast along the links | Good, nothing copied | Hard: many links to plan | Maps, where many items link to many items |
| Relational | Fast with keys and an index | Good: each fact stored once | Medium: tables and keys | Shops, schools, banks |
The cells students fill in are left blank in this preview.
Quick check: which model?
Have a go
Quick check: which model?
4 questionsSpeed: how fast you find one item
How fast can the computer find one item?
In a tree you start at the top and go down. This is fast if you know the path. It is slow if you do not. In a relational model the computer uses a key and an index. An index is like the index of a book: it jumps to the right place.
Storage: write each fact once
How much space does the data need?
If the same address is written in 100 rows, that is wasted space. It is also a risk. If the address changes, you must change 100 rows. A relational model stores the address once and points to it with a key.
Worked example: one address, 100 orders
| Way | What is stored | Characters used |
|---|---|---|
| One flat table | 100 orders, each with the 30-character address | 100 times 30 = 3000 |
| Two linked tables | 1 address of 30 characters + 100 customer ids of 3 characters | 30 + 300 = 330 (100 times 3 is 300) |
| Saving | 3000 take away 330 | 2670 characters saved |
| If the address changes | Flat: 100 rows to change. Linked: 1 row to change. | 100 changes against 1 change |
The cells students fill in are left blank in this preview.
Try it: count the characters
orders = 100
address_size = 30
id_size = 3
flat = orders * address_size
linked = address_size + orders * id_size
print("Flat:", flat)
print("Linked:", linked)
print("Saved:", flat - linked)
# Change orders to 10 and run it again. Is the saving still big?
Make a free account to save your work and track your progress.
Your turn: count the saving
A shop stores the address of a customer. Count the characters used by two ways of storing it. The first way writes the address in every order. The second way writes it once and uses an id. Press Run Tests to check your work.
def flat_characters(orders, address_size):
# One table holds every order, and each order row has the full address.
# How many characters are used for addresses?
return 0
def linked_characters(orders, address_size, id_size):
# The address is stored once. Each order holds only the customer id.
# How many characters are used for the address and the ids together?
return 0
def saving(orders, address_size, id_size):
# How many characters does the linked way save?
return 0
Make a free account to save your work, or press Start lab to work through the whole lesson.
Complexity: how hard it is to build
A tree is easy to draw and easy to understand. A network has many links, so it is hard to plan and hard to change. A relational model needs more steps: tables and keys that link them. But most programmers know it well.
A model that is hard to build costs more time and money.
Extra: Foreign keys and joins
A database is an organised store of data in tables. Database people have names for the parts that link tables.
A foreign key is a column that holds the id of a row in another table. In an orders table, customer_id is a foreign key.
A join puts rows from two tables together, by matching the foreign key to the id. So each order can show its address, but the address is stored only once.
A simple way to choose
Match the job to the best model
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: choosing a data model
Press Play on the short film below. It starts with a company, a road map and a shop. Then it asks three things, counts the characters again and ends with a myth.
Film: choosing a data model
Choosing a data model
Every scene, as text
- A road map, a shop and a company. A company has one boss at the top (model: hierarchical, a tree). A map has many roads meeting at many towns (model: network, a web). A shop has customers, orders and products that all link (model: relational, tables linked by keys).
- Ask three things. How fast to find an item (factor: speed). How much space (factor: storage). How hard to build (factor: complexity).
- Worked example: one address, 100 orders. One flat table: 100 × 30 = 3000 (duplicate: one address in 100 rows). Two linked tables: 30 + 300 = 330 (key: 100 customer ids of 3 characters). 3000 − 330 = 2670 characters saved. If the address changes: 100 changes against 1 change.
- Complexity: how hard to build. A tree is easy to draw and easy to understand (building it: easy). A network has many links, so it is hard to plan and hard to change (building it: hard). A relational model needs more steps, but most programmers know it well (building it: medium).
- Myth (false): one model is best for every job. That is not true: each one has a drawback. A tree cannot share an item between two parents (drawback: of the hierarchical model). A family tree: each child has two parents (so: better described as a network). Tables need keys to link them (drawback: of the relational model). Always say why a model fits the job.
There is no perfect model
Warning
Many people think one model is best for every job. No model is, because each one has a drawback. A tree is easy but cannot share an item between two parents. A network is flexible but hard to build. Tables are common but need keys to link them. So always say why a model fits the job.
Your turn: give your advice
A hospital must store patients, doctors and appointments. A patient can see many doctors. A doctor sees many patients. Pick one data model. Write at least six sentences. Say how fast you find an item, how much space it needs and how hard it is to build. Name one drawback of your choice and one model you did not choose.
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.
Final check
Have a go
Final check
5 questionsRemember
Tip
Ask three things: how fast to find an item, how much space, how hard to build. Use a tree for one parent, a network for many links, and tables for most business data.