12 - Entity-Relationship (ER) Model
2277 words
11 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# 12 - Entity-Relationship (ER) Model ## 🎯 Learning Objectives After reading this topic, you will be able to: - Identify entities, attributes, and relationships from a problem description - Draw ER diagrams with proper symbols (rectangles, diamonds, ovals, lines) - Distinguish between weak and strong entity sets -...

12 - Entity-Relationship (ER) Model
🎯 Learning Objectives
After reading this topic, you will be able to:
- Identify entities, attributes, and relationships from a problem description
- Draw ER diagrams with proper symbols (rectangles, diamonds, ovals, lines)
- Distinguish between weak and strong entity sets
- Model specialization/generalization hierarchies
- Apply cardinality constraints correctly
📋 Prerequisites
- General understanding of what a database is
- 03 - Relational Model — Keys, domains, relations
📖 Core Content
12.1 Intuition: Designing Databases Visually
Before writing SQL CREATE TABLE statements, you need a blueprint. The ER model is a visual design tool that helps you:
- Identify what "things" (entities) exist in your system
- Define what properties (attributes) they have
- Specify how they relate to each other Analogy: Building a house. You wouldn't start laying bricks without an architectural plan. Similarly, don't start creating tables without an ER diagram.
Why This Matters: ER modeling is the most tested topic in DBMS exams (~16% of all questions!). Getting the design right early prevents costly schema changes later.
12.2 ER Diagram Notation
(Diagram)
| Symbol | Meaning | Example |
|---|---|---|
| Rectangle | Entity set | [Student] |
| Diamond | Relationship | {Enrolls} |
| Oval | Attribute | (name) |
| Underlined text | Primary key | (ID) |
| Double oval | Multi-valued attribute | (phone_numbers) |
| Dashed oval | Derived attribute | (age) (derived from DOB) |
| Double rectangle | Weak entity set | [Dependent] |
| Double diamond | Identifying relationship | {Depends_on} |
12.3 Entity Sets
An entity is a distinguishable object in the real world. An entity set is a collection of similar entities (like a table in the relational model).
Examples:
instructor— each instructor is an entitystudent— each student is an entitycourse— each course is an entity
Strong vs. Weak Entity Sets
| Feature | Strong Entity Set | Weak Entity Set |
|---|---|---|
| Has its own primary key | Yes | No (has a discriminator/partial key) |
| Can exist independently | Yes | No — depends on a strong entity |
| Representation | Single rectangle | Double rectangle |
| Identifying relationship | None | Double diamond |
Example: A
loan entity might have loan_number as its own key. But loan_payment depends on loan — it has a payment_number discriminator, but needs loan_number (the owner's key) to be uniquely identified.
Primary key of weak entity set = its discriminator + primary key of the identifying strong entity set.12.4 Attributes
| Attribute Type | Description | Example |
|---|---|---|
| Simple | Atomic, indivisible | salary (a single number) |
| Composite | Can be subdivided | name → first_name, last_name |
| Single-valued | One value per entity | ID (one per student) |
| Multi-valued | Multiple values | phone_numbers (several numbers) |
| Derived | Computed from other attributes | age (from date_of_birth) |
| Stored | Physically stored | date_of_birth |
(Diagram)
12.5 Relationships
A relationship is an association among entities (usually two, but can be three or more).
Degree of a Relationship
| Degree | Number of Entity Sets | Example |
|---|---|---|
| Unary/Recursive | 1 | prereq — a course's prerequisite is another course |
| Binary | 2 | advisor — instructor advises student |
| Ternary | 3 | offers — department offers course in semester |
| n-ary | n | General case |
Cardinality Constraints
Cardinality defines how many entities of one set can be related to entities of another:
| Constraint | Notation | Meaning |
|---|---|---|
| One-to-One (1:1) | [A]----(1)----(1)----[B] | Each A relates to at most one B; each B relates to at most one A |
| One-to-Many (1:N) | [A]----(1)----(N)----[B] | Each A relates to many B; each B relates to at most one A |
| Many-to-One (N:1) | [A]----(N)----(1)----[B] | Each A relates to at most one B; each B relates to many A |
| Many-to-Many (M:N) | [A]----(M)----(N)----[B] | Each A relates to many B; each B relates to many A |
(Diagram)
An instructor can advise many students; a student has at most one advisor.
Participation Constraints
| Constraint | Meaning | Notation |
|---|---|---|
| Total participation | Every entity in the set MUST participate in the relationship | Double line |
| Partial participation | Some entities may not participate | Single line |
Example: Every student MUST have an advisor (total participation of Student). An instructor MAY advise zero students (partial participation of Instructor).
Cardinality Limits
You can specify exact min-max cardinalities:
- (0,1) — at most one (partial)
- (1,1) — exactly one (total)
- (0,N) — zero or many
- (1,N) — at least one, possibly many
12.6 Specialization and Generalization
Specialization (Top-Down)
Taking a general entity and dividing it into sub-entity sets:
(Diagram)
- Inheritance: Sub-entities inherit all attributes of the parent entity
- Overlapping vs. Disjoint: Can an employee be both a Secretary and Engineer? If yes → overlapping; if no → disjoint
- Total vs. Partial: Must every employee be one of these types? If yes → total; if no → partial
Generalization (Bottom-Up)
Combining similar entities into a higher-level entity:
(Diagram)
12.7 Aggregation
Aggregation treats a relationship as a higher-level entity:
(Diagram)
Here, the "Evaluates" relationship (between Instructor and Project) is treated as an entity that participates in the "Uses" relationship with Software.
12.8 Worked Examples
Example 1: University ER Diagram (Partial)
Entities:
instructor(ID, name, dept_name, salary), department(dept_name, building, budget) Relationship: works_in (many-to-one from instructor to department)
(Diagram)Example 2: One-to-One Relationship
A team has exactly one captain. A captain captains exactly one team.
(Diagram)
This 1:1 can be represented as a single table:
team_captain(team_code, team_name, player_num, player_name).Example 3: Weak Entity
(Diagram)
Loanis strong (hasloan_numberas key)Paymentis weak (haspayment_numberas discriminator)- Payment's full key =
payment_number+loan_number(from Loan)
12.9 Ternary and Higher-Order Relationships
Most relationships are binary (between two entity sets). But some require three or more:
Example: A
Supplier supplies a Part to a Project. This is a ternary relationship — all three entities are needed to describe the "supplies" relationship.
(Diagram)
Mapping: Create a separate table with all three primary keys:sqlCREATE TABLE supplies ( supplier_id INTEGER, part_id INTEGER, project_id INTEGER, quantity INTEGER, PRIMARY KEY (supplier_id, part_id, project_id), FOREIGN KEY (supplier_id) REFERENCES supplier(supplier_id), FOREIGN KEY (part_id) REFERENCES part(part_id), FOREIGN KEY (project_id) REFERENCES project(project_id) );
12.10 Complete ER Diagram Example: University Database
Let's build a comprehensive ER diagram for a university:
Entities:
instructor(ID, name, salary) — key: IDstudent(ID, name, tot_cred) — key: IDcourse(course_id, title, credits) — key: course_iddepartment(dept_name, building, budget) — key: dept_namesection(sec_id, semester, year) — weak, discriminator: (sec_id, semester, year) Relationships:instructorworks_fordepartment(N:1)studentbelongs_todepartment(N:1)instructoradvisesstudent(1:N)instructorteachessection(M:N)studenttakessection(M:N, attribute: grade)coursehassection(1:N)sectionis_offered_bycourse(identifying for weak entity) (Diagram) This ER diagram captures the main university domain and would translate to about 5-6 relational tables.
📐 Key Formulas / Concepts
| Concept | Definition |
|---|---|
| Entity | Real-world object distinguishable from others |
| Entity Set | Collection of similar entities |
| Relationship | Association among entities |
| Cardinality | Number of entities that can relate to another entity |
| Weak Entity | Entity that depends on another for its identity |
| Discriminator | Partial key of a weak entity set |
| Specialization | Top-down: dividing entities into sub-types |
| Generalization | Bottom-up: combining entities into super-type |
| Aggregation | Treating a relationship as an entity |
ER Notation Quick Reference
| Symbol | Meaning |
|---|---|
| Rectangle | Strong entity set |
| Double rectangle | Weak entity set |
| Diamond | Relationship |
| Double diamond | Identifying relationship |
| Oval | Attribute |
| Underlined text | Primary key |
| Double oval | Multi-valued attribute |
| Dashed oval | Derived attribute |
| Double line | Total participation |
| Single line | Partial participation |
| Arrow on line | "One" side of relationship |
| Line (no arrow) | "Many" side |
⚠️ Common Pitfalls
Pitfall 1: Confusing Cardinality Direction
The Mistake: Drawing a many-to-one relationship as one-to-many.
Why It's Wrong: The direction matters! "An instructor advises many students" has "many" on the student side.
Memory Aid: Read the relationship from each entity's perspective:
- "Each instructor advises MANY students" → N on student side
- "Each student has ONE advisor" → 1 on instructor side
Pitfall 2: Making Everything an Entity
The Mistake: Creating entities for attributes (e.g., creating a
Color entity instead of a color attribute).
Why It's Wrong: Simple properties should be attributes, not entities.
Rule: If a property has no independent existence and no sub-properties of its own, it's likely an attribute, not an entity.Pitfall 3: Forgetting the Weak Entity's Full Key
The Mistake: Thinking the discriminator alone is the primary key.
Why It's Wrong: A weak entity's full primary key is its discriminator + the identifying strong entity's primary key.
Example:
Payment(payment_number, loan_number, amount, date) — the key is (payment_number, loan_number), not just payment_number.📝 Practice Questions
Q1. What is the difference between a strong entity set and a weak entity set?
Answer
| Strong Entity Set | Weak Entity Set |
|---|---|
| Has its own primary key | Has a discriminator (partial key) only |
| Can exist independently | Depends on identifying strong entity |
| Represented by single rectangle | Represented by double rectangle |
| Example: Student (ID is the key) | Example: Dependent (needs Employee's ID) |
Q2. Explain cardinality constraints in relationships.
AnswerCardinality constraints define how many entities of one set can relate to entities of another:
- 1:1: Each A relates to at most one B (e.g., person - passport)
- 1:N: Each A relates to many B; each B relates to at most one A (e.g., department - employee)
- M:N: Many A relate to many B (e.g., student - course)
The "1" side means total participation if mandatory (double line), partial if optional (single line).
Q3. What is the difference between specialization and generalization?
Answer
- Specialization (top-down): Start with a general entity (e.g., Employee) and divide into sub-types (Secretary, Engineer). Process of defining sub-groupings.
- Generalization (bottom-up): Start with specific entities (Car, Truck) and combine into a higher-level entity (Vehicle). Process of identifying commonalities.
Both are represented the same way in ER diagrams (is-a hierarchy).
Q4. What is a derived attribute? Give an example.
AnswerA derived attribute is computed from other stored attributes. It's not physically stored in the database.Example:ageis derived fromdate_of_birth. Instead of storingage(which changes every year), storedate_of_birthand computeagewhen needed.In ER diagrams, derived attributes are shown with dashed ovals.
Q5. What is a multi-valued attribute? How is it handled in ER diagrams?
AnswerA multi-valued attribute can have multiple values for a single entity. Example: a person can have multiple phone numbers.In ER diagrams, they're shown as double ovals. In the relational model, they're typically handled by creating a separate table for the multi-valued attribute.Example:person(ssn, name)+person_phone(ssn, phone_number)
Q6. What is aggregation in ER modeling?
AnswerAggregation treats a relationship as a higher-level entity so it can participate in other relationships.Example: AnInstructorevaluates aProject. TheEvaluatesrelationship (which involves both Instructor and Project) then participates in aUsesrelationship withSoftware.Aggregation is useful when a relationship itself needs to be related to another entity.
Q7. How do you determine the minimum number of tables needed for a given ER diagram?
AnswerGeneral rules:
- Each strong entity set → 1 table
- Each weak entity set → 1 table (includes the strong entity's key)
- Many-to-many relationship → 1 separate table
- Many-to-one/One-to-many → Add FK on the "many" side; no separate table needed
- One-to-one → Merge into one table (if total participation on both sides)
- Multi-valued attributes → 1 separate table
Q8. In an ER diagram, what do double lines and double diamonds represent?
Answer
- Double line (on relationship): Total participation — every entity in the set MUST participate in the relationship
- Double diamond: Identifying relationship — connects a weak entity set to its identifying strong entity set
- Double rectangle: Weak entity set
Q9. Design an ER diagram for a library with entities: Book (ISBN, title, author), Member (member_id, name), Loan (date_borrowed, date_returned). A member can borrow many books, a book can be borrowed by many members over time.
AnswerEntities:
Book(ISBN, title, author)— key: ISBNMember(member_id, name)— key: member_idRelationship:
borrowsbetween Member and Book (M:N relationship with attributes date_borrowed, date_returned)The M:N relationship requires a junction tableborrows(member_id, ISBN, date_borrowed, date_returned).Diagram(Diagram)
Q10. Explain the difference between total and partial participation with examples.
Answer
- Total participation: Every entity in the set MUST participate. Example: Every student must have an advisor (double line from Student to advisor relationship)
- Partial participation: Some entities may NOT participate. Example: Not every instructor advises a student (single line from Instructor to advisor relationship)
In ER diagrams, total = double line, partial = single line.
Q11. What is a recursive relationship? Give an example.
AnswerA recursive relationship is when an entity set relates to itself. Example: A course can have another course as a prerequisite.Diagram(Diagram)This means: a course has at most one prerequisite, but a course can be a prerequisite for many courses.
Q12. Given the following description, identify entities, attributes, relationships, and cardinalities: "A hospital has many doctors and many patients. Each doctor is assigned to a single department. Each doctor can treat many patients. A patient is treated by exactly one doctor. Each patient has a name, address, phone, and date of birth."
AnswerEntities and attributes:
Doctor(doctor_id, name, specialization)— key: doctor_idPatient(patient_id, name, address, phone, DOB)— key: patient_idDepartment(dept_id, dept_name)— key: dept_idRelationships:
works_in: Doctor → Department (N:1) — many doctors per departmenttreats: Doctor → Patient (1:N) — one doctor treats many patientsCardinalities:
- Department (1) — works_in — (N) Doctor: Each dept has many doctors; each doctor belongs to one dept
- Doctor (1) — treats — (N) Patient: Each doctor treats many patients; each patient treated by one doctor
🔗 Cross-References
- Next Topic: 13 - ER-to-Relational Mapping
- Previous Topic: 11 - Relational Calculus
- Related: BSCS2003 (MAD 1) — Schema design for web applications
- Related: BSMS2001 (BDM) — Business data modeling
- Textbook: Silberschatz, Korth, Sudarshan — Chapter 7 (Database Design and the ER Model) Join Discord Previous11 - Relational CalculusNext13 - ER-to-Relational Mapping