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 (1 of 3): Choosing a data model

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

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

WordWhat it meansExample
Hierarchical modelA tree where every item has one parent (one item above it).A company chart
Network modelA web where an item can link to many others.A road map
Relational modelTables linked by keys.Customers and orders
EfficientUsing little time or space.Finding a row in a second
StorageThe space the data takes on a disk.3000 characters
ComplexityHow hard something is to build and understand.A model with many links
IndexA list that helps the computer find a row fast.Like the index of a book
KeyAn id in a row that links it to a row in another table.customer_id
DuplicateThe 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

ModelFinding one itemStorageBuilding itBest for
HierarchicalFast if you start at the top and go downGood, but a shared item is copiedEasyFolders, company charts
NetworkFast along the linksGood, nothing copiedHard: many links to planMaps, where many items link to many items
RelationalFast with keys and an indexGood: each fact stored onceMedium: tables and keysShops, 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 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

Speed: 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

WayWhat is storedCharacters used
One flat table100 orders, each with the 30-character address100 times 30 = 3000
Two linked tables1 address of 30 characters + 100 customer ids of 3 characters30 + 300 = 330 (100 times 3 is 300)
Saving3000 take away 3302670 characters saved
If the address changesFlat: 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

Code: python
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.

Code: python
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

A flowchart from "Look at your data" to "Does each item have only one parent?", where yes leads to "Hierarchical model" and no leads to "Mostly many-to-many links, such as a map?", where yes leads to "Network model" and no to "Relational model".A flowchart from "Look at your data" to "Does each item have only one parent?", where yes leads to "Hierarchical model" and no leads to "Mostly many-to-many links, such as a map?", where yes leads to "Network model" and no to "Relational model".

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

  1. 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).
  2. Ask three things. How fast to find an item (factor: speed). How much space (factor: storage). How hard to build (factor: complexity).
  3. 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.
  4. 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).
  5. 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 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

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.

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.