Skip to main content

10.3 - Relationship Types

One-to-One

  • Definition: Each row in Table A is linked to one and only one row in Table B.
  • Example: A User table and a UserProfile table, where each user has one profile.
Deep Dive: One-to-One Relationship Example

Users Table:

UserIDUserName
1Alice
2Bob

UserProfiles Table:

ProfileIDBio
1"Bio for Alice"
2"Bio for Bob"

Here, UserID in the UserProfiles table is a foreign key that references UserID in the Users table, illustrating a one-to-one relationship. One user would only have one unique user ID and one unique profile ID.

One-to-Many

  • Definition: A row in Table A can have multiple linked rows in Table B.
  • Example: A Customer table and an Orders table, where each customer can have multiple orders.
Deep Dive: One-to-Many Relationship Example

Customers Table:

CustomerIDNameEmail
1John Doejohn@example.com
2Jane Smithjane@example.com

Orders Table:

OrderIDOrderDateCustomerID
1012023-07-011
1022023-07-021
1032023-07-032

Here, CustomerID in the Orders table is a foreign key that references CustomerID in the Customers table, illustrating a one-to-many relationship where each customer can have multiple orders.

Many-to-Many

  • Definition: Rows in Table A can have multiple links to rows in Table B and vice versa.
  • Example: A Students table and a Courses table, where each student can enroll in multiple courses and each course can have multiple students.
Deep Dive: Many-to-Many Relationship Example

Students Table:

StudentIDName
1Alice
2Bob

Courses Table:

CourseIDCourseName
101Math
102Science

Enrollments Table (Junction Table):

StudentIDCourseID
1101
1102
2101

Here, the Enrollments table creates a many-to-many relationship between the Students and Courses tables. Each student can enroll in multiple courses and each course can have multiple students.

Self-Referencing Relationship

  • Definition: A table has a relationship with itself (aka. Recursive Relationship).
  • Example: An Employees table where each employee can have a manager who is also an employee.