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

Free lesson

OS 4.1 (3 of 6): Data models and E-R diagrams

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

Who has the book?

A school library has 5,000 books and 800 students. Aisha borrows two books. Sam borrows one.

How can the library know who has which book, and when it is due back?

First you decide what data you need and how the parts connect. You draw it. This drawing is a data model.

Key words

WordWhat it meansExample
DatabaseAn organised store of data that programs can search and change.
Data modelA picture of the data you need and how it is organised.
EntityA thing you keep data about.Student, Book
AttributeOne fact about an entity.name, title
RelationshipA link between two entities.A student borrows a book
Primary keyA value that is different for every row. It finds one row.student_id
Foreign keyA column that holds the primary key of another table.loan.student_id
Data typeThe kind of value a column holds.
IntegerA whole number with no decimal point.42
IndexA list that helps the database find rows fast.
Hierarchical modelData organised as a tree. Each item has one parent.
Relational modelData in tables that are linked by keys.
E-R modelEntity Relationship model. A diagram of entities and their links.
UMLUnified Modelling Language. A standard way to draw designs, including classes.

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

Why draw before you build?

A building needs a plan before the work starts. Data is the same.

A mistake in a drawing is cheap: you change the drawing. A mistake found after you build the database is not. You must change the data too.

A data model shows the data you need and how it is organised.

Check: entity or attribute?

Have a go

Check: entity or attribute?

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

How to read an E-R diagram

Each box is an entity. Each line is a relationship.

The marks at the end of a line say how many. || means exactly one. o{ means zero or many.

In the diagrams below, one student makes zero or many loans. One book is in zero or many loans. This is a one-to-many relationship. The loan table holds both foreign keys.

Three levels of data model

LevelWho it is forWhat it showsLibrary example
ConceptualCustomers and managersThe big picture: entities and relationships. No detail.Student borrows Book
LogicalDesignersTables, attributes and keys. Not tied to one database product.Student(student_id, name, year)
PhysicalDevelopersThe real design for one database: data types, sizes, indexes.student_id INTEGER PRIMARY KEY

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

Conceptual: the big picture

erDiagram
  STUDENT ||--o{ LOAN : makes
  BOOK ||--o{ LOAN : "is in"

Read the big picture

Have a go

Read the big picture

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

Logical: attributes and keys

erDiagram
  STUDENT ||--o{ LOAN : makes
  BOOK ||--o{ LOAN : "is in"
  STUDENT {
    int student_id PK
    string name
    int year
  }
  BOOK {
    int book_id PK
    string title
  }
  LOAN {
    int loan_id PK
    int student_id FK
    int book_id FK
    string due
  }

The model as real tables

Code: sql
SELECT student.name, book.title, loan.due
FROM loan
JOIN student ON loan.student_id = student.student_id
JOIN book ON loan.book_id = book.book_id;

-- Change the query to show only the loans of Sam.

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

Hierarchical and relational models

HierarchicalRelational
ShapeA tree. Each item has one parentTables linked by keys
ExampleFolders on a laptop, a chart of who works for whomStudent, Book and Loan tables
Good atData that is truly a treeData with many links in many directions
Weak atAn item that belongs in two placesNeeds keys and joins to link tables, which take time to learn

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

A hierarchical model

flowchart TD
  school[School] --> y12[Year 12]
  school --> y13[Year 13]
  y12 --> a[Aisha]
  y12 --> b[Ben]
  y13 --> c[Sam]

Match the model

Have a go

Loading…

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

The same idea in UML

classDiagram
  class Student {
    +student_id
    +name
    +year
  }
  class Book {
    +book_id
    +title
  }
  class Loan {
    +due
  }
  Student "1" --> "0..*" Loan : makes
  Book "1" --> "0..*" Loan : is in

Draw a model for a football club

Your local football club has three teams: Under 16, Under 18 and Adults. The club keeps data about its own teams, players and matches. A team has many players. A player is in one team. A team plays many matches against other clubs. The other club is not in this database, so its name is just text. Finish the E-R diagram. Add the attributes and mark the primary key (PK) and foreign key (FK) in each box.

erDiagram
  TEAM ||--o{ PLAYER : has
  TEAM ||--o{ MATCH : plays
  TEAM {
    int team_id PK
    string name
  }
  %% Add the attributes of PLAYER (name, shirt number) and the foreign key to TEAM.
  %% Add the attributes of MATCH (date, other club, our goals, their goals) and its keys (PK and FK).

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

Read the logical diagram

Have a go

Read the logical diagram

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

Exit quiz

Have a go

Exit quiz

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

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.