Database ManagementUnit 312 min read
ER Model: Concepts, Design & Relationships
Unit 3 of Database Management explores the Entity-Relationship (ER) Model, covering entities, attributes, relationships (1:1, 1:M, M:N), cardinality, and ER diagram design—essential for modeling real-world systems like hospital management or e-commerce platforms.
TAKEAWAYS:
- The ER model visually represents data as entities, attributes, and relationships (1:1, 1:M, M:N) to design databases before coding.
- Cardinality (e.g., "one doctor to many patients") defines how entities interact, while modality (optional/mandatory) ensures data integrity.
- Weak entities depend on strong entities (e.g., a
Dependentneeds aPatientto exist) and require an identifying relationship. - ER diagrams convert to relational schemas via normalization (e.g., M:N becomes a junction table).
- Real-world use: ER models power apps like eSewa (user-transaction relationships) and hospital systems (patient-doctor appointments).
- Exam focus: Design ER diagrams for case studies (e.g., banks, universities) and explain relationship types with examples.
1. Core Concepts of the ER Model
The Entity-Relationship (ER) model is a conceptual tool to design databases by breaking real-world systems into:
- Entities: Objects with meaning (e.g.,
Patient,Doctor). - Attributes: Properties of entities (e.g.,
PatienthasPID,Name,Age). - Relationships: How entities interact (e.g.,
DoctortreatsPatient).
Key Definitions
classDiagram
class Entity {
+Name: String
+Attributes: List[Attribute]
+Relationships: List[Relationship]
}
class Attribute {
+Name: String
+Type: DataType (e.g., INT, VARCHAR)
+Key: Primary/Foreign/Composite
}
class Relationship {
+Name: String
+Degree: 1:1, 1:M, M:N
+Cardinality: Min/Max participation
}
Entity "1" --> "0..*" Attribute : contains
Entity "1" --> "0..*" Relationship : participates in2. Entities and Attributes
Entities
- Strong Entity: Exists independently (e.g.,
Doctor). - Weak Entity: Depends on another entity for existence (e.g.,
Appointmentdepends onDoctorandPatient).- Requires a partial key (e.g.,
AppointmentID+DoctorID).
- Requires a partial key (e.g.,
Attributes
| Type | Example | Description |
|---|---|---|
| Simple | Patient.Name |
Atomic value (cannot be broken down). |
| Composite | Patient.Address → City, Zip |
Group of attributes (e.g., Address splits into Street, City). |
| Single-valued | Patient.Age |
One value per entity. |
| Multi-valued | Patient.PhoneNumbers |
Multiple values (e.g., home/work phones). |
| Derived | Patient.AgeIn2030 |
Calculated (e.g., Age + (2030 - CurrentYear)). |
| Key Attributes | Patient.PID |
Primary key (unique identifier). |
Worked Example: Hospital System
- Entity:
Patient- Attributes:
- Simple:
PID(PK),Name,Age - Composite:
Address→Street,City - Multi-valued:
PhoneNumbers - Derived:
InsuranceEligibility(calculated fromIncomeandPolicyID).
- Simple:
- Attributes:
3. Relationships: Types and Cardinality
Degrees of Relationships
| Degree | Example | Description |
|---|---|---|
| Unary | Doctor specializes in Doctor |
Self-referencing (e.g., a doctor’s specialty). |
| Binary | Doctor treats Patient |
Two entities (most common). |
| Ternary | Doctor, Patient, Appointment |
Three entities (e.g., a doctor’s appointment with a patient). |
Cardinality
Defines how many instances of one entity relate to another:
- 1:1 (One-to-One): One doctor has one office.
- 1:M (One-to-Many): One doctor treats many patients.
- M:N (Many-to-Many): Many doctors prescribe many medicines.
Visualizing Cardinality:
erDiagram
Doctor ||--o{ Prescription : "prescribes"
Prescription {
int PK PrescriptionID
string MedicineName
int Dosage
}
Doctor {
int PK DoctorID
string Name
}
Patient {
int PK PatientID
string Name
}
Doctor ||--o{ Patient : "treats" "1:M"Real-World Tie-In: eSewa
- Relationship:
UsermakesTransaction(1:M).- One user (e.g.,
UID: 12345) can make multiple transactions (e.g., bill payments, transfers). - Cardinality: 1 user → M transactions.
- One user (e.g.,
4. Modality (Optional vs. Mandatory)
- Mandatory (Total): Every
Doctormust have anOffice(minimum cardinality = 1). - Optional (Partial): A
Patientmay or may not have anInsurance(minimum = 0).
Example: Ncell Customer-Plan Relationship
- Mandatory: Every
Customermust have onePlan(minimum = 1). - Optional: A
Plancan have zero or moreAdd-ons(e.g., extra data).
5. Weak Entities and Identifying Relationships
Weak Entity: Cannot exist without a strong entity.
- Example:
Appointmentdepends on bothDoctorandPatient.- Partial Key:
AppointmentID(unique only within aDoctor-Patientpair). - Identifying Relationship: The link between
DoctorandPatientis double-lined in ER diagrams.
- Partial Key:
erDiagram
Doctor ||--o{ Appointment : "has"
Patient ||--o{ Appointment : "has"
Appointment {
int PK AppointmentID
date AppointmentDate
string Status
}
Appointment }|--|| Doctor : "identifying"
Appointment }|--|| Patient : "identifying"Real-World Tie-In: Pathao Rider-Order System
- Weak Entity:
OrderItem(depends onOrderandRider).- Partial Key:
ItemID+OrderID(to distinguish between items in the same order).
- Partial Key:
6. Converting ER Diagrams to Relational Schemas
| ER Concept | Relational Schema | Example |
|---|---|---|
| Entity | Table with PK attributes. | Patient(PID, Name, Age) |
| 1:1 Relationship | Merge into one table or separate tables. | Doctor(DoctorID, OfficeID) + Office |
| 1:M Relationship | Foreign key in the "many" side. | Patient(PID, DoctorID) (DoctorID → FK) |
| M:N Relationship | Create a junction table. | Prescription(DoctorID, MedicineID) |
| Weak Entity | Include PK of strong entity + partial key. | Appointment(AppointmentID, DoctorID, PatientID) |
Worked Example: Hospital ER → SQL
-- Entities
CREATE TABLE Doctor (
DoctorID INT PRIMARY KEY,
Name VARCHAR(100),
Specialization VARCHAR(50)
);
CREATE TABLE Patient (
PatientID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT
);
-- 1:M Relationship (Doctor treats Patient)
CREATE TABLE Appointment (
AppointmentID INT PRIMARY KEY,
DoctorID INT,
PatientID INT,
AppointmentDate DATE,
FOREIGN KEY (DoctorID) REFERENCES Doctor(DoctorID),
FOREIGN KEY (PatientID) REFERENCES Patient(PatientID)
);
-- M:N Relationship (Doctor prescribes Medicine)
CREATE TABLE Prescription (
PrescriptionID INT PRIMARY KEY,
DoctorID INT,
MedicineID INT,
Dosage INT,
FOREIGN KEY (DoctorID) REFERENCES Doctor(DoctorID),
FOREIGN KEY (MedicineID) REFERENCES Medicine(MedicineID)
);
7. Advantages and Limitations of ER Model
| Advantages | Limitations |
|---|---|
| ✅ Visual clarity: Easy to understand for non-technical stakeholders. | ❌ No direct implementation: Requires conversion to relational model. |
| ✅ Flexible: Supports complex relationships (M:N, weak entities). | ❌ No handling of constraints: Integrity rules (e.g., triggers) must be added later. |
| ✅ Standardized: Widely used in industry (e.g., ERP systems). | ❌ Scalability: Large systems may need extensions (e.g., UML). |
8. Practical Applications
Case Study 1: Bank Loan System
ER Model:
- Entities:
Customer,Loan,Branch. - Relationships:
Customerapplies forLoan(1:M).Loanis processed byBranch(1:M).
- Weak Entity:
LoanInstallment(depends onLoanandCustomer).
Real-World Use: Nabil Bank’s loan portal uses this to track customer loans and branches.
Case Study 2: University Management System
ER Model:
- Entities:
Student,Course,Professor. - Relationships:
Studentenrolls inCourse(M:N → junction tableEnrollment).Courseis taught byProfessor(1:M).
Real-World Use: TU’s internal system manages student-course enrollments via ER-to-SQL conversion.
## In the Real World
eSewa (Nepal)
- ER Concept Used: 1:M Relationship (
User→Transaction). - How: Each user (e.g.,
UID: ABC123) can make multiple transactions (bill payments, transfers). The system stores:User(UserID, Name, Email)Transaction(TransactionID, UserID, Amount, Date, Type)- Foreign key
UserIDlinks transactions to users.
- ER Concept Used: 1:M Relationship (
Khalti (Digital Wallet)
- ER Concept Used: M:N Relationship (
User↔Merchant). - How: Users can transact with multiple merchants, and merchants accept payments from multiple users. A junction table
Transactionresolves this:CREATE TABLE Transaction ( TransactionID INT PRIMARY KEY, UserID INT, MerchantID INT, Amount DECIMAL, FOREIGN KEY (UserID) REFERENCES User(UserID), FOREIGN KEY (MerchantID) REFERENCES Merchant(MerchantID) );
- ER Concept Used: M:N Relationship (
NTC (Telecom Billing)
- ER Concept Used: Weak Entity (
Billdepends onCustomerandService). - How: A
Billcannot exist without aCustomerand aService(e.g., internet, landline). The partial key isBillID+CustomerID.
- ER Concept Used: Weak Entity (
## Exam Tip
Diagram Design (30% of marks)
- Must-haves:
- Rectangles for entities, ovals for attributes, diamonds for relationships.
- Label cardinality (e.g., "1:M") and modality (e.g., "0..1" for optional).
- Use double lines for identifying relationships (weak entities).
- Common Mistakes:
- Forgetting to mark primary keys (underline or
PK). - Misrepresenting M:N as 1:M (always use a junction table in SQL).
- Forgetting to mark primary keys (underline or
- Must-haves:
Relationship Types (20% of marks)
- Memorize examples:
- 1:1:
Personhas onePassport. - 1:M:
Departmenthas manyEmployees. - M:N:
Studentenrolls inCourse(viaEnrollmenttable).
- 1:1:
- Exam Question: "Give an example of a 1:1 relationship in a hospital system."
Answer:
Doctorhas oneOffice(and vice versa).
- Memorize examples:
ER to SQL Conversion (30% of marks)
- Step-by-Step:
- Convert entities to tables (PK as primary key).
- For 1:M, add FK to the "many" side.
- For M:N, create a junction table with both FKs.
- For weak entities, include the strong entity’s PK + partial key.
- Example Trace:
ER:
DoctortreatsPatient(1:M). SQL:CREATE TABLE Patient (PatientID INT PRIMARY KEY, Name VARCHAR(100)); CREATE TABLE Doctor (DoctorID INT PRIMARY KEY, Name VARCHAR(100)); CREATE TABLE Appointment ( AppointmentID INT PRIMARY KEY, DoctorID INT, PatientID INT, FOREIGN KEY (DoctorID) REFERENCES Doctor(DoctorID), FOREIGN KEY (PatientID) REFERENCES Patient(PatientID) );
- Step-by-Step:
Case Study Questions (20% of marks)
- Approach:
- Identify entities (e.g., in a university system:
Student,Course,Professor). - Draw relationships (e.g.,
Studentenrolls inCourseis M:N). - Note weak entities (e.g.,
Gradedepends onStudentandCourse).
- Identify entities (e.g., in a university system:
- Past Exam Question:
"Design an ER diagram for a Hospital Management System."
Answer Structure:
- Entities:
Patient,Doctor,Department,Appointment. - Relationships:
Doctorworks inDepartment(1:1).DoctortreatsPatient(1:M).Appointmentis a weak entity (depends onDoctorandPatient).
- Entities:
- Approach:
## Quick Revision Checklist
Before the exam, verify you can: ✅ Draw an ER diagram for any given scenario (e.g., library, bank, university). ✅ Distinguish between 1:1, 1:M, M:N relationships with examples. ✅ Convert an ER diagram to SQL tables (including junction tables). ✅ Identify weak entities and identifying relationships. ✅ Explain cardinality and modality with real-world ties (e.g., eSewa, Ncell).
Based on the TU BBM syllabus for Database Management (COM312), unit 3.
Discussion
Loading…