Sub topic 3
Cardinality Ratios and Participation Constraints
This chapter assumes you know what entities, entity sets, relationships, and attributes are in an ER diagram. It also assumes you know what a primary key is and can read a simple CREATE TABLE statement. Two kinds of constraint decide how entities may be connected: cardinality ratios and participation constraints. GATE questions often ask you to read these from a diagram or a requirements text, and then to count tables or decide where a foreign key goes.
The two constraints at a glance
A relationship set connects entity sets. Two questions can be asked about each entity set that takes part in it.
- How many? How many entities on the other side can one entity be related to? This is the cardinality ratio.
- Must it? Must every entity of the set take part in the relationship at all? This is the participation constraint.
| Constraint | Question it answers | Possible values | Drawn as |
|---|---|---|---|
| Cardinality ratio | Maximum number of partners | 1:1, 1:N, N:1, M:N | Labels 1, N, M on the lines |
| Participation | Minimum number of partners | Total (at least 1) or partial (0 or more) | Double line (total), single line (partial) |
The cardinality ratio gives the maximum number of relationships an entity can have. Participation gives the minimum. Together they say the same thing as the (min, max) notation used in some textbooks.
Cardinality ratios
Take a binary relationship set R between entity sets A and B.
- One-to-one (1:1). Each A entity is related to at most one B entity, and each B entity to at most one A entity. Example: a department has one manager, and an employee manages at most one department.
- One-to-many (1:N). Each A entity may be related to many B entities, but each B entity is related to at most one A entity. Example: one department has many employees, and each employee works in one department.
- Many-to-one (N:1). The mirror image of 1:N. Which one you call it depends only on which entity set you name first.
- Many-to-many (M:N). Each A entity may be related to many B entities and the reverse. Example: a student enrolls in many courses, and a course has many students.
A common mistake is to read the label on the wrong side. In the diagram below, the "N" next to Employee means that one department is related to many employees. It describes how many Employee entities can be attached to one Department entity.
In this Mermaid notation, || means exactly one and o{ means zero or many. The diagram says each employee belongs to exactly one department, and a department may have zero or more employees.
Participation constraints
- Total participation (also called existence dependency). Every entity in the set must take part in at least one relationship. It is drawn as a double line.
- Partial participation. Some entities may take part in no relationship. It is drawn as a single line.
Example: if every employee must belong to a department, then Employee participates totally in Works_In. If a department can exist before any employee is hired, Department participates partially.
Participation is a property of one entity set in one relationship set. The same entity set can be total in one relationship and partial in another.
Combining both constraints
The (min, max) pair for an entity set joins the two ideas.
| Participation | Cardinality side | (min, max) |
|---|---|---|
| Partial | One | (0, 1) |
| Total | One | (1, 1) |
| Partial | Many | (0, N) |
| Total | Many | (1, N) |
In (min, max) notation, the pair is written next to the entity set whose entities are being counted. This differs from the 1/N labels in Chen notation, where the label sits on the opposite side in some books. Check which convention a question uses before you read it.
Why these constraints matter for tables
The constraints decide how a relationship is stored when the diagram is converted to tables. Chapter "Converting an ER Diagram to Tables" gives the full procedure. The summary below is the part that depends on this chapter.
- M:N. The relationship needs its own table. Its primary key is the combination of the keys of both entity sets.
- 1:N. Put the key of the "one" side into the "many" side's table as a foreign key. No extra table is needed.
- 1:1. Put a foreign key on either side. Prefer the side with total participation, so that the column never needs to hold NULL.
- Total participation on the foreign-key side. Declare the foreign key
NOT NULL. This enforces "every employee has a department" in the schema.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_id INT NOT NULL,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);Here NOT NULL on dept_id encodes total participation of Employee. Removing it would make participation partial.
Limits of what a table can enforce
A foreign key can enforce "at most one" and, with NOT NULL, "at least one" from the many side. It cannot easily enforce total participation of the one side. For example, "every department has at least one employee" cannot be written with plain keys, because the department row would have to exist before any employee row can refer to it. That rule needs a trigger, a deferred constraint, or application logic. This is a frequent exam trap.
Worked example
Requirements: a university has instructors and courses. An instructor may teach many courses. Each course is taught by exactly one instructor. Some instructors have no course this term.
- Entity sets: Instructor, Course.
- Relationship Teaches: one instructor to many courses, so the ratio is 1:N (Instructor to Course).
- Course is total in Teaches (each course has exactly one instructor). Instructor is partial (some teach nothing).
- Table design: put
instructor_idin Course as aNOT NULLforeign key. No separate Teaches table is needed.
Total number of tables: 2.
If instead a course could have several instructors, the ratio becomes M:N and the table count becomes 3.
Relationships with attributes
A relationship can carry its own attributes, such as grade on Enrolls between Student and Course. The mapping depends on the ratio.
- In M:N, the attribute goes into the relationship table.
- In 1:N, the attribute can move to the "many" side table.
- In 1:1, it can move to either side.
Higher-degree relationships
A ternary relationship, such as Supplies among Supplier, Part, and Project, has constraints that are more subtle than binary ones. An "N" or "1" on a line says how many entities of that set can occur for a fixed combination of the other entities. A ternary relationship with all three sides N becomes one table whose key combines all three keys. If one side is 1, that side's key is not needed in the primary key.
Common errors
- Reading the ratio label on the wrong side of the line.
- Treating double lines as meaning "many". Double lines mean total participation, not cardinality.
- Making a separate table for a 1:N relationship. It is allowed but unnecessary, and GATE minimum-table counts assume it is merged.
- Forgetting
NOT NULLwhen participation is total. - Assuming a key can enforce total participation of the "one" side.
Exercises
Exercise
Every employee must work in exactly one department. A department may have no employees. Give the cardinality ratio from Department to Employee, the participation of each entity set in Works_In, and the minimum number of tables.
Show answer
The ratio is 1:N. Employee participates totally and Department participates partially. The minimum is 2 tables, with dept_id placed in Employee as a NOT NULL foreign key.
Exercise
In a 1:1 relationship Manages between Employee and Department, Department has total participation and Employee has partial participation. On which table should the foreign key go, and why?
Show answer
On Department, as emp_id of the manager. Department participates totally, so every row has a value and the column can be NOT NULL. Placing it on Employee would leave NULLs for non-managers.
Exercise
Can the rule "every department has at least one employee" be enforced by a foreign key from employee to department alone? Explain.
Show answer
No. A foreign key from Employee to Department guarantees that each employee has a valid department. It says nothing about whether each department has an employee. That rule needs a trigger, a deferred constraint, or application checks.