Sign up for a club
A school has chess, football and drama clubs. A student can join many clubs. A club has many students.
What must the system remember? The name? The year? Which club? What if someone types year 20? In UK schools, the years go from 7 to 13.
Before you build tables, you write down every fact and every rule. This is the design step.
Key words
| Word | What it means | Example |
|---|---|---|
| Database | An organised store of data that programs can search and change. | |
| Data dictionary | A list of every field in the database, with its type, size and rules. | |
| Field | One fact in a table: one column. | |
| Data type | The kind of value a field holds. | |
| Validation | A check that data is sensible before it is stored. | year must be 7 to 13 |
| Primary key | A field that is different for every row. It finds one row. | |
| Foreign key | A field that holds the primary key of a row in another table. | |
| Entity | A thing the database stores facts about. Each one is a table. | |
| ERD | Entity relationship diagram. A picture of the tables and how they link. | |
| One-to-many | One row in table A links to many rows in table B. | |
| Many-to-many | Many rows in table A link to many rows in table B. |
The cells students fill in are left blank in this preview.
What must the system remember?
Have a go
What must the system remember?
3 questionsThe data dictionary
A data dictionary has one row for each field. Everybody on the team reads the same list. Nobody guesses.
For each field it says the name, the type, the size, if it is required, and the validation rule. Read the one below for the member table. Then answer the quick check.
A data dictionary for member
| Field | Type | Size | Required | Validation | Key |
|---|---|---|---|---|---|
| member_id | Whole number | Up to 6 digits | Yes | Different for every member | Primary key |
| name | Text | Up to 40 characters | Yes | Not empty | |
| year | Whole number | 2 digits | Yes | From 7 to 13 | |
| Text | Up to 80 characters | No | Must contain @ |
The cells students fill in are left blank in this preview.
Quick check: reading the dictionary
Have a go
Quick check: reading the dictionary
3 questionsMany-to-many needs a link table
One student joins many clubs. One club has many students. This is many-to-many.
A database cannot store it in two tables alone. So you add a link table in the middle. Here it is membership. Each row holds one member_id and one club_id.
Now both relationships are one-to-many. One member has many memberships. One club has many memberships.
The ERD for the clubs
erDiagram
MEMBER ||--o{ MEMBERSHIP : has
CLUB ||--o{ MEMBERSHIP : has
MEMBER {
int member_id PK
string name
int year
}
CLUB {
int club_id PK
string club_name
}
MEMBERSHIP {
int member_id FK
int club_id FK
}
The design as real tables
-- Which clubs is Aisha in?
SELECT club.club_name
FROM membership
JOIN member ON membership.member_id = member.member_id
JOIN club ON membership.club_id = club.club_id
WHERE member.name = 'Aisha';
-- Now try: INSERT INTO member VALUES (3, 'Zed', 20);
-- The validation rule refuses it.
Make a free account to save your work and track your progress.
Match each design word to its meaning
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Draw the ERD for a school library
A school library lends books. A student can borrow many books over time. A book can be borrowed by many students over time. Finish the ERD. Add a link table called LOAN with a date. Mark every primary key (PK) and foreign key (FK). Show each relationship as one-to-many.
erDiagram
STUDENT {
int student_id PK
string name
}
BOOK {
int book_id PK
string title
}
%% Add the LOAN table with its two foreign keys and a date.
%% Add the two relationships.
Make a free account to save your work, or press Start lab to work through the whole lesson.
Write a data dictionary for books
Write a data dictionary for the table BOOK in the library. Use a table with these columns: field, type, size, required, validation, key. Include at least five fields, such as book_id, title, author, year_published and copies. Give a sensible validation rule for each one. Say in one line why a data dictionary helps a team.
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.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go