3. Types of relationships

  • The relationships used when linking data across tables can be broken down into three main types:
    • One-to-one (1:1)
    • One-to-many (1:M)
    • Many-to-many (M:M)

1. One-to-one (1:1) relationship

  • This is the simplest type of relationship, and it’s the same type of relationship we were looking at when linking our tables previously
  • This type of relationship says that a record in one table can only be linked to one record in another, and vice-versa
  • Example: A company has company vehicles, each employee gets their own company car
    • Tables:
      Employees Table

      employee_idnamecar_id
      1Sarah Miller101
      2James Wilson102

      Cars Table

      car_idmakemodel
      101ToyotaCamry
      102HondaAccord
    • Relationship:

      • car_id in the Employees table is a foreign key that references car_id in the Cars table.
      • Each employee is linked to exactly one car, and each car is assigned to exactly one employee. Thus one car for one employee

2. One-to-many (1:M) relationship

  • This relationship is a little bit more complicated, but is quite common when linking tables
  • This type of relationship says that a record in one table can be linked to many records in another table
  • Example: A company has multiple departments, and each department is made up of multiple employees
    • Tables:
      Departments Table

      department_iddepartment_name
      1Engineering
      2Marketing

      Employees Table

      employee_idnamedepartment_id
      1Sarah Miller1
      2James Wilson1
      3Emily Brown2
    • Relationship

    • department_id in the Employees table is a foreign key that references department_id in the Departments table.

    • Each department can have multiple employees, but each employee can only belong to one department. Thus, one department for many employees

3. Many-to-many (M:M) relationship

  • This is probably the most complicated relationship, but it’s not a huge leap from the existing relationships we know
  • This type of relationship says that multiple records in one table can be associated with multiple records in another
  • Example: Students enroll in multiple courses, and each course can have multiple students.
    • Tables:
      Students Table

      student_idname
      1John Doe
      2Jane Smith

      Courses Table

      course_idcourse_name
      101Database Systems
      102Calculus I
    • Wait, how can we link these?

    • The typical solution would be to link the two tables on their primary keys student_id and course_id

    • However, we can only have one value in a column. So if a student can be in multiple courses, how do we show their course ids?

    • The solution is that we must create a third intermediate table (known as a junction table) to represent the relationship

    • Our intermediate table could be:
      Enrollments Table (Junction table)

      student_idcourse_id
      1101
      1102
      2101
    • Relationship:

      • student_id in the Enrollments table is a foreign key that references student_id in the Students table.
      • course_id in the Enrollments table is a foreign key that references course_id in the Courses table.
      • This allows us to have multiple rows per student ID, meaning a student can be in multiple courses and vice versa.
      • Each student can enroll in multiple courses, and each course can have multiple students enrolled.

4. Examples of different relationships

  • In the following examples, try to identify what the relationship type is and (if necessary) what the junction table might look like
Example 1: User Profiles

Problem:

A website wants to maintain detailed profiles for its users. Each user has exactly one profile, and each profile belongs to exactly one user.

Tables:

Users Table

user_idusernameemail
1alice_johnalice@example.com
2bob_smithbob@example.com

Profiles Table

profile_iduser_idbioprofile_picture
11Love to code!alice.jpg
22Enjoys hiking.bob.jpg

Choosing Primary and Foreign Keys:

  1. Primary Key:

    • In the Users table, user_id serves as the primary key. It uniquely identifies each user.
    • In the Profiles table, profile_id serves as the primary key. It uniquely identifies each profile.
  2. Foreign Key:

    • user_id in the Profiles table is a foreign key referencing user_id in the Users table.
    • Each user has exactly one profile, and each profile is linked to exactly one user.

Resulting Table Structure:

Users Table

Profiles Table

  • Primary Key: profile_id
  • Foreign Key: user_id (refers to user_id in the Users table)
    profile_iduser_idbioprofile_picture
    11Love to code!alice.jpg
    22Enjoys hiking.bob.jpg

Relationship Type:

  • One-to-One (1:1): Each user has one profile, and each profile is associated with exactly one user.
Example 2: Patients and Doctors

Problem:

A hospital wants to track which patients are treated by which doctors. In the hospital a patient can be seen by multiple doctors

Tables:

Patients Table

patient_idname
1Alice Green
2Bob Brown

Doctors Table

doctor_idname
1Dr. Smith
2Dr. Adams

Appointments Table

appointment_idpatient_iddoctor_idappointment_date
1112024-09-15
2222024-09-16

Relationship Type:

  • Many-to-Many (M:M): Each patient can see multiple doctors, and each doctor can see multiple patients.
Example 3: Department and Employees

Problem:

A company wants to organize employees by department.

Tables:

Departments Table

department_iddepartment_name
1HR
2Engineering

Employees Table

employee_idnamedepartment_id
1Sarah Miller1
2John Doe2

Relationship Type:

  • One-to-Many (1:M): Each department can have multiple employees, but each employee belongs to only one department.
Example 4: Courses and Students

Problem:

A university needs to track which students are enrolled in which courses.

Tables:

Courses Table

course_idcourse_name
301Introduction to CS
302Data Structures

Students Table

student_idname
1Alice Johnson
2Bob Brown

Enrollments Table (Junction Table)

student_idcourse_id
1301
1302
2301

Relationship Type:

  • Many-to-Many (M:M): Each student can enroll in multiple courses, and each course can have multiple students.
Example 5: Orders and Products

Problem:

A retail store wants to manage the products in each order.

Tables:

Orders Table

order_idorder_date
10012024-09-01
10022024-09-02

Products Table

product_idproduct_name
501Laptop
502Mouse

Order_Products Table (Junction Table)

order_idproduct_idquantity
10015011
10015022
10025011

Relationship Type:

  • Many-to-Many (M:M): Each order can include multiple products, and each product can appear in multiple orders.