Database Design
Senior 4 · ICT
Database Design
Lesson 2 of 5
25 min
A list of students is useful. A list of books is useful. But the library's real question is who has borrowed which book, and that is a connection between the two lists. Before anyone builds tables, a database designer writes down the things to be stored, their properties and, above all, how they are connected, and asks precise questions: can a student borrow more than one book? Can a book be borrowed by more than one student at the same time? The answers decide the design.
Definition
Entity (entity type): a kind of thing, real or abstract, about which data is stored: Student, Book, Class, Payment. Entity instance: one particular example, such as the student Aline Uwase.
Attribute: a property that describes an entity: a student's BirthDate.
Relationship: an association between entities, named with a verb: a Student borrows a Book; a Class has Students.
Cardinality: the number of instances of one entity that can be linked to one instance of the other: one-to-one (1:1), one-to-many (1:N) or many-to-many (M:N).
Participation (optionality): whether every instance must take part in the relationship (mandatory) or may (optional).
| Kind | Meaning | Example |
|---|---|---|
| Simple (atomic) | Cannot be divided further in a useful way | BirthDate, Class |
| Composite | Made of smaller parts that are useful on their own | FullName = FirstName + LastName; Address = Province + District + Sector |
| Single-valued | One value per instance | StudentID |
| Multi-valued | May have several values for one instance | Phone numbers, subjects taught by a teacher |
| Stored | Kept in the database | BirthDate |
| Derived | Calculated from other data whenever needed, so not stored | Age (from BirthDate), a class's number of students |
| Key (identifier) | Its value is different for every instance, so it identifies one instance | StudentID, ISBN, a national ID number |
Why store BirthDate and not Age? Age changes every year by itself; a stored age would quietly become wrong, while an age calculated from the birth date is always right.
Most relationships join two entities (a binary relationship). The picture shows real instances, so you can see what each type means.
| Type | Rule, read both ways | More examples |
|---|---|---|
| One-to-one (1:1) | One A is linked to at most one B, and one B to at most one A | A school has one head teacher, who heads one school; a citizen has one national ID card |
| One-to-many (1:N) | One A is linked to many Bs, but each B to only one A | A province has many districts, each district is in one province; a customer makes many orders |
| Many-to-many (M:N) | One A is linked to many Bs, and one B to many As | Students and subjects; books and authors; teachers and classes |
Always read a relationship in both directions. "A class has many students" alone does not tell 1:N from M:N; you also need "each student is in one class".
A relationship can also link an entity to itself (a unary or recursive relationship): a student mentors other students; a teacher heads a department of other teachers. A relationship among three entities at once (a ternary one) also exists: a teacher teaches a subject to a class.

Cardinality gives the maximum; participation gives the minimum. Designers often write both as a (min, max) pair, as in the diagram above:
Participation turns into rules the database enforces: a mandatory link becomes a field that may not be left empty (NOT NULL).
Press Run. The 1:N relationship "a province has districts" is stored by putting the ProvinceID of its province in every District record. The first query counts the "many" side for each province (3, 5, 8, 7 and 7, a total of 30 districts); the second follows a district to its "one" province. JOIN matches the records whose ProvinceID values are equal.
Worked example 1: decide the type from the rules
For each pair, ask the question in both directions.
| Pair and rule | A to B | B to A | Type |
|---|---|---|---|
| Class and class teacher: "each class has one class teacher, and a teacher is class teacher of at most one class" | one | one | 1:1 |
| Teacher and subject: "a teacher may teach several subjects, and a subject is taught by several teachers" | many | many | M:N |
| Book and copy: "the library owns several copies of a book; each copy is of one book" | many | one | 1:N |
| Student and fee payment: "a student makes many payments; each payment is from one student" | many | one | 1:N |
The same two entities can have different types in different schools. If a school allows each student only one club, Student–Club is 1:N there; if it allows several, it is M:N. The business rule decides, not the names of the entities.
Worked example 2: read the type from the data
A sample of the school's club register:
| Student | Club |
|---|---|
| Aline | ICT |
| Aline | Debate |
| Eric | ICT |
| Grace | Debate |
Aline appears with two clubs, so one student can have many clubs. ICT appears with two students, so one club has many students. Both directions are "many": the relationship is M:N. A sample can prove that a relationship is "many" (one counter-example is enough), but it can never prove "one": that must come from the rules.
Code Darasa: Database Relationships Explained. Watch one-to-one, one-to-many and many-to-many relationships with everyday examples, and notice how each is read in both directions.
Warning
Tip
The two-question test. "Can one A have more than one B?" and "Can one B have more than one A?" Yes–no or no–yes gives 1:N, no–no gives 1:1, yes–yes gives M:N.
Practice
Key points
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
-- One province has many districts; each district is in one province.
CREATE TABLE Province (
ProvinceID INTEGER PRIMARY KEY,
Name TEXT NOT NULL
);
CREATE TABLE District (
DistrictID INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
ProvinceID INTEGER NOT NULL REFERENCES Province(ProvinceID)
);
INSERT INTO Province VALUES
(1, 'City of Kigali'), (2, 'Northern Province'), (3, 'Southern Province'),
(4, 'Eastern Province'), (5, 'Western Province');
INSERT INTO District (Name, ProvinceID) VALUES
('Gasabo', 1), ('Kicukiro', 1), ('Nyarugenge', 1),
('Burera', 2), ('Gakenke', 2), ('Gicumbi', 2), ('Musanze', 2), ('Rulindo', 2),
('Gisagara', 3), ('Huye', 3), ('Kamonyi', 3), ('Muhanga', 3),
('Nyamagabe', 3), ('Nyanza', 3), ('Nyaruguru', 3), ('Ruhango', 3),
('Bugesera', 4), ('Gatsibo', 4), ('Kayonza', 4), ('Kirehe', 4),
('Ngoma', 4), ('Nyagatare', 4), ('Rwamagana', 4),
('Karongi', 5), ('Ngororero', 5), ('Nyabihu', 5), ('Nyamasheke', 5),
('Rubavu', 5), ('Rusizi', 5), ('Rutsiro', 5);
-- The "many" side: how many districts does each province have?
SELECT p.Name AS Province, COUNT(*) AS Districts
FROM District AS d
JOIN Province AS p ON d.ProvinceID = p.ProvinceID
GROUP BY p.ProvinceID
ORDER BY p.ProvinceID;
-- The "one" side: which province is Huye in?
SELECT d.Name AS District, p.Name AS Province
FROM District AS d
JOIN Province AS p ON d.ProvinceID = p.ProvinceID
WHERE d.Name = 'Huye';Quick check
Each copy in a library is a copy of one book, and the library owns several copies of some books. What is the Book–Copy relationship?