Master the foundational concepts of database design with Entity Relationship modeling
The Entity-Relationship (ER) model is a high-level conceptual data model that helps database designers plan the structure and relationships of data before implementation. Developed by Peter Chen in 1976, it provides a graphical representation of real-world entities and their relationships.
Note: ER modeling is typically the first step in database design, performed before deciding on specific database technologies or implementation details.
Example of a basic ER diagram showing entities and relationships
The ER model consists of three basic concepts:
In a university database:
ER modeling offers several benefits for database design:
Which of the following is NOT a basic component of the ER model?
An entity is an object or concept that exists in the real world and can be distinctly identified. In database terms, an entity is typically represented as a table when transformed into a relational model.
Strong entity (Student) vs Weak entity (Dependent)
Attributes are properties or characteristics that describe an entity. They provide details about an entity and are represented as columns in relational tables.
Cannot be divided further (e.g., StudentID)
Can be divided into smaller parts (e.g., Address)
Contains only one value (e.g., Birthday)
Contains multiple values (e.g., PhoneNumbers)
Value calculated from other attributes (e.g., Age from Birthdate)
Uniquely identifies an entity instance (e.g., StudentID)
Consider a Student entity with attributes:
Visual representation of a Student entity with attributes
Identify the type of each attribute for the following Product entity:
Relationships represent associations between entities. They show how entities interact with each other and are a crucial part of database design. Relationships are typically represented as lines connecting entity boxes in ER diagrams.
Example of relationships between Class, Course, and Professor entities
Cardinality defines how many instances of an entity relate to one instance of another entity. There are three main types:
One instance of Entity A is associated with exactly one instance of Entity B, and vice versa.
Example: Person and Passport (one person has exactly one passport, and one passport belongs to exactly one person)
One instance of Entity A is associated with multiple instances of Entity B, but one instance of Entity B is associated with only one instance of Entity A.
Example: Department and Employee (one department has many employees, but each employee belongs to only one department)
One instance of Entity A is associated with multiple instances of Entity B, and one instance of Entity B is associated with multiple instances of Entity A.
Example: Student and Course (one student can enroll in many courses, and one course can have many students)
Sometimes, relationships themselves can have attributes. These attributes describe properties of the relationship rather than the entities involved.
Consider a many-to-many relationship between Student and Course entities. The relationship "Enrolls" might have the following attributes:
Relationship with attributes between Student and Course entities
Which type of relationship would be most appropriate for Doctor and Patient entities in a hospital system where doctors can have many patients and patients can be treated by multiple doctors?
Draw an ER diagram for the following scenario:
A library system needs to track books, authors, and borrowers. Books can have multiple authors, and authors can write multiple books. Borrowers can check out multiple books, but each book can only be checked out by one borrower at a time. The system needs to track when books are checked out and returned.
Participation constraints specify whether all or only some entity instances participate in a relationship.
Every instance of the entity must participate in the relationship. Represented by a double line in ER diagrams.
Total participation (double line) from Order to Customer
Example: Every Order must be associated with a Customer (an order cannot exist without a customer).
Some instances of the entity may participate in the relationship, but not all. Represented by a single line in ER diagrams.
Partial participation (single line) from Customer to Order
Example: Not every Customer must have placed an Order (a customer can exist without having placed any orders).
These concepts help to model hierarchical relationships between entity types.
The process of defining subsets of an entity type that have distinctive attributes or relationships.
Specialization: Vehicle → Car, Truck, Motorcycle
Example: Vehicle can be specialized into Car, Truck, and Motorcycle, each with specific attributes.
The reverse process of abstraction, where similar entities are generalized into a higher-level entity type.
Generalization: Savings Account, Checking Account → Bank Account
Example: Savings Account and Checking Account can be generalized into Bank Account.
Inheritance in ER modeling allows subclass entities to inherit attributes and relationships from their superclass.
Person entity with Employee and Customer as subclasses
In the example above:
Aggregation represents a "part-of" relationship where an entity is made up of component entities but those components can exist independently.
Aggregation: Department consists of Employees
Example: A Department consists of multiple Employees, but Employees can exist independently of the Department.
Apply your knowledge of advanced ER concepts to analyze this university system:
Advanced ER model for a university management system
Notice how this diagram incorporates:
How would you modify this diagram to include the concept of Research Projects where faculty members from different departments can collaborate?
The Entity-Relationship model provides a conceptual design of the database, but most database systems implement the relational model. Converting an ER diagram to a relational schema is a critical step in database implementation.
Note: Relational databases store data in tables with rows and columns, where each table represents an entity or relationship from the ER model.
Follow these rules to convert your ER diagram to a relational schema:
Create a relation (table) for each regular entity type.
Example: Student entity with attributes (StudentID, Name, Address) becomes:
Student(StudentID, FirstName, LastName, Street, City, State, ZipCode)
Create a relation for each weak entity type:
Example: Dependent weak entity related to Employee becomes:
Dependent(EmployeeID, DependentName, Relationship, BirthDate)
where EmployeeID is a foreign key referencing Employee
Choose one of the related entities and include the primary key of the other as a foreign key:
Example: Person and Passport with 1:1 relationship becomes:
Person(PersonID, Name, ...)
Passport(PassportNumber, IssueDate, ExpiryDate, PersonID)
where PersonID in Passport references Person
Include the primary key of the entity on the "one" side as a foreign key in the relation for the entity on the "many" side:
Example: Department and Employee with 1:N relationship becomes:
Department(DeptID, DeptName, Location, ...)
Employee(EmployeeID, Name, Salary, DeptID, ...)
where DeptID in Employee references Department
Create a new relation for the relationship:
Example: Student and Course with M:N relationship "Enrolls" becomes:
Student(StudentID, Name, ...)
Course(CourseID, Title, Credits, ...)
Enrollment(StudentID, CourseID, EnrollmentDate, Grade)
where StudentID and CourseID reference Student and Course respectively
There are multiple approaches to mapping inheritance hierarchies:
Create one table for the entire hierarchy with all attributes and a discriminator column:
Single table for Person, Employee, and Customer
Person(PersonID, Name, Address, PersonType, EmployeeID, Salary, Department, CustomerID, CreditLimit, CustomerType)
Pros: Simple, good performance for queries across the hierarchy
Cons: Many null values, potential for large tables
Create separate tables for the superclass and each subclass with appropriate references:
Separate tables with foreign keys
Person(PersonID, Name, Address)
Employee(EmployeeID, PersonID, Salary, Department)
Customer(CustomerID, PersonID, CreditLimit, CustomerType)
Pros: No null values, normalized structure
Cons: Requires joins for complete entity data
Create a separate table for each subclass with all attributes (including inherited ones):
Separate tables with duplicated attributes
Employee(PersonID, Name, Address, EmployeeID, Salary, Department)
Customer(PersonID, Name, Address, CustomerID, CreditLimit, CustomerType)
Pros: Good query performance for specific subclasses
Cons: Data redundancy, update anomalies
Convert the following ER diagram to a relational schema:
Library system ER diagram from Lesson 3
You've completed the Entity Relationship Model course