Entity Relationship Diagrams (ERDs) visually represent the structure of a database, showing entities and the relationships between them.
One-to-One (1:1)
• Each person has exactly one passport
• Each passport belongs to exactly one person
One-to-Many (1:M)
• One doctor can have MANY patients
• Each patient has only ONE doctor (in this system)
Example: Customer and Orders
• One customer can place MANY orders
• Each order belongs to ONE customer
Many-to-Many (M:M)
Many-to-many relationships require a JUNCTION TABLE (also called a linking table or bridge table) to implement in a relational database.
Problem:
• One student takes MANY courses
• One course has MANY students
• This is many-to-many - can't directly link!
Solution - Junction Table:
Student Table:
| StudentID | Name |
|---|---|
| S001 | Alice |
| S002 | Bob |
| CourseID | Title |
|---|---|
| C001 | Maths |
| C002 | Physics |
| StudentID | CourseID | Grade |
|---|---|---|
| S001 | C001 | A |
| S001 | C002 | B |
| S002 | C001 | B |
• Alice (S001) takes Maths AND Physics
• Bob (S002) takes only Maths
• Maths course has Alice AND Bob
• The junction table resolves the M:M relationship
Drawing an Entity Relationship Diagram
Each entity is drawn as a box containing its name, written in the singular. A line joins two entities that are related, and the ends of the line show the type of relationship: a plain end means "one" and a crow's foot, three short lines that fan out, means "many". The diagrams below use a plain line end for "one" and an arrowhead to stand for the crow's foot.
One-to-one: Employee ---------- ParkingSpace
One-to-many: Customer ---------< Booking
Many-to-many: Student >--------< Course
A many-to-many relationship cannot be implemented directly, so the link table is drawn between the two entities. It is at the "many" end of both lines:
Student ---------< Enrolment >--------- Course
• One course has many enrolments
• Each enrolment belongs to one student and one course