Entity relationships area fundamental concept in data modeling, used to define how different data entities (or tables in a relational database) are connected and interact with each other. They help structure data in a meaningful way, ensuring data integrity, optimizing queries, and supporting business rules.
Defining Data Structure
Relationships establish how entities (e.g., Customer, Order, Product) are linked.
Example: A Customerplaces an Order, and an OrdercontainsProducts.
Ensuring Data Integrity
Relationships enforce constraints (e.g., foreign keys) to prevent orphaned or inconsistent data.
Example: An Order table must reference a valid CustomerID.
Supporting Business Rules
Relationships model real-world interactions (e.g., one-to-many, many-to-many).
Example:
One-to-Many (1:N): One Department has many Employees.
Many-to-Many (M:N): A Student can enroll in multiple Courses, and a Course can have many Students (resolved via a junction table).
One-to-One (1:1): A User has one Profile.
Optimizing Database Performance
Properly defined relationships improve query efficiency by reducing redundancy (normalization).
Indexes on foreign keys speed up joins.
Facilitating Data Retrieval (Joins)
Relationships enable SQL joins (INNER JOIN, LEFT JOIN) to fetch related data across tables.
Example:
SELECT Customers.Name, Orders.Date
FROM Customers
INNER JOIN Orders ON Customers.ID = Orders.CustomerID;
Conclusion:
Entity relationships are crucial for organizing data logically, maintaining consistency, and enabling efficient data operations. They form the backbone of relational database design and are essential for applications ranging from transactional systems to analytics.
Define Concept of Entity Relationship
Entity relationships are associations between entities, which you have come to recognize as links between tables. Typically, a relationship is indicated by a verb connecting two entities. Consider the following two related entities with several attributes listed for each (key attributes are indicated with asterisks).
🔍 Database Diagram Analysis
This diagram shows two entities: EMPLOYEE and PROJECT, with a many-to-many relationship between them, as indicated by the double-headed red arrow and the text:
"Employees are assigned to projects OR
Projects are assigned to employees"
🧾 Table Structures and Attributes
EMPLOYEE Table
EmpID (Primary Key) – Unique identifier for each employee.
LastName – Employee's last name.
FirstName – Employee's first name.
PROJECT Table
ProjID (Primary Key) – Unique identifier for each project.
ProjName – Name of the project.
ProjDept – Department responsible for the project.
🔗 Relationship Between EMPLOYEE and PROJECT
Type: Many-to-Many
Interpretation:
An employee can work on multiple projects.
A project can have multiple employees assigned.
📘 Normalization Note
In relational design, a many-to-many relationship is not directly implemented between two tables. It requires a
junction (associative) table, commonly called something like:
`EMPLOYEE_PROJECT` (not shown but implied)
Attributes might include:
EmpID – Foreign key referencing EMPLOYEE
ProjID – Foreign key referencing PROJECT
Possibly: HoursWorked, AssignmentDate, etc.
✅ Summary
Table
Attributes
EMPLOYEE
EmpID, LastName, FirstName
PROJECT
ProjID, ProjName, ProjDept
Relationship
Many-to-Many between EMPLOYEE and PROJECT
Two related entities: Employees are assigned to projects OR Projects are assigned to employees.
Here is the SQL schema for the given diagram, including the junction table[1] `EMPLOYEE_PROJECT` to model the many-to-many relationship between `EMPLOYEE` and `PROJECT`.
-- EMPLOYEE table
CREATE TABLE EMPLOYEE (
EmpID INT PRIMARY KEY,
LastName VARCHAR(50),
FirstName VARCHAR(50)
);
-- PROJECT table
CREATE TABLE PROJECT (
ProjID INT PRIMARY KEY,
ProjName VARCHAR(100),
ProjDept VARCHAR(100)
);
-- Junction table to model the many-to-many relationship
CREATE TABLE EMPLOYEE_PROJECT (
EmpID INT,
ProjID INT,
AssignedDate DATE, -- Optional: date the employee was assigned
HoursWorked DECIMAL(5,2), -- Optional: hours worked on the project
PRIMARY KEY (EmpID, ProjID),
FOREIGN KEY (EmpID) REFERENCES EMPLOYEE(EmpID),
FOREIGN KEY (ProjID) REFERENCES PROJECT(ProjID)
);
🔧 Notes:
The primary key of EMPLOYEE_PROJECT is a composite key (EmpID, ProjID) to ensure that an employee can only be assigned to a project once.
Optional fields like AssignedDate and HoursWorked are included for practical tracking—feel free to remove or rename them as needed.
A general statement about the relationship between the EMPLOYEE and PROJECT entities may be put this way:
Employees are assigned to projects (or)
Projects are assigned to employees.
Although these statements denote an association between the two entities, we know nothing about how the entities are formally related to one another. Until we know considerably more, we cannot convert them into an ER diagram or, ultimately, into linked tables.
For example, one important thing we do not yet know is if the EMPLOYEE entity should insert its key attribute (EmpID) into the PROJECT entity to establish the relationship, or if the PROJECT entity should insert its key attribute (ProjID) into the EMPLOYEE entity. We also do not know if every employee must be assigned to a project, or if deleting an employee record from an Employees table will have an adverse effect on records in a linked Projects table. Determining how entities are related requires careful interpretation of the business rules uncovered during Requirements Analysis (and the application of common-sense rules). Rules tell the database designer the type of relationship that exists between entities, and the type of participation entities enjoy in relationships.
While it is true that the entity-relationship model of a database is independent of the formal data model used to express the structure of the data to a DBMS, we often make decisions on how to model the data based on the requirement of the formal data model we will be using. Removing multivalued attributes is one such case and you will also see an example of this when we deal with many-to-many relationships between entities.
Entity Relationships - Exercise
Before moving on to the next lesson, click the Exercise link below to check your understanding of entity relationships. Entity Relationships - Exercise
The next lesson briefly describes the types of relationships that can exist between entities.
[1]junction table: In ERD diagrams, a junction table (also known as an associative entity or bridge table) is introduced to resolve many-to-many relationships between two entities. It acts as an intermediary, containing foreign keys from both related entities to convert the many-to-many relationship into two one-to-many relationships.