π Entity Relationships describe how tables in a relational database are connected to one another. These relationships are established using Primary Keys and Foreign Keys, allowing data to be organized efficiently while maintaining integrity and minimizing redundancy.
π What are Entity Relationships?
In a relational database, each table represents an entityβsuch as a customer, employee, product, or order. Relationships define how records in one table are associated with records in another table. These relationships form the foundation of relational database design.
Information
π― Why are Entity Relationships Important?
Relationships enable databases to store related information across multiple tables while preserving consistency and reducing duplicate data.
- π Reduce data redundancy.
- π Maintain referential integrity.
- π Support efficient normalization.
- π Simplify complex data retrieval using joins.
- π Improve database scalability and maintainability.
π§© Core Components
| Component | Description |
|---|---|
| Entity | A real-world object represented as a table. |
| Attribute | A property of an entity represented as a column. |
| Primary Key | Uniquely identifies each row. |
| Foreign Key | References the primary key of another table. |
| Relationship | Association between two entities. |
π Example Tables
Consider two entities: Customers and Orders.
| Customers |
|---|
| CustomerID (PK) |
| CustomerName |
| Orders |
|---|
| OrderID (PK) |
| CustomerID (FK) |
| OrderDate |
| TotalAmount |
The CustomerID column in the Orders table references the CustomerID in the Customers table.
π’ Types of Entity Relationships
1οΈβ£ One-to-One (1:1)
Each record in one table is related to exactly one record in another table.
| Employees | EmployeeDetails |
|---|---|
| EmployeeID | EmployeeID |
| Name | PassportNumber |
Every employee has one detail record, and each detail record belongs to one employee.
One-to-One Relationship
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100)
);
CREATE TABLE EmployeeDetails (
EmployeeID INT PRIMARY KEY,
PassportNumber VARCHAR(50),
FOREIGN KEY (EmployeeID)
REFERENCES Employees(EmployeeID)
);2οΈβ£ One-to-Many (1:N)
One record in the parent table can have multiple related records in the child table.
| Customers | Orders |
|---|---|
| CustomerID | CustomerID |
| Name | OrderDate |
| TotalAmount |
One customer can place many orders, but each order belongs to only one customer.
One-to-Many Relationship
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(100)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID)
REFERENCES Customers(CustomerID)
);3οΈβ£ Many-to-Many (M:N)
Multiple records in one table can relate to multiple records in another table. This relationship is implemented using a junction (bridge) table.
| Students | Enrollments | Courses |
|---|---|---|
| StudentID | StudentID (FK) | CourseID |
| Name | CourseID (FK) | CourseName |
Many-to-Many Relationship
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100)
);
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100)
);
CREATE TABLE Enrollments (
StudentID INT,
CourseID INT,
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID)
REFERENCES Students(StudentID),
FOREIGN KEY (CourseID)
REFERENCES Courses(CourseID)
);Important
π Relationship Summary
| Relationship | Description | Example |
|---|---|---|
| One-to-One | One record relates to one record. | Person β Passport |
| One-to-Many | One parent has many children. | Customer β Orders |
| Many-to-Many | Many records relate to many records. | Students β Courses |
π Querying Related Data
Relationships allow data to be retrieved efficiently using SQL joins.
Retrieve Customer Orders
SELECT
c.CustomerName,
o.OrderID,
o.OrderDate
FROM Customers c
JOIN Orders o
ON c.CustomerID = o.CustomerID;πΌ Real-World Applications
- π Customers and orders in e-commerce.
- π¦ Customers and bank accounts.
- π Students and courses.
- π₯ Patients and appointments.
- π’ Employees and departments.
- π¦ Products and suppliers.
ποΈ Database Compatibility
Entity relationships are supported by all relational database management systems through primary keys, foreign keys, and SQL constraints.
| Database System | Relationship Support |
|---|---|
| MySQL | β Yes |
| PostgreSQL | β Yes |
| SQL Server | β Yes |
| Oracle | β Yes |
| SQLite | β Yes (foreign key enforcement must be enabled). |
β οΈ Common Mistakes
- β Missing foreign key constraints.
- β Using duplicate data instead of relationships.
- β Creating unnecessary many-to-many relationships.
- β Forgetting indexes on frequently joined foreign keys.
- β Confusing entity relationships with SQL joins.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π Tables represent entities.
- π Relationships connect entities using keys.
- π Primary keys uniquely identify records.
- π Foreign keys establish relationships.
- π The three primary relationship types are One-to-One, One-to-Many, and Many-to-Many.
- π Well-designed relationships improve consistency, scalability, and maintainability.