All About DB Relationships, Sets, and Types

Table of Contents

A relationship is a connection between two entities. Picture a library database where a READER BORROWS a BOOK. It’s crucial to understand that different instances of readers will borrow different books. For example, Alice BORROWS “1984”, while Bob BORROWS “To Kill a Mockingbird”.

The term relationship type refers to the general nature of the connection between two entities, such as BORROWS in our example. On the other hand, the collection of similar connections at a specific moment is called the Relationship Set.

graph TD

    subgraph "Entity-Relationship Model"

        A[READER] -->|BORROWS| B[BOOK]

    end

    subgraph "Relationship Instances"

        C[Alice] -->|BORROWS| D["1984"]

        E[Bob] -->|BORROWS| F["To Kill a Mockingbird"]

    end

    style A fill:#f9f,stroke:#333,stroke-width:2px

    style B fill:#cfc,stroke:#333,stroke-width:2px

    style C fill:#f9f,stroke:#333,stroke-width:2px

    style D fill:#cfc,stroke:#333,stroke-width:2px

    style E fill:#f9f,stroke:#333,stroke-width:2px

    style F fill:#cfc,stroke:#333,stroke-width:2px

It’s worth noting that, mirroring the convention for entity types and entity sets, a relationship type and its corresponding relationship set are typically referred to by the same name for simplicity’s sake.

Visual Representation in ER Diagrams

In Entity-Relationship (ER) diagrams, relationship types take the form of diamond-shaped boxes. These diamonds are linked by straight lines to rectangular boxes representing the participating entity types. The name of the relationship is displayed within the diamond-shaped box.

erDiagram

    READER ||--o{ BORROWS : "participates in"

    BOOK ||--o{ BORROWS : "is subject of"

    READER {

        int ReaderID PK

        string Name

        string Email

    }

    BOOK {

        int BookID PK

        string Title

        string Author

        string ISBN

    }

    BORROWS {

        int ReaderID FK

        int BookID FK

        date BorrowDate

        date DueDate

    }

Think of these relationships as the threads in a spider’s web. Just as each thread connects different parts of the web and allows the spider to navigate its domain, relationships connect different entities in your data model and enable information to flow between them.

Relationship Attributes

While we typically associate attributes with entities, it’s important to note that relationships can also have attributes. These attributes provide additional information about the association between entities.

For example, in our READER BORROWS BOOK relationship, we might want to include attributes such as:

  • BorrowDate: The date the book was borrowed
  • DueDate: The date the book is due to be returned
  • RenewalCount: The number of times the borrowing period has been extended
erDiagram
    READER ||--o{ BORROWS : ""
    BOOK ||--o{ BORROWS : ""
    BORROWS {
        date BorrowDate
        date DueDate
        int RenewalCount
    }
    BORROWS }o--|| READER : ""
    BORROWS }o--|| BOOK : ""

    READER {
        string ReaderID
    }
    BOOK {
        string ISBN
    }

Relationship attributes are particularly useful when the information they contain is specific to the relationship and doesn’t belong to any single participating entity.

Weak Entities and Identifying Relationships

In some cases, an entity’s existence is dependent on its relationship with another entity. These are called weak entities, and the relationships that give them context are called identifying relationships.

A weak entity is one that cannot be uniquely identified by its attributes alone. It relies on its relationship with a strong (or owner) entity for identification. The relationship between a weak entity and its owner is called an identifying relationship.

For example, in a university database, consider a COURSE entity and a SECTION entity:

  • COURSE is a strong entity with a unique identifier (CourseID)
  • SECTION is a weak entity because its identifier (SectionNumber) is only unique within the context of a specific course

The relationship HAS between COURSE and SECTION is an identifying relationship.

erDiagram
    COURSE ||--o{ SECTION : HAVE
    
    COURSE {
        string CourseID
    }
    
    SECTION {
        string SectionNumber PK
        string RoomNumber
    }

In this example, a SECTION’s unique identity is a combination of its partial key (SectionNumber) and the full key of its owner COURSE (CourseID). This composite key ensures each SECTION is uniquely identifiable within the entire database.

Understanding weak entities and identifying relationships is crucial for accurately modeling scenarios where some entities’ existence and identification depend on their associations with other entities.

Degrees of Relationship Types

Imagine a scenario where an employee works on multiple projects, and each project involves multiple employees. This intricate web of connections is represented in our data model through the concept of relationship degrees.

The degree of a relationship type is determined by the number of participating entity types. We’ll concentrate on three primary types:

The Unary (recursive) relationship type

A unary relationship type involves only one entity type, but in different roles. Consider a SUPERVISES relationship within an EMPLOYEE entity set. Here, one employee supervises another, both belonging to the same EMPLOYEE entity set. The EMPLOYEE entity type participates twice: once as a supervisor, and once as a supervisee.

graph TD
    A[Employee A]
    B[Employee B]
    C[Employee C]
    D[Employee D]
    E[Employee E]
    F[Employee F]

    A -.-> B
    A -.-> C
    D -.-> E
    D -.-> F

    B --> A
    C --> A
    E --> D
    F --> D

    linkStyle 0,1,2,3 stroke-width:2px,stroke-dasharray: 5 5
    linkStyle 4,5,6,7 stroke-width:2px

In this example, Employee A supervises Employees B and C, while Employee D supervises Employees E and F.

The Binary relationship type

This relationship type connects two distinct entity types and is the most common. For instance, consider a WORKS_ON relationship between EMPLOYEE and PROJECT entity types, associating each employee with the projects they’re working on.

graph LR
    subgraph Employees
        EA[Employee A]
        EB[Employee B]
        EC[Employee C]
    end

    subgraph Projects
        P1[Project 1]
        P2[Project 2]
        P3[Project 3]
    end

    EA --> P1
    EA --> P2
    EB --> P3
    EC --> P2
    EC --> P3

The Ternary relationship type

When three entity types are linked, we have a ternary relationship. An example could be a SUPPLY relationship connecting SUPPLIER, PART, and PROJECT entities. Each relationship instance associates a supplier providing a particular part to a specific project.

graph TD
    subgraph Suppliers
        SA[Supplier A]
        SB[Supplier B]
    end

    subgraph Parts
        PX[Part X]
        PY[Part Y]
        PZ[Part Z]
    end

    subgraph Projects
        P1[Project 1]
        P2[Project 2]
        P3[Project 3]
    end

    SA --> PX --> P1
    SA --> PY --> P2
    SB --> PY --> P2
    SB --> PZ --> P3

    classDef default fill:#f9f,stroke:#333,stroke-width:2px;
    classDef suppliers fill:#bbf,stroke:#333,stroke-width:2px;
    classDef parts fill:#bfb,stroke:#333,stroke-width:2px;
    classDef projects fill:#fbb,stroke:#333,stroke-width:2px;
    class SA,SB suppliers;
    class PX,PY,PZ parts;
    class P1,P2,P3 projects;

Think of these relationship degrees as the organizational structure in a company. Just as employees interact with each other (unary), with projects (binary), and with suppliers and parts for specific projects (ternary), entities in your data model interact in increasingly complex ways.

Binary Relationship Type Constraints

While entities form connections, these connections are often subject to organizational rules or specific scenarios. Imagine a university where each student must enroll in exactly one major. Such rules are termed “constraints” on relationship types within our schema.

These constraints limit the possible combinations of entities that can participate in a given relationship set. Two primary types of binary relationship constraints exist: mapping multiplicity and participation.

Mapping Multiplicity

Mapping multiplicity describes the maximum number of entities that can be associated with a given entity via a relationship. For binary relationships, we consider three types of multiplicity:

  1. One-to-One (1:1): In a relationship between entity sets A and B, each entity in A is associated with at most one entity in B, and vice versa. For example, in a university database, a STUDENT entity might have a 1:1 relationship with a DORM_ROOM entity, as each student occupies at most one dorm room, and each dorm room houses at most one student.
erDiagram
    STUDENT ||--|| DORM_ROOM : OCCUPIES
    STUDENT {
        string StudentID PK
        string Name
    }
    DORM_ROOM {
        string RoomNumber PK
        string Building
    }
  1. One-to-Many (1:N): Here, each entity in A can be associated with multiple entities in B, but each entity in B is associated with at most one entity in A. In our university example, a PROFESSOR might TEACH multiple COURSES, but each COURSE is taught by only one PROFESSOR.
graph TD
    subgraph Professors
        P1[Professor Smith]
        P2[Professor Johnson]
        P3[Professor Lee]
    end

    subgraph Courses
        C1[CS101: Intro to Programming]
        C2[CS201: Data Structures]
        C3[CS301: Algorithms]
        C4[CS401: Artificial Intelligence]
        C5[CS501: Machine Learning]
    end

    P1 -->|Teaches| C1
    P1 -->|Teaches| C2
    P2 -->|Teaches| C3
    P2 -->|Teaches| C4
    P3 -->|Teaches| C5

    classDef professor fill:#f9f,stroke:#333,stroke-width:2px;
    classDef course fill:#bbf,stroke:#333,stroke-width:2px;
    class P1,P2,P3 professor;
    class C1,C2,C3,C4,C5 course;
  1. Many-to-Many (M:N): In this case, each entity in A can be associated with multiple entities in B, and vice versa. For instance, STUDENT entities might have an ENROLLS_IN relationship with COURSE entities, as students can enroll in multiple courses and courses can have multiple students.
graph TD
    subgraph Students
        S1[Student 1]
        S2[Student 2]
        S3[Student 3]
    end

    subgraph Courses
        C1[Course A]
        C2[Course B]
        C3[Course C]
    end

    S1 -->|Enrolls in| C1
    S1 -->|Enrolls in| C2
    S2 -->|Enrolls in| C1
    S2 -->|Enrolls in| C3
    S3 -->|Enrolls in| C2
    S3 -->|Enrolls in| C3

    classDef student fill:#f9f,stroke:#333,stroke-width:2px;
    classDef course fill:#bbf,stroke:#333,stroke-width:2px;
    class S1,S2,S3 student;
    class C1,C2,C3 course;

Participation Levels

Participation constraints specify whether an entity’s existence depends on its relationship with another entity. There are two types:

  1. Total Participation: This mandates that every entity in a set must participate in at least one relationship instance. It’s represented by a double line between the entity set and relationship set. For example, in a university system, every COURSE must be TAUGHT_BY a PROFESSOR.
graph LR
    subgraph Courses
        C1[CS101: Intro to Programming]
        C2[CS201: Data Structures]
        C3[CS301: Algorithms]
        C4[CS401: AI]
    end

    subgraph Professors
        P1[Prof. Smith]
        P2[Prof. Johnson]
        P3[Prof. Lee]
    end

    C1 ===|TAUGHT_BY| P1
    C2 ===|TAUGHT_BY| P1
    C3 ===|TAUGHT_BY| P2
    C4 ===|TAUGHT_BY| P3

    classDef course fill:#bbf,stroke:#333,stroke-width:4px;
    classDef professor fill:#f9f,stroke:#333,stroke-width:2px;
    class C1,C2,C3,C4 course;
    class P1,P2,P3 professor;

    style C1 stroke-dasharray: 5 5
    style C2 stroke-dasharray: 5 5
    style C3 stroke-dasharray: 5 5
    style C4 stroke-dasharray: 5 5
  1. Partial Participation: This allows entities to optionally participate in a relationship. It’s represented by a single line. For instance, not every STUDENT may WORK_AS a TEACHING_ASSISTANT.
graph LR
    subgraph Students
        S1[Student 1: Alice]
        S2[Student 2: Bob]
        S3[Student 3: Charlie]
        S4[Student 4: Diana]
        S5[Student 5: Eve]
    end

    subgraph Teaching Assistants
        TA1[TA: Database Systems]
        TA2[TA: Data Structures]
        TA3[TA: Algorithms]
    end

    S1 ---|WORK_AS| TA1
    S3 ---|WORK_AS| TA2
    S5 ---|WORK_AS| TA3

    classDef student fill:#bbf,stroke:#333,stroke-width:2px;
    classDef ta fill:#f9f,stroke:#333,stroke-width:2px;
    class S1,S2,S3,S4,S5 student;
    class TA1,TA2,TA3 ta;

    style S2 stroke-dasharray: 5 5
    style S4 stroke-dasharray: 5 5

Think of these constraints as the rules of engagement in the data world. Just as social interactions are governed by certain norms, these constraints define how entities can interact within your data model.