Core CS · DBMS
How many, and must there be any at all?
One edge of an ER diagram answers two separate questions, in two separate places. Get them the wrong way round and the tables you build are wrong in a way no amount of careful SQL recovers.
Draw the links, and watch a rule refuse one →01 The idea
Two questions about the same edge
An entity set is a collection of things of one kind: every student in the college, every final-year project. A relationship set is what connects two of them. Draw a line from STUDENT to a diamond and another from the diamond to PROJECT, and you have said that students and projects can be linked. You have not yet said anything about how many, or about whether the link is optional.
The diagram answers both, with two labels that sit in different places and mean different things. One is a number written beside each edge, and it caps how many entities may be attached. The other is the thickness of the line, and it decides whether an entity is allowed to have nothing attached at all. Interviewers separate the two on purpose, because a student who has memorised the shapes uses one word for both and gets caught in the follow-up.
Everything in this lesson lives in one college. Three students, S1 Asha, S2 Bina and S3 Chetan. Two final-year projects, P1 Solar Tracker and P2 Chat App. One relationship set, WORKS_ON, between them. Every rule you meet is checked against those five entities, by hand, one pair at a time.
02 Worked example
One relationship set, counted twice
WORKS_ON records which student works on which final-year project. Read the five nodes left to right. Node 3 is highlighted because it is the thing the other four are measuring, and the two counts that follow it are the two questions from section 01.
Nodes 4 and 5 count two different things over the same three pairs, and that is the whole difficulty of this module. Node 4 asks how many times one entity is allowed to appear, and answers with a number beside each edge. Node 5 asks whether an entity is allowed to appear zero times, and answers with the thickness of the line. Shrink the set to {(S1,P1), (S2,P1)} and node 4 changes: N:1 is now enough, because every student has one project while Solar Tracker has two students. Node 5 does not change at all: Chetan still appears nowhere, so participation is still partial. One warning about node 4 before you use it in an exam. Counting the rows tells you which ratios a set is consistent with, not which one the college has chosen: that shrunken set is legal under N:1 and equally legal under M:N. The ratio is a rule about what may be recorded tomorrow, so it comes from the college. Counting is how you check that rule, not how you invent it.
There is one more fact hiding in node 3 that neither count picks up. Asha spends 6 hours a week on Solar Tracker and 4 on Chat App, and Bina spends 10 on Solar Tracker. Those numbers do not belong to Asha, who has two of them. They do not belong to Solar Tracker, which also has two of them. They belong to the pair, and section 03 is where they get somewhere to live.
03 Mechanics
Degree, ratio, participation: three labels on one edge
A relationship set carries three independent labels, and you can get any one of them right while getting the others wrong. Degree says how many entity sets take part. The cardinality ratio caps how many entities may appear on each side. Participation says whether an entity may appear zero times. Take them in that order, against the same college.
| Degree | In our college | What it needs that a binary does not | Where it lands in tables |
|---|---|---|---|
| Unary, also called recursive one entity set |
MENTORS on STUDENT: (S1, S3), Asha mentors Chetan |
Role names. Both edges leave STUDENT, so nothing in the picture says which end is the mentor and which is the mentee. The pair is ordered; the drawing is not. | One table with two foreign keys into student. The role names become the column names, because that is the only thing telling the two keys apart. |
| Binary two entity sets |
WORKS_ON between STUDENT and PROJECT |
Nothing. This is the default, and almost every relationship you will ever draw is binary. | A foreign key when the ratio is 1:N or N:1, a table of its own when it is M:N. |
| Ternary three entity sets |
SUPERVISES(STUDENT, PROJECT, FACULTY): F1 supervises Asha on Solar Tracker |
An argument that three binary relationships would not say the same thing. Usually they would not, and you should be ready to show why. | One table holding three foreign keys, not three tables holding two each. |
Now the ratio, checked against the exact set from section 02: W = {(S1,P1), (S1,P2), (S2,P1)}. Four counts decide every row below. S1 appears twice, S2 once, P1 twice, P2 once.
| Ratio, STUDENT : PROJECT | What it permits | Where the labels go | Is W legal? |
|---|---|---|---|
| 1:1 | One project per student and one student per project. | 1 beside STUDENT, 1 beside PROJECT |
No. S1 appears twice and P1 appears twice. The largest legal subset holds two pairs, for example {(S1,P2), (S2,P1)} |
| 1:N | Many projects per student, one student per project. | 1 beside STUDENT, N beside PROJECT |
No. (S1,P1) and (S1,P2) are fine. (S2,P1) is not, because P1 already has S1 |
| N:1 | One project per student, many students per project. | N beside STUDENT, 1 beside PROJECT |
No. (S1,P1) and (S2,P1) are fine. (S1,P2) is not, because S1 already has P1 |
| M:N | Many each way. | M beside STUDENT, N beside PROJECT |
Yes. All three pairs stand |
Three notations are in circulation and they do not look alike. This lesson uses the Chen and Elmasri convention, which is what Indian university syllabi and placement interviews are set from: a number written beside each edge. Silberschatz writes no numbers and draws an arrowhead into the entity set that is capped at one, so an arrow into PROJECT means one project per student. Crow’s foot draws the three-pronged symbol at the end of the line touching the entity that can be many, which carries the same information as the letter N in the same position. Never read a number off a diagram without first checking which of the three you are looking at.
Participation is the second label and it sits on the same edge. A single line from the entity set to the diamond is partial: an entity of that set may appear in zero relationship instances. A double line is total: every entity of that set must appear in at least one. In W, Chetan appears in none, so STUDENT is partial. Chat App has Asha, and Solar Tracker has Asha and Bina, so PROJECT could be drawn total. Those two facts are independent of each other and independent of the M:N above them.
| The question | Cardinality ratio | Participation constraint |
|---|---|---|
| What it asks | At most how many? | At least one, or none at all? |
| What it constrains | The maximum links an entity may have | The minimum links an entity must have, which is 0 or 1 |
| Drawn as | 1, N or M beside each edge | single line = partial, double line = total |
| On our set W | S1 has 2 links and P1 has 2, so neither maximum is 1 | S3 has 0 links, so STUDENT cannot be drawn total |
| Change it and | you change where the data can be stored at all: a foreign key, or a whole extra table | you change whether a column may be NULL, not whether the table exists. Under 1:1 it also decides which of the two tables holds the key |
Both labels turn into DDL, and the M:N case is where the relationship finally gets a place to keep its own attributes. Line 7 is the reason this table has to exist: hours is a fact about the pair, and a pair is the only thing in the schema that has one row per pair. Line 1 declares STUDENT total, and that is a ruling the college has taken rather than something W already satisfies: Chetan is on no project yet. Step 7 of the console is that same ruling being tested against the links that survived.
-- STUDENT M : N PROJECT · STUDENT total · PROJECT partialCREATE TABLE student (sid TEXT PRIMARY KEY, name TEXT NOT NULL);CREATE TABLE project (pid TEXT PRIMARY KEY, title TEXT NOT NULL);CREATE TABLE works_on ( -- the relationship set, as a table sid TEXT NOT NULL REFERENCES student(sid), pid TEXT NOT NULL REFERENCES project(pid), hours INT NOT NULL, -- a fact about the PAIR, not about either end PRIMARY KEY (sid, pid) -- a set, not a bag: one link per pair);-- Make the same edge STUDENT 1 : N PROJECT and works_on disappears:-- ALTER TABLE project ADD sid TEXT REFERENCES student(sid); -- FK on the N side-- ...NOT NULL as well, if PROJECT's participation is total-- STUDENT's own total participation has no column to mark. See section 06.
Line 8 is the line people leave out. A relationship set is a set, so (S1, P1) can hold or not hold, and cannot hold twice. Without that composite key the table is a bag, and a bag is a shape no ER diagram can describe. Lines 11 and 12 are the same edge under a different ratio: one label on a drawing is the difference between two tables and three.
There is a third notation that carries both labels at once. (min, max) writes one pair on each edge, and the pair constrains the entity set it sits beside: STUDENT (1, N) says every student takes part in at least one and at most N relationship instances. min > 0 is total participation, min = 0 is partial. It is compact, and it hides a trap. Take STUDENT 1 : N PROJECT with both sides total. In ratio form that is 1 beside STUDENT and N beside PROJECT. In (min, max) form it is STUDENT (1, N) and PROJECT (1, 1). The two maxima have swapped edges, because the ratio label counts the set it sits beside while the (min, max) label counts the instances that set takes part in. A diagram that mixes the two reads backwards, and so does a candidate who learned one and is shown the other.
05 Cheat sheet
Every label on one card
Nine rows, and each one is a thing an interviewer can ask you to draw and then ask you to turn into a table. The last column is the half most candidates skip.
| Label | What it says | Drawn as | What it becomes in tables |
|---|---|---|---|
| 1:1 | At most one each way. | 1 beside both edges | One table, or a foreign key plus UNIQUE. Put the key on whichever side is total. |
| 1:N STUDENT : PROJECT | Many projects per student, one student per project. | 1 beside STUDENT, N beside PROJECT | A foreign key on PROJECT, the N side. Never on the 1 side. |
| N:1 | One project per student, many students per project. | N beside STUDENT, 1 beside PROJECT | The same shape read from the other end, so the key goes on STUDENT. |
| M:N | Many each way. | M beside STUDENT, N beside PROJECT | Its own table holding both keys, with PRIMARY KEY (sid, pid). |
| Total participation | Every entity of this set appears in at least one instance. | double line on that edge | NOT NULL on the foreign key, but only when the key sits on that same table. |
| Partial participation | An entity may appear in none. | single line, and it is the default | Nothing to declare. This is what you get by saying nothing. |
| (min, max) | Both constraints, on the edge of the set they constrain. | (1, N) beside STUDENT | min > 0 is total. The max is the ratio, read from the opposite edge to where you expect it. |
| Relationship attribute | A fact about the pair, not about either end. | an oval hanging off the diamond | A column of the M:N table. Where else it could live depends on the ratio. |
| Role name | Which end of the edge this is. | written on the edge itself | The column name, and the only thing that makes a recursive relationship readable. |
06 Where & why
What each label turns into on a real engine
None of these labels stays a drawing. Each one becomes a line of DDL on a system that is running right now, and an interviewer can hear the difference between a candidate who has typed them and one who has only seen the diamond.
STUDENT 1 : N PROJECT becomes one column, project.sid REFERENCES student(sid), on the N side. Change that one label to M:N and the column cannot hold two values any more, so it becomes works_on(sid, pid, hours) with PRIMARY KEY (sid, pid). A single letter on a drawing is the difference between two tables and three.
Total participation is NOT NULL only when the foreign key sits on that same table. “Every student must be on at least one project” is a rule about rows that are not in the student table, and CREATE ASSERTION, the standard SQL feature that would state it directly, is implemented by none of the mainstream engines. It ends up as a trigger, a check run inside the transaction, or a nightly report somebody has to read.
PRIMARY KEY (sid, pid) on an M:N table is not a performance decision. It is the line that stops Asha being recorded on Solar Tracker twice with two different hour counts. A relationship set is a set; without that key the table is a bag, and a bag is a shape the diagram has no way to describe or to reject.
A recursive relationship maps to two foreign keys into one table: mentor_sid and mentee_sid, both REFERENCES student(sid). The role names on the diagram are the column names, which is why an unlabelled recursive edge is unreadable rather than merely untidy. Add CHECK (mentor_sid <> mentee_sid) unless a student is allowed to mentor themselves.
07 Interview questions
What they actually ask
ER modelling is where a DBMS interview stops being definitions and starts being design. The two questions almost everyone gets asked are the difference between cardinality and participation, and how an M:N relationship becomes tables. Both are below, and so are the follow-ups that come after a confident answer.
What is a relationship set, and how is it different from a relationship instance?
What is the degree of a relationship?
Why does a recursive relationship need role names?
In a 1:N relationship, which side is the many side?
What is the difference between a cardinality ratio and a participation constraint?
How is total participation drawn, and what does it become in SQL?
What is (min, max) notation and why would you use it?
When does a relationship need an attribute of its own?
Can a ternary relationship always be replaced by three binary ones?
How does an M:N relationship become tables?
You are five minutes into whiteboarding a schema. Which of these labels actually change the tables you end up with?
08 Practice problems
Six to work through
Say every edge out loud as two sentences before you write a single number. The people who get these wrong are almost never confused about the concept; they wrote the ratio down in the opposite order to the one they read it in.