Database Management SystemUnit 29 min read
ER Model: Entities, Relationships, Cardinalities & Specialization
Unit 2 of Database Management System explores the Entity-Relationship (ER) model, covering entity sets, attributes, relationship types (1:1, 1:N, M:N), cardinalities, weak entities, and generalization/specialization hierarchies with practical examples and ER diagram construction.
TAKEAWAYS:
- The ER model visually represents real-world data as entities, attributes, and relationships using diagrams.
- Cardinalities (1:1, 1:N, M:N) define how many instances of one entity relate to another, critical for database design.
- Weak entities depend on strong entities (e.g.,
Order_Linedepends onOrder) and require partial keys. - Generalization/specialization organizes hierarchical data (e.g.,
Vehicle→Car,Bike) to reduce redundancy. - ER diagrams map directly to relational schemas via mapping rules (e.g., M:N → junction table).
- Constraints (e.g.,
min/max cardinality,overlapping/covering) enforce data integrity in designs.
Core Concepts of the ER Model
The Entity-Relationship (ER) model is a high-level conceptual tool for designing databases. It focuses on:
- Entities: Real-world objects (e.g.,
Customer,Product). - Attributes: Properties of entities (e.g.,
Customer.name,Product.price). - Relationships: Associations between entities (e.g.,
places_order).
1. Entities and Attributes
Entities are nouns representing objects, events, or concepts. Attributes describe their properties.
classDiagram
class Entity {
+name: String
+attributes: List~Attribute~
}
class Attribute {
<<enumeration>>
SIMPLE
COMPOSITE
DERIVED
MULTIVALUED
}
Entity "1" --> "*" Attribute : containsTypes of Attributes:
| Type | Example | Notes |
|---|---|---|
| Simple | Customer.age |
Atomic (indivisible) value. |
| Composite | Customer.address.city |
Can be broken down (e.g., address). |
| Multivalued | Student.skills = {"Python", "SQL"} |
One entity can have multiple values. |
| Derived | Employee.salary_after_tax |
Calculated from other attributes. |
Worked Example: For a hospital management system, identify entities and attributes:
- Entity:
PatientAttributes:patient_id(simple, primary key)name(simple)diagnoses(multivalued: {"Fever", "Hypertension"})age(derived fromdob).
2. Relationships and Cardinalities
Relationships connect entities. Cardinality defines how many instances of one entity relate to another.
Types of Relationships
| Type | Example | ER Diagram Symbol |
|---|---|---|
| 1:1 | Person ↔ Passport |
`---- |
| 1:N | Professor → Course (1 prof teaches many courses) |
`---- |
| M:N | Student ↔ Course (many students take many courses) |
`----} |
| Unary | Employee → Manager (self-referencing) |
`---- |
Mapping Cardinalities to Relational Tables
| ER Relationship | Relational Schema | Example |
|---|---|---|
| 1:1 | Combine into one table or use foreign key. | Passport(..., person_id FK) → Person |
| 1:N | Foreign key in the "N" side. | Course(course_id, prof_id FK) → Professor |
| M:N | Create a junction table. | Enrollment(student_id FK, course_id FK) |
Worked Example: Daraz Order System
- Entities:
Customer,Order,Product - Relationships:
CustomerplacesOrder(1:N: one customer can place many orders).OrdercontainsProduct(M:N: one order can have multiple products, one product can be in multiple orders).
- ER Diagram:
erDiagram Customer ||--o{ Order : places Order ||--o{ Product : contains - Relational Tables:
-- Junction table for M:N CREATE TABLE Order_Items ( order_id INT PRIMARY KEY, product_id INT PRIMARY KEY, quantity INT );
3. Weak Entities and Identifying Relationships
Weak entities cannot exist without a strong entity (e.g., Order_Line depends on Order).
- Require a partial key (e.g.,
line_numberinOrder_Line). - Represented with a double rectangle in ER diagrams.
Example: Ncell Bill Payment
- Strong Entity:
Customer - Weak Entity:
Bill_Payment(depends onCustomerand has a partial keypayment_id). - Relationship:
CustomerhasBill_Payment(1:N).
erDiagram
Customer ||--o{ Bill_Payment : has
Bill_Payment {
payment_id PK
amount
date
}4. Generalization and Specialization
Used to model hierarchical relationships (e.g., Vehicle → Car, Bike).
- Generalization: Bottom-up (specific → general).
- Specialization: Top-down (general → specific).
Constraints:
| Constraint | Description | Example |
|---|---|---|
| Disjoint | Subclasses cannot overlap. | Vehicle → Car or Bike (not both). |
| Overlapping | Subclasses can overlap. | Employee → Manager and Developer. |
| Total | All entities must belong to a subclass. | Animal → Mammal or Bird. |
| Partial | Not all entities need a subclass. | Shape → Circle (optional). |
Example: NEPSE Stock Market
- Superclass:
Trader - Subclasses:
Retail_Trader(disjoint, total)Institutional_Trader(disjoint, total)
- ER Diagram:
erDiagram Trader ||--|{ Retail_Trader : IS_A Trader ||--|{ Institutional_Trader : IS_A
5. Extended ER Features
- Ternary Relationships: Involve 3+ entities (e.g.,
Student,Course,Professorin aTeachesrelationship). - N-ary Relationships: Generalized for N entities.
- Composite Attributes: Grouped attributes (e.g.,
Address→street,city).
Example: Pathao Ride System
- Ternary Relationship:
Driver,Passenger,Ride(all three participate inRide).
erDiagram
Driver ||--o{ Ride : provides
Passenger ||--o{ Ride : takes
Ride {
ride_id PK
distance
fare
}In the Real World
eSewa (Nepal)
- ER Concept: Weak Entities and 1:N Relationships.
- How:
User(strong entity) has manyTransactions(weak entity, depends onUserviauser_id). TheTransactiontable includes a partial key (transaction_id) and foreign keys (user_id,service_id).
Khalti (Digital Payments)
- ER Concept: Generalization/Specialization and M:N Relationships.
- How:
- Superclass:
User(general). - Subclasses:
Merchant,Customer(specialized). - M:N:
User↔Transaction(one user can have many transactions, one transaction involves multiple users like payer/payee).
- Superclass:
NTC (Telecom Billing)
- ER Concept: Ternary Relationships and Derived Attributes.
- How:
- Entities:
Customer,Plan,Usage_Record. - Ternary Relationship:
Customersubscribes toPlanand generatesUsage_Record. - Derived Attribute:
Remaining_Data(calculated asPlan.data_limit - Usage_Record.data_used).
- Entities:
Exam Tip
Diagrams Are Mandatory:
- Always draw an ER diagram for design questions. Label:
- Entities (rectangles).
- Attributes (ovals).
- Relationships (diamonds) with cardinalities (e.g.,
1:N).
- Use crow’s foot notation for clarity.
- Always draw an ER diagram for design questions. Label:
Mapping Rules:
- For M:N, create a junction table with composite primary key.
- For 1:1, decide whether to merge tables or use a foreign key.
Constraints Matter:
- Questions often ask about disjoint/overlapping or total/partial constraints. Use real-world analogies (e.g., "A person can be only a student or an employee" = disjoint, total).
Weak Entities:
- Identify them by asking: "Can this entity exist without another?" If no, it’s weak. Include a partial key in your answer.
SQL Conversion:
- Practice converting ER diagrams to SQL. Example:
-- From ER: Patient (1:N) Prescription CREATE TABLE Patient ( patient_id INT PRIMARY KEY, name VARCHAR(100) ); CREATE TABLE Prescription ( prescription_id INT PRIMARY KEY, patient_id INT REFERENCES Patient(patient_id), medicine VARCHAR(100), dosage VARCHAR(50) );
- Practice converting ER diagrams to SQL. Example:
Common Pitfalls:
- Multivalued attributes: Never store as separate rows; use arrays or separate tables.
- Derived attributes: Document them but don’t store redundantly (e.g.,
agefromdob).
Based on the TU BBA syllabus for Database Management System (IT232), unit 2.
Discussion
Loading…