Database Management SystemUnit 211 min read
Entity-Relationship Model: ER Diagrams, Attributes, Relationships & Constraints
Unit 2 of Database Management System introduces the Entity-Relationship (ER) model, a foundational tool for designing databases. This note covers entities, attributes, relationships (1:1, 1:N, M:N), cardinality, weak entities, and ER diagram construction, with real-world examples from Nepali apps (e.g., eSewa, Daraz) a
TAKEAWAYS:
- The ER model visually represents real-world data as entities (objects), attributes (properties), and relationships (interactions) using diagrams.
- Relationships can be 1:1 (one-to-one), 1:N (one-to-many), or M:N (many-to-many), each requiring different database design approaches.
- Weak entities depend on identifying relationships and require a partial key (discriminator) to uniquely identify them.
- Cardinality (min/max participation) defines how many instances of one entity relate to another (e.g., a Customer must place 0 or more Orders).
- ER diagrams translate into relational schemas via normalization (covered in Unit 6), ensuring efficient data storage and queries.
- Real-world applications: eSewa uses ER-like structures to link users, transactions, and service providers; Daraz maps products, orders, and suppliers in M:N relationships.
1. Core Concepts of the ER Model
The Entity-Relationship (ER) model is a high-level conceptual tool for database design. It abstracts real-world data into three key components:
1.1 Entities
An entity is a real-world object that has a distinct identity and can be uniquely identified. Entities can be:
- Strong entities: Exist independently (e.g.,
Student,Book). - Weak entities: Exist only through another entity (e.g.,
Dependentof anEmployee).
Example:
In a Library Management System, Book and Member are strong entities, while Loan (a record of a book borrowed by a member) could be a weak entity dependent on both.
erDiagram
Member ||--o{ Loan : "borrows"
Book ||--o{ Loan : "is_borrowed"
Loan {
int loan_id PK
string date
string return_date
int member_id FK
int book_id FK
}
Member {
int member_id PK
string name
}
Book {
int book_id PK
string title
}Corrected ER diagram showing Loan as a weak entity with foreign keys to Member and Book.1.2 Attributes
Attributes describe properties of entities. They can be:
- Simple vs. Composite: A
Name(composite:FirstName,LastName) vs.Age(simple). - Single-valued vs. Multivalued:
Phone(single) vs.Skills(multivalued for aJobApplicant). - Derived: Calculated from other attributes (e.g.,
Salary=BasePay+Bonus). - Key Attributes:
- Primary Key (PK): Uniquely identifies an entity (e.g.,
StudentID). - Foreign Key (FK): Links to another entity’s PK (e.g.,
MemberIDinLoan).
- Primary Key (PK): Uniquely identifies an entity (e.g.,
Example:
For Member:
| Attribute | Type | Key? |
|---|---|---|
| MemberID | Integer | PK |
| Name | String | |
| String | Unique | |
| MembershipDate | Date |
1.3 Relationships
Relationships define how entities interact. Types:
- 1:1 (One-to-One): One entity relates to exactly one other (e.g.,
PassporttoPerson). - 1:N (One-to-Many): One entity relates to many (e.g.,
AuthortoBook). - M:N (Many-to-Many): Many entities relate to many (e.g.,
StudenttoCourse).
Cardinality specifies minimum/maximum participation:
- Total Participation (Double Line): Every entity must participate (e.g., every
Bookmust belong to aCategory). - Partial Participation (Single Line): Optional participation (e.g., a
Membermay or may not have aLoan).
Example (1:N):
A Professor can teach many Courses, but a Course has only one Professor.
erDiagram
Professor ||--o{ Course : "teaches"
Course {
int course_id PK
string name
int professor_id FK
}2. Weak Entities and Identifying Relationships
A weak entity cannot exist without its owner entity. It requires:
- A partial key (discriminator) to distinguish instances.
- An identifying relationship (double diamond) with the strong entity.
Example: In a Hospital Management System:
Patient(strong entity) has manyAppointments (weak entity).Appointmentdepends onPatientand has a partial key (e.g.,AppointmentID).
erDiagram
Patient ||--o{ Appointment : "has"
Appointment {
int appointment_id PK
string date
string time
int patient_id FK
string status
}
Patient {
int patient_id PK
string name
}Revised to show Appointment as a weak entity with a partial key and foreign key to Patient.Real-World Tie-In:
- eSewa treats
Transactionas a weak entity dependent onUserandServiceProvider. TheTransactionID(partial key) +UserIDuniquely identifies a transaction.
3. Converting ER Diagrams to Relational Schemas
ER diagrams are translated into tables (relations) using these rules:
- Entities → Tables.
- Attributes → Columns.
- Relationships:
- 1:1 or 1:N: Add the PK of the "1" side as a FK in the "N" side.
- M:N: Create a junction table with PKs of both entities.
- Weak Entities: Combine with the strong entity’s PK + partial key as PK.
Example (M:N → Junction Table):
Student and Course have an M:N relationship via Enrollment.
erDiagram
Student ||--o{ Enrollment : "takes"
Course ||--o{ Enrollment : "offers"
Enrollment {
int enrollment_id PK
int student_id FK
int course_id FK
string grade
}
Student {
int student_id PK
string name
}
Course {
int course_id PK
string name
}Corrected junction table with composite primary key and foreign keys.Relational Schema:
Student (StudentID, Name, Email)
Course (CourseID, Title, Credit)
Enrollment (StudentID, CourseID, EnrollmentID, Grade) -- Composite PK
4. Advantages and Limitations of ER Model
| Advantages | Limitations |
|---|---|
| Visual and intuitive for designers. | Does not handle temporal data (history). |
| Supports complex relationships. | Requires conversion to relational model for implementation. |
| User-friendly for non-technical stakeholders. | No direct support for subclasses (handled later in OO databases). |
| Standardized notation (Crow’s Foot, Chen). | Scaling to very large systems can be complex. |
5. Real-World Applications in Nepal
Example 1: eSewa (Digital Payments)
- Entities:
User,ServiceProvider,Transaction. - Relationships:
User1:NTransaction(one user can make many transactions).TransactionM:NServiceProvider(via a junction tablePayment).
- Weak Entity:
Transactiondepends on bothUserandServiceProvider.
Example 2: Daraz (E-Commerce)
- Entities:
Customer,Product,Order,Supplier. - Relationships:
Customer1:NOrder(one customer can place many orders).OrderM:NProduct(viaOrderItemjunction table).Supplier1:NProduct(one supplier provides many products).
Worked Example: Daraz Order Processing
- A
Customer(PK:CustomerID) places anOrder(PK:OrderID). - The
Ordercontains manyOrderItems (junction table withProductIDandQuantity). - The
Productis linked to aSupplier(1:N).
erDiagram
Customer ||--o{ Order : "places"
Order ||--o{ OrderItem : "contains"
OrderItem }|--|| Product : "refers_to"
Product ||--|| Supplier : "supplied_by"
Customer {
int customer_id PK
string name
}
Order {
int order_id PK
string order_date
}
OrderItem {
int order_item_id PK
int order_id FK
int product_id FK
int quantity
}
Product {
int product_id PK
string name
}
Supplier {
int supplier_id PK
string name
}Complete Daraz e-commerce ER diagram with all attributes and relationships.Example 3: NTC (Telecom Billing)
- Entities:
Customer,ServicePlan,Bill,Payment. - Relationships:
Customer1:NBill(one customer gets many bills).Bill1:1Payment(each bill has one payment record).
- Weak Entity:
Paymentdepends onBill.
6. Common Pitfalls and Best Practices
- Avoid Redundancy: Use junction tables for M:N relationships instead of duplicating data.
- Normalize Early: Design ER diagrams with minimal redundancy to ease conversion to 3NF (Unit 6).
- Document Constraints: Clearly mark cardinality (e.g., "a
Loanmust have exactly oneBook"). - Use Real Names: Name entities/attributes based on business terms (e.g.,
Memberinstead ofUserin a library).
7. Step-by-Step: Designing an ER Diagram
Problem: Design a University Admissions System with:
Student(Name, DOB, Contact)Department(Name, Head)Course(Code, Credits)Admission(Date, FeeStatus)
Steps:
- Identify Entities:
Student,Department,Course. - Define Attributes:
Student:StudentID(PK),Name,DOB,Contact.Department:DeptID(PK),Name,Head.
- Define Relationships:
Student1:NAdmission(one student can apply to many departments).Department1:NCourse(one department offers many courses).AdmissionM:NCourse(viaEnrollmentjunction table).
- Draw the ER Diagram:
erDiagram
Student ||--o{ Admission : "applies_to"
Department ||--o{ Course : "offers"
Admission ||--o{ Enrollment : "enrolls_in"
Enrollment }|--|| Course : "takes"
Student {
int student_id PK
string name
}
Admission {
int admission_id PK
int student_id FK
int department_id FK
}
Department {
int department_id PK
string name
}
Course {
int course_id PK
string name
}
Enrollment {
int enrollment_id PK
int admission_id FK
int course_id FK
}Complete academic system ER diagram with all relationships and attributes.8. Exam Tip: How to Score Full Marks
Diagrams Are Mandatory:
- Always draw Crow’s Foot notation for relationships (examiners deduct marks for incorrect symbols).
- Label PKs, FKs, and cardinality clearly.
Answer Past Questions Precisely:
- For Library Management System (common exam question):
- Include entities:
Book,Member,Loan. - Show 1:N (
MembertoLoan) and M:N (BooktoLoanvia junction table). - Add attributes:
BookID(PK),ISBN,Title;MemberID(PK),Name.
- Include entities:
- For Library Management System (common exam question):
Explain Conversions:
- If asked to convert ER to relational schema, show tables + PK/FK constraints.
- Example:
Book (BookID, Title, AuthorID) Member (MemberID, Name, Email) Loan (LoanID, BookID, MemberID, Date) -- Composite PK
Real-World Links:
- Relate your answer to Nepali apps (e.g., "Like Daraz’s
Ordertable, our junction table handles M:N relationships"). - Use traffic routes as an analogy for 1:N (one road has many intersections).
- Relate your answer to Nepali apps (e.g., "Like Daraz’s
Avoid Common Mistakes:
- ❌ Drawing M:N directly without a junction table.
- ❌ Forgetting partial keys for weak entities.
- ❌ Using arrows instead of Crow’s Foot for relationships.
Based on the PU BE Computer (PU) syllabus for Database Management System, unit 2.
Discussion
Loading…