Sub topic 1
Entities, Entity Sets, and Relationships
Prerequisites
This chapter assumes you know three things:
- Basic set notation: subsets, union, and the cross product A × B.
- What a table, a row, and a column are.
- Introductory SQL, meaning
CREATE TABLEandSELECT.
You do not need any earlier ER material. This is the first chapter of the tutorial.
Why the ER model comes first
The entity-relationship (ER) model is a way to describe the data a system must store before you choose any tables. You draw the things the business cares about and the links between them. Later chapters turn that drawing into relational tables, add constraints, and then test the tables for redundancy.
GATE CS questions on this topic usually give a short requirements paragraph and ask you to count tables, identify keys, or pick the correct diagram. Every one of those questions starts with identifying the entities and relationships correctly.
Entities and entity sets
An entity is a distinguishable object in the real world. It can be a person, a course, an account, or an event. "Employee Asha" is an entity. "The Compiler Design course" is another.
An entity set is a collection of entities of the same type that share the same kinds of properties. EMPLOYEE is an entity set. Asha, Ravi, and Meena are individual entities that belong to it.
- In a diagram, a rectangle stands for an entity set, not for a single entity.
- A single entity is an instance. An entity set is the type, in the same way a table is the type and a row is the instance.
- Entities in one set are distinguished by their attribute values. The next chapter covers attributes and keys in detail.
An entity set is not always something you can touch. COURSE, ORDER, and BOOKING are entity sets too, because each member is a distinct object the system tracks.
Relationships and relationship sets
A relationship is an association among two or more entities. "Asha works in the Sales department" is a relationship between one EMPLOYEE entity and one DEPARTMENT entity.
A relationship set is a collection of relationships of the same type. Formally, a relationship set R among entity sets E1, E2, ..., En is a subset of the cross product:
R ⊆ E1 × E2 × ... × En
Each element of R is a tuple (e1, e2, ..., en) with each ei in Ei. A diamond in the diagram stands for the relationship set.
This definition has a practical consequence. Because R is a set, the same combination of entities cannot appear twice. Asha can work in Sales only once in a relationship set with no extra attributes.
A runnable view of the definition
The following Python code treats entity sets and a relationship set as ordinary sets.
from itertools import product
employees = {"E1", "E2", "E3"}
projects = {"P1", "P2"}
# The relationship set WORKS_ON is a subset of EMPLOYEE x PROJECT.
works_on = {("E1", "P1"), ("E2", "P1"), ("E2", "P2")}
all_pairs = set(product(employees, projects))
assert works_on <= all_pairs
print("possible pairs:", len(all_pairs)) # 6
print("actual pairs:", len(works_on)) # 3
print("E3 works on nothing:", all(e != "E3" for e, _ in works_on))The output shows that most combinations in the cross product are not relationships. Employee E3 is a member of EMPLOYEE but takes part in no relationship. Whether that is allowed is a participation constraint, which a later chapter covers.
Degree of a relationship set
The degree is the number of entity sets that take part in the relationship set.
| Degree | Name | Example |
|---|---|---|
| 1 | Unary (recursive) | `EMPLOYEE` supervises `EMPLOYEE` |
| 2 | Binary | `EMPLOYEE` works in `DEPARTMENT` |
| 3 | Ternary | `SUPPLIER` supplies `PART` to `PROJECT` |
Binary relationships are by far the most common. A ternary relationship is correct only when the three-way association cannot be rebuilt from three binary ones. In the supplier example, knowing that supplier S supplies part P, that P is used in project J, and that S works for J does not tell you that S supplies P to J.
Roles and recursive relationships
When the same entity set appears more than once in a relationship, each appearance needs a role name. In SUPERVISES, one EMPLOYEE plays the role "supervisor" and the other plays "subordinate". Without role names the diagram is ambiguous, because you could not tell which end is which.
Descriptive attributes on relationships
A relationship set can have its own attributes. These describe the association, not either entity. For example, hours on WORKS_ON records how many hours employee E2 spends on project P2. The value belongs to the pair, so it cannot be stored on EMPLOYEE or on PROJECT alone.
A quick test: if an attribute's value changes when you change the partner entity but the first entity stays the same, it belongs to the relationship.
A small diagram
The diagram below shows two entity sets, one binary relationship set, and one many-to-many relationship set. The symbols on the lines record cardinality, which the third chapter explains. For now, read each line as "these two entity sets are related".
Common errors
- Drawing a single entity as a rectangle. A rectangle is always an entity set.
- Treating a foreign key as part of the ER diagram. In ER you draw a relationship (a diamond), not a key column. Foreign keys appear only after conversion to tables.
- Making an entity set when the thing is really an attribute. A department's name is an attribute. A department itself, with its own manager and budget, is an entity set.
- Forgetting role names in recursive relationships.
- Using a ternary relationship when two binary relationships express the same facts, or the reverse.
Exercise
A university stores students, courses, and the grade each student received in each course. Name the entity sets, the relationship set, its degree, and the attribute that belongs to the relationship set.
Show answer
The entity sets are STUDENT and COURSE. The relationship set is ENROLLED_IN (or TAKES), with degree 2 (binary). The attribute grade is a descriptive attribute of the relationship set, because its value depends on the pair (student, course) and not on the student or the course alone.
Exercise
In a relationship set R among entity sets A, B, and C, with |A| = 4, |B| = 3, and |C| = 2, what is the largest number of relationships R can contain, assuming it has no descriptive attributes?
Show answer
R is a subset of A × B × C, so it has at most 4 × 3 × 2 = 24 tuples.