Database Management SystemUnit 211 min read
Entity-Relationship (ER) Model & Database Design
Unit 2 of Database Management System: Covers ER modeling fundamentals, entity-relationship diagrams, cardinality constraints, database design principles, and real-world database architecture with practical examples and visuals.
TAKEAWAYS:
- ER diagrams visually represent real-world entities, attributes, and relationships in a database schema.
- Cardinality (1:1, 1:N, M:N) defines how entities relate, while participation (mandatory/optional) clarifies constraints.
- Weak entities rely on parent entities for identification (e.g.,
Order→Customer). - Database design follows steps: requirement analysis → conceptual design (ERD) → logical design → physical implementation.
- Normalization (1NF, 2NF, 3NF) eliminates redundancy after ER modeling.
- Real-world systems (eSewa, Daraz) use ER concepts to model transactions, users, and inventory.
1. Introduction to Entity-Relationship (ER) Modeling
ER modeling is the first step in database design, translating real-world data into a structured schema. It uses entities (objects), attributes (properties), and relationships (connections) to represent business logic.
Key Definitions
- Entity: A distinct object (e.g.,
Customer,Product,Order). - Attribute: Property of an entity (e.g.,
Name,Email,OrderDate). - Primary Key: Unique identifier for an entity (e.g.,
CustomerID). - Relationship: Association between entities (e.g.,
CustomerplacesOrder).
ER Diagram Symbols
erDiagram
ENTITY "Customer" ||--o{ "Order" : "places"
ENTITY "Order" ||--|| "Product" : "contains"
ENTITY "Customer" {
string custID PK
string name
}
ENTITY "Order" {
string orderID PK
date orderDate
}
ENTITY "Product" {
string prodID PK
string name
decimal price
}ER diagram with attributes included for clarity (1:M and M:1 relationships)
- Rectangle: Entity (e.g.,
Customer). - Oval: Attribute (e.g.,
Name). - Diamond: Relationship (e.g.,
places). - Crow’s Foot: Cardinality (1:N, M:N).
2. Entities and Attributes
Entity Types
- Strong Entity: Independent existence (e.g.,
Employee). - Weak Entity: Depends on another entity (e.g.,
Orderdepends onCustomer).
Example: HR Database
erDiagram
ENTITY "Employee" {
string EmpID PK
string FirstName
string LastName
decimal Salary
string DeptID FK
}
ENTITY "Department" {
string DeptID PK
string DeptName
string LocationID FK
}Key Points:
EmpIDis the primary key forEmployee.DeptIDis a foreign key linking toDepartment.- Weak entities (e.g.,
Order) are shown with a diamond and dashed line.
3. Relationships and Cardinality
Types of Relationships
| Type | Description | Example |
|---|---|---|
| One-to-One | One entity relates to exactly one. | Driver → Vehicle (1:1) |
| One-to-Many | One entity relates to many. | Customer → Order (1:N) |
| Many-to-Many | Many entities relate to many. | Student → Course (M:N) |
Cardinality Notation
erDiagram
ENTITY "Student" ||--o{ "Enrollment" : "takes"
ENTITY "Course" ||--o{ "Enrollment" : "offers"
ENTITY "Enrollment" {
string enrollID PK
string studentID FK
string courseID FK
date enrollDate
}Added Enrollment entity to show M:N relationship with attributes- 1:N: One
Studenttakes manyCourses(viaEnrollment). - M:N: Resolved via a junction entity (
Enrollment).
Participation Constraints
- Mandatory (Total): Every entity must participate (e.g.,
Employeemust have aDeptID). - Optional (Partial): Participation is optional (e.g.,
Ordermay not have aDiscount).
4. Weak Entities and Identifying Relationships
Weak entities cannot exist without a parent entity and require a partial key (e.g., Order needs OrderID + CustomerID).
Example: Daraz Order System
erDiagram
ENTITY "Customer" {
string CustID PK
string Name
string Contact
}
ENTITY "Order" {
string OrderID PK
string CustID FK
date OrderDate
decimal TotalAmount
}Added missing attributes (Contact, TotalAmount) for real-world relevanceOrderis weak; it cannot exist withoutCustomer.- Identifying relationship:
Customer→Order(dashed line).
5. Database Design Process
- Requirement Analysis: Gather business rules (e.g., "A
Customercan place multipleOrders"). - Conceptual Design: Draw ERD (e.g.,
Customer→Order). - Logical Design: Convert ERD to relations (tables).
- Physical Design: Choose DBMS (e.g., MySQL, PostgreSQL).
Worked Example: Bank Branch Database
Requirements:
- Each
Bankhas multipleBranches. - Each
Branchhas multipleAccountsandLoans.
erDiagram
ENTITY "Bank" {
string BankID PK
string Name
string Headquarters
}
ENTITY "Branch" {
string BranchID PK
string BankID FK
string Location
string BranchManager
}
ENTITY "Account" {
string AccountID PK
string BranchID FK
decimal Balance
string AccountType
}
ENTITY "Loan" {
string LoanID PK
string BranchID FK
decimal Amount
date StartDate
}Added realistic attributes (Headquarters, BranchManager, AccountType) and dates Key Observations:
Branchis a weak entity (depends onBank).AccountandLoanare strong entities linked viaBranchID.
6. Normalization (Post-ER Design)
After ER modeling, normalize tables to eliminate redundancy:
- 1NF: No repeating groups (e.g., split
OrderintoOrderandOrderItem). - 2NF: Remove partial dependencies (e.g.,
Salarydepends onEmpID, notDeptID). - 3NF: Remove transitive dependencies (e.g.,
Locationshould not depend onDeptID).
Example: Unnormalized vs. Normalized
Unnormalized:
erDiagram
ENTITY "Order" {
string OrderID PK
string CustName
string ProductName
int Quantity
decimal Price
string Address
}Added Address attribute to show unnormalized data's real-world limitations
Problem: Repeating ProductName for multiple items.
Normalized (3NF):
erDiagram
ENTITY "Order" {
string OrderID PK
string CustID FK
}
ENTITY "OrderItem" {
string OrderID FK
string ProductID FK
int Quantity
decimal Price
}
ENTITY "Product" {
string ProductID PK
string Name
}In the Real World
- eSewa Transaction System
- Idea: Uses M:N relationships between
UserandTransaction. - How: Each
Usercan have manyTransactions, and eachTransactionlinks to aService(e.g., bill payment). - ER Snippet:
- Idea: Uses M:N relationships between
erDiagram
ENTITY "User" {
string UserID PK
string Name
string Email
}
ENTITY "Transaction" {
string TransID PK
string UserID FK
date TransDate
decimal Amount
}
ENTITY "Service" {
string ServiceID PK
string Name
decimal Cost
}Added attributes to show real transaction system with costs and datesDaraz Inventory Management
- Idea: Weak entities for
OrderItem(depends onOrderandProduct). - How:
OrderItemcannot exist withoutOrderIDandProductID. - Worked Example:
- A
Customerplaces anOrderfor 2Laptops. - The system creates two
OrderItemrecords (each withOrderID,ProductID,Quantity).
- A
- Idea: Weak entities for
Pathao Driver Assignment
- Idea: One-to-Many relationship between
DriverandRide. - How: One
Drivercan handle multipleRides, but eachRideis assigned to exactly oneDriver. - ER Snippet:
- Idea: One-to-Many relationship between
erDiagram
ENTITY "Driver" {
string DriverID PK
string Name
string Vehicle
}
ENTITY "Ride" {
string RideID PK
string DriverID FK
date RideDate
decimal Fare
string StartLocation
string EndLocation
}Added ride details (Vehicle, Fare, locations) for practical understanding7. Database Architecture (Centralized vs. Distributed)
| Feature | Centralized Database | Distributed Database |
|---|---|---|
| Data Location | Single server (e.g., NEPSE) | Multiple servers (e.g., global banks) |
| Scalability | Limited by single server | Scales horizontally |
| Fault Tolerance | Single point of failure | Redundancy across nodes |
| Example | University student records | Multinational company (e.g., Coca-Cola) |
Exam Tip: For a multinational company, choose distributed for:
- Redundancy (backup servers).
- Performance (local data access).
- Security (data isolation by region).
8. Advanced ER Concepts
Binary vs. Ternary Relationships
- Binary: Two entities (e.g.,
Student→Course). - Ternary: Three entities (e.g.,
Student,Course,Professorin aTeachesrelationship).
erDiagram
ENTITY "Student" {
string StudentID PK
string Name
}
ENTITY "Course" {
string CourseID PK
string Title
}
ENTITY "Professor" {
string ProfID PK
string Name
}
ENTITY "Enrollment" {
string EnrollID PK
string StudentID FK
string CourseID FK
string ProfID FK
date Semester
}Added Enrollment entity with semester to properly show ternary relationship Why Ternary?
- Avoids M:N complexity (e.g.,
Student→Professor→Course).
Exam Tip
ER Diagram Questions:
- Always label primary keys (
PK), foreign keys (FK), and weak entities (dashed line). - Show cardinality (1:N, M:N) with crow’s foot.
- Example: For a bank, draw
Bank→Branch→Account.
- Always label primary keys (
Database Design:
- Explain steps: requirements → ERD → normalization → implementation.
- Compare centralized vs. distributed with real examples (e.g., NEPSE vs. global bank).
Normalization:
- Identify repeating groups (1NF violation) and partial dependencies (2NF).
- Example: Split
OrderintoOrderandOrderItemfor 1NF.
Real-World Mapping:
- Link ER concepts to apps (e.g.,
eSewatransactions =User→Transaction). - Use weak entities for scenarios like
Order→Customer.
- Link ER concepts to apps (e.g.,
Common Pitfalls:
- Forgetting mandatory vs. optional participation.
- Misrepresenting M:N as two 1:N relationships (always use a junction entity).
Based on the TU BITM syllabus for Database Management System (IT220), unit 2.
Discussion
Loading…