Relationships in ER Modeling
Degree of Relationship
Read aloud is unavailable in this browser.
How Many Entities Does a Relationship Connect?
When we speak of the "degree" of a relationship, we're asking a fundamental question: How many entity types participate in this association? The answer profoundly affects how we model, implement, and query the relationship.
Most relationships you'll encounter are binary—connecting exactly two entity types. Students enroll in courses. Employees work in departments. Customers place orders. These two-party associations are so common that beginners sometimes assume all relationships are binary.
But the real world doesn't always divide neatly into pairs. Sometimes three or more distinct entities participate together in a single association that cannot be decomposed. A vendor supplies a specific product to a particular warehouse. A doctor prescribes a medication to a patient at a certain dosage. These ternary and higher-degree relationships require careful understanding to model correctly.
By the end of this page, you will understand binary, ternary, and n-ary relationships, recognize when higher-degree relationships are necessary, appreciate the tradeoffs in degree choice, and know how to implement each degree in relational schemas.
Defining Relationship Degree
The degree of a relationship type is the number of entity types that participate in the relationship. This is a fundamental structural property that affects everything from diagram notation to database implementation.
Formal Definition:
Given a relationship type R involving entity types E₁, E₂, ..., Eₙ, the degree of R is n.
Common Degrees:
| Degree | Name | Description | Prevalence |
|---|---|---|---|
| 1 | Unary (Recursive) | Single entity type relates to itself | Common (hierarchies, graphs) |
| 2 | Binary | Two distinct entity types | Most common (~90% of relationships) |
| 3 | Ternary | Three distinct entity types | Occasional (supply chains, assignments) |
| 4+ | N-ary (Quaternary, etc.) | Four or more entity types | Rare (complex scheduling, multi-party) |
Why Degree Matters:
-
Semantic Precision: A ternary relationship "Vendor supplies Product to Warehouse" captures information that three binary relationships cannot fully represent.
-
Implementation Complexity: Binary relationships often use foreign keys; ternary and higher typically require junction tables with composite keys.
-
Query Patterns: Higher-degree relationships require more complex joins and different query strategies.
-
Cardinality Interpretation: Cardinality constraints have different meanings in higher-degree relationships.
-
Diagram Readability: Diagrams become harder to read as relationship degree increases.
Some texts use 'unary' for self-referential relationships (degree 1 in terms of distinct entity types). Others count the same entity type twice in a self-referential relationship, calling it binary. We follow the convention that degree counts distinct entity types, so Employee-manages-Employee is degree 1 (unary/recursive).
Unary (Recursive) Relationships
A unary or recursive relationship connects an entity type to itself. Different instances of the same entity type relate to each other through distinct roles.
When Unary Relationships Occur:
- Hierarchies: Employee manages Employee (org chart)
- Networks: Person knows Person (social graph)
- Sequences: Task precedes Task (project scheduling)
- Comparisons: Product similar_to Product (recommendations)
Formal Structure:
Relationship: MANAGES
Participant 1: EMPLOYEE (role: manager)
Participant 2: EMPLOYEE (role: subordinate)
Cardinality: One manager supervises many subordinates (1:N)
ER Diagram Representation:
┌───────────────┐
│ │
┌─────┤ EMPLOYEE ├─────┐
│ │ │ │
│ └───────────────┘ │
│ │
(manager) (subordinate)
│ │
│ ┌───────────────┐ │
└─────┤ MANAGES ├─────┘
└───────────────┘
The two lines from EMPLOYEE to MANAGES represent the two roles—one employee participates as manager, another as subordinate. Role labels are essential for clarity.
SQL Implementation:
CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
name VARCHAR(100),
manager_id INT, -- Self-referencing FK
FOREIGN KEY (manager_id) REFERENCES Employee(emp_id)
);
-- Sample data showing hierarchy
INSERT INTO Employee VALUES (1, 'CEO', NULL); -- Top, no manager
INSERT INTO Employee VALUES (2, 'VP Sales', 1); -- Reports to CEO
INSERT INTO Employee VALUES (3, 'VP Tech', 1); -- Reports to CEO
INSERT INTO Employee VALUES (4, 'Dev Lead', 3); -- Reports to VP Tech
INSERT INTO Employee VALUES (5, 'Developer', 4); -- Reports to Dev Lead
Querying Hierarchies:
-- Find all direct reports of employee #3
SELECT * FROM Employee WHERE manager_id = 3;
-- Recursive query for full hierarchy (SQL:1999+)
WITH RECURSIVE OrgChart AS (
SELECT emp_id, name, manager_id, 0 as level
FROM Employee WHERE manager_id IS NULL
UNION ALL
SELECT e.emp_id, e.name, e.manager_id, oc.level + 1
FROM Employee e
JOIN OrgChart oc ON e.manager_id = oc.emp_id
)
SELECT * FROM OrgChart ORDER BY level;
Unary relationships can form cycles (A manages B, B manages C, C manages A). In hierarchies, cycles are usually invalid. Database constraints alone cannot prevent cycles—application logic or triggers are needed. Graph databases handle this more naturally.
Binary Relationships
Binary relationships connect exactly two distinct entity types. They represent the vast majority of relationships in real-world databases and are the default assumption when discussing relationships without specifying degree.
Why Binary Dominates:
-
Natural Pairings: Many real-world associations are inherently two-party (buyer-seller, parent-child, author-book).
-
Simpler Semantics: Two-party associations are easier to name, understand, and validate.
-
Efficient Implementation: Binary 1:N uses simple FK; even M:N only needs one junction table.
-
Query Simplicity: Binary relationships require at most one JOIN to traverse.
| Domain | Entity 1 | Relationship | Entity 2 | Cardinality |
|---|---|---|---|---|
| Education | STUDENT | ENROLLS_IN | COURSE | M:N |
| Employment | EMPLOYEE | WORKS_IN | DEPARTMENT | N:1 |
| E-commerce | CUSTOMER | PLACES | ORDER | 1:N |
| Healthcare | PATIENT | ASSIGNED_TO | DOCTOR | N:1 |
| Library | BOOK | WRITTEN_BY | AUTHOR | M:N |
| Manufacturing | PRODUCT | BELONGS_TO | CATEGORY | N:1 |
Binary Cardinality Patterns:
1:1 (One-to-One)
EMPLOYEE ──1────◇────1── PARKING_SPOT
Meaning: Each employee has at most one reserved spot;
each spot is reserved for at most one employee.
Implication: FK in either table, or merge tables.
1:N (One-to-Many)
DEPARTMENT ──1────◇────N── EMPLOYEE
Meaning: Each department has many employees;
each employee belongs to one department.
Implication: FK in Employee table pointing to Department.
M:N (Many-to-Many)
STUDENT ──M────◇────N── COURSE
Meaning: Each student takes many courses;
each course has many students.
Implication: Junction table with composite key.
When starting to model a relationship, first try to express it as binary. Only escalate to ternary or higher when binary relationships demonstrably fail to capture the required semantics. Most "ternary" relationships are actually combinations of binary relationships.
Ternary Relationships
A ternary relationship involves exactly three entity types in a single association. Ternary relationships are used when three entities together capture information that cannot be derived from pairwise binary relationships.
The Key Question:
Before using a ternary relationship, ask: Can this be modeled as two or three binary relationships? If the answer is "yes, without losing information," use binary. If critical information would be lost, use ternary.
Classic Example: SUPPLIES
Consider: Vendors supply products to warehouses.
Attempt with Binary Relationships:
VENDOR ──supplies── PRODUCT (Vendor supplies this product in general)
VENDOR ──serves── WAREHOUSE (Vendor delivers to this warehouse in general)
PRODUCT ──stocked_at── WAREHOUSE (Product is available at this warehouse)
What's Lost: These binaries tell us that Vendor V1 supplies Product P1 somewhere, and Vendor V1 delivers to Warehouse W1 something. But they don't tell us: Does V1 supply P1 specifically to W1?
The ternary relationship captures exactly this:
SUPPLIES(VENDOR, PRODUCT, WAREHOUSE)
Instance: (Acme Corp, Widget, NY Warehouse)
Meaning: Acme Corp supplies Widgets specifically to the NY Warehouse.
ER Diagram for Ternary:
VENDOR
│
│
┌──────◇──────┐
│ SUPPLIES │
└──────┬──────┘
/│
/ │
/ │
PRODUCT │ WAREHOUSE
│
(quantity, price, contract_date)
The diamond connects to three entity types. Relationship attributes (quantity, price) belong to the ternary relationship, not any single entity.
SQL Implementation:
CREATE TABLE Vendor (
vendor_id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Product (
product_id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Warehouse (
warehouse_id INT PRIMARY KEY,
location VARCHAR(200)
);
-- Ternary relationship as junction table
CREATE TABLE Supplies (
vendor_id INT,
product_id INT,
warehouse_id INT,
quantity INT,
unit_price DECIMAL(10,2),
contract_date DATE,
PRIMARY KEY (vendor_id, product_id, warehouse_id),
FOREIGN KEY (vendor_id) REFERENCES Vendor(vendor_id),
FOREIGN KEY (product_id) REFERENCES Product(product_id),
FOREIGN KEY (warehouse_id) REFERENCES Warehouse(warehouse_id)
);
Note the three-part composite primary key—all three entity keys together uniquely identify a supply arrangement.
In ternary relationships, cardinality is specified per entity type relative to pairs of the other two. For example: 'For each (Product, Warehouse) pair, how many Vendors can supply it?' This three-way cardinality is harder to visualize and specify than binary cardinality.
Understanding Ternary Cardinality
Cardinality in ternary relationships is more nuanced than in binary. Instead of asking "how many Y's per X," we ask "how many Z's per (X, Y) pair."
Cardinality Interpretation for SUPPLIES(Vendor, Product, Warehouse):
-
Vendor cardinality: For each (Product, Warehouse) pair, how many Vendors can supply it? If multiple vendors can supply the same product to the same warehouse, this is "many."
-
Product cardinality: For each (Vendor, Warehouse) pair, how many Products can be supplied? If a vendor can supply multiple products to a warehouse, this is "many."
-
Warehouse cardinality: For each (Vendor, Product) pair, how many Warehouses can receive it? If a vendor can ship the same product to multiple warehouses, this is "many."
| Scenario | V per (P,W) | P per (V,W) | W per (V,P) | Key Impact |
|---|---|---|---|---|
| Exclusive supplier per warehouse-product | 1 | M | M | PK could be (product, warehouse) |
| Each vendor supplies exactly one product type to each warehouse | M | 1 | M | PK could be (vendor, warehouse) |
| Fully flexible (most common) | M | M | M | PK is (vendor, product, warehouse) |
| Single warehouse per vendor-product | M | M | 1 | PK could be (vendor, product) |
Visual Cardinality Notation:
VENDOR
│
M (many vendors per product-warehouse pair)
│
┌──────◇──────┐
│ SUPPLIES │
└──────┬──────┘
/│
M │ M
/ │
PRODUCT │ WAREHOUSE
This notation places the cardinality value on each connecting line, indicating the maximum participation for that entity type.
Effect on Primary Key:
The cardinality determines which entity combinations form the primary key:
- If all are "many" (M:M:M): PK = (all three FKs)
- If one is "one" (M:M:1): The "one" side's FK might not be needed in PK
- If two are "one" (M:1:1): PK could be just the "many" side's FK
However, including all three in the PK is the safest default unless business rules clearly dictate otherwise.
When defining ternary cardinality, construct specific examples: 'Can Acme supply Widgets to both NYC and LA warehouses? Yes → Warehouse cardinality is many.' 'Can both Acme and Beta supply Widgets to NYC warehouse? Yes → Vendor cardinality is many.' Concrete examples prevent abstract cardinality errors.
When to Choose Ternary Over Binary
Choosing between ternary and multiple binary relationships is a critical modeling decision. Here's a systematic approach:
The Information Loss Test:
Try to model the situation with binary relationships. Then ask: Can I reconstruct any valid ternary instance from the binary instances? If not, ternary is required.
- •The association requires all three entities simultaneously
- •Relationship attributes belong to the triple, not any pair
- •Binary combinations don't uniquely identify instances
- •Business rules reference the three-way combination
- •Example: Doctor prescribes Drug to Patient (dose is per prescription)
- •Pairs of entities relate independently
- •Each binary relationship has distinct semantics
- •There's no information loss from decomposition
- •The ternary seems artificial or forced
- •Example: Author writes Book; Publisher publishes Book (separate, not three-way)
Decomposition Example: Where Binary Works
Consider: Authors write Books, and Publishers publish Books.
Ternary approach (wrong):
PUBLISHES(AUTHOR, BOOK, PUBLISHER)
Problem: This implies the author-book-publisher triple is atomic. But in reality:
- An author writes a book (binary: WRITES)
- A publisher publishes a book (binary: PUBLISHES)
- The author and publisher don't directly interact in this model
Binary approach (correct):
WRITES(AUTHOR, BOOK) -- Author writes this book
PUBLISHES(BOOK, PUBLISHER) -- Publisher publishes this book
No information is lost. The publisher of a book can be found via the PUBLISHES binary relationship, regardless of author.
If you have relationship attributes, ask: Which entities do they describe? If an attribute (like 'quantity supplied') requires all three entities to make sense, the relationship is truly ternary. If attributes only require two entities, you likely have binary relationships.
N-ary Relationships (Degree 4+)
Relationships of degree 4 or higher (quaternary, quinary, etc.) are rare but occasionally necessary. They follow the same principles as ternary relationships, extended to more participants.
When N-ary Emerges:
- Complex Scheduling: Teacher teaches Course in Room during TimeSlot (4 entities)
- Financial Transactions: Buyer purchases Item from Seller using Currency (4 entities)
- Research Assignments: Faculty supervises Student on Project funded by Grant (4 entities)
Example: Course Scheduling (Quaternary)
SCHEDULES(INSTRUCTOR, COURSE, ROOM, TIMESLOT)
Instance: (Prof. Smith, CSE301, Room 100, Mon 9am)
Meaning: Prof. Smith teaches CSE301 in Room 100 at Mon 9am.
SQL Implementation:
-- Quaternary relationship
CREATE TABLE CourseSchedule (
instructor_id INT,
course_id INT,
room_id INT,
timeslot_id INT,
semester VARCHAR(20), -- Relationship attribute
PRIMARY KEY (instructor_id, course_id, room_id, timeslot_id),
FOREIGN KEY (instructor_id) REFERENCES Instructor(id),
FOREIGN KEY (course_id) REFERENCES Course(id),
FOREIGN KEY (room_id) REFERENCES Room(id),
FOREIGN KEY (timeslot_id) REFERENCES TimeSlot(id)
);
The four-part primary key ensures uniqueness: no scheduling conflict can occur because each combination is unique.
| Degree | PK Columns | JOINs to Traverse | Cardinality Complexity | Practical Notes |
|---|---|---|---|---|
| 2 (Binary) | 1-2 | 1 | 2 values | Default, highly optimized |
| 3 (Ternary) | 3 | 2-3 | 3 values (each relative to other 2) | Occasional, well-supported |
| 4 (Quaternary) | 4 | 3-4 | 4 values (each relative to other 3) | Rare, consider decomposition |
| 5+ | 5+ | 4+ | Increasingly complex | Very rare, reconsider model |
N-ary relationships with n ≥ 4 often indicate an opportunity for decomposition. The quaternary SCHEDULES might decompose to: TEACHES(Instructor, Course), HELD_IN(Course, Room), AT_TIME(Course, TimeSlot). Only retain the n-ary form if decomposition genuinely loses information.
KEEP LEARNING
There’s more to understand.
You’re reading the introduction. Create a free account to read the complete lesson, save notes and keep track of your progress.