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
| Word | What it means | Example |
|---|---|---|
| Database | An organised store of data that programs can search and change. | |
| Data model | A picture of the data you need and how it is organised. | |
| Entity | A thing you keep data about. | Student, Book |
| Attribute | One fact about an entity. | name, title |
| Relationship | A link between two entities. | A student borrows a book |
| Primary key | A value that is different for every row. It finds one row. | student_id |
| Foreign key | A column that holds the primary key of another table. | loan.student_id |
| Data type | The kind of value a column holds. | |
| Integer | A whole number with no decimal point. | 42 |
| Index | A list that helps the database find rows fast. | |
| Hierarchical model | Data organised as a tree. Each item has one parent. | |
| Relational model | Data in tables that are linked by keys. | |
| E-R model | Entity Relationship model. A diagram of entities and their links. | |
| UML | Unified 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 questionsHow 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
| Level | Who it is for | What it shows | Library example |
|---|---|---|---|
| Conceptual | Customers and managers | The big picture: entities and relationships. No detail. | Student borrows Book |
| Logical | Designers | Tables, attributes and keys. Not tied to one database product. | Student(student_id, name, year) |
| Physical | Developers | The 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 questionsLogical: 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
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
| Hierarchical | Relational | |
|---|---|---|
| Shape | A tree. Each item has one parent | Tables linked by keys |
| Example | Folders on a laptop, a chart of who works for whom | Student, Book and Loan tables |
| Good at | Data that is truly a tree | Data with many links in many directions |
| Weak at | An item that belongs in two places | Needs 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 questionsExit quiz
Have a go