Concepts / Database Normalization: Organizing Data for Efficiency

Database Normalization: Organizing Data for Efficiency

A many-to-many relationship exists when records on both sides can connect to multiple records on the opposite side, unlike one-to-many where multiplicity flows in only one direction.

  • Programming

When One-to-Many Breaks Down

A one-to-many relationship works when one record connects to multiple records of another type, but the reverse is not also true. For example, a teacher has many students, and a department has many employees. In each case, the relationship has one entity on one side and many records on the other. Some real-world connections are more flexible: a record on the left can connect to multiple records on the right, and a record on the right can also connect to multiple records on the left. That pattern is a many-to-many relationship.

What do you think happens?

A user can belong to many courses, and each course can contain many users. Which relationship pattern is this?

  • One-to-one
  • One-to-many
  • Many-to-many
Reveal answer

Answer: Many-to-many

Multiplicity exists in both directions: one user can connect to multiple courses, and one course can connect to multiple users.

Tracing the Two-Way Multiplicity

To identify the relationship, ask two questions. First, can one record on the left connect to multiple records on the right? Second, can one record on the right connect to multiple records on the left? If the answer to both questions is yes, the relationship is many-to-many. This two-question check prevents you from forcing a real-world connection into a one-to-many design that cannot represent all of its links.

manymanyUsersCourses
How can one record in each entity connect to multiple records in the other entity?

In an entity-relationship diagram, a many-to-many relationship is shown with a many indicator at both ends of the connecting line. The visual is different from one-to-many, which has a single indicator at one end and a many indicator at the other. The two many indicators show that neither side is limited to a single related record.

Comparing Cardinality Patterns

Relationship patternMultiplicityDiagram indicatorsTypical implementation
One-to-manyOne side connects to many; the other side connects to oneSingle indicator on one end and many indicator on the otherA foreign-key column on the many side
Many-to-manyBoth sides can connect to many recordsMany indicator on both endsA junction table with foreign keys to both original tables

For a one-to-many relationship, the foreign key belongs on the many side. In the source example, a student table can contain teacher_id, so each student record points to one teacher. This works because the student-to-teacher connection follows the required one-to-many pattern. It does not work when both entities need to connect to multiple records.

one to manymany to manyTeacherUserStudentsCourse
What changes in the relationship pattern when multiple records on both sides can be connected instead of only one side allowing multiple matches?

Why Direct Foreign Keys Fail

A direct one-to-many implementation uses one foreign-key column to point from each record on the many side to a record on the one side. The limitation is that one foreign-key column stores only one value per row. If a course_id column is added to the user table, each user row can point to only one course. If a user_id column is added to the course table, each course row can point to only one user. Neither design can represent users belonging to many courses and courses containing many users.

one value each wayone-to-manyone-to-manyUsercourse_id: one valueUsersCourseuser_id: one valueJunction tableforeign keys to both tablesCourses
Where would multiple related foreign-key values be stored if the relationship were modeled directly, and why does a junction table provide the needed structure?

Building the Junction Table

Connecting Users and Courses

Model a system in which users can belong to many courses and courses can contain many users.

Identify both directions: A user can connect to multiple courses, and a course can connect to multiple users. Therefore, the original relationship is many-to-many.

Introduce a third table: Create a junction table, also called an associative entity, between the users table and the courses table.

Add the two foreign keys: The junction table contains a foreign key pointing to the users table and another foreign key pointing to the courses table.

Read the resulting relationships: Users connect to many junction-table records, while each junction-table record points to a user. Courses connect to many junction-table records, while each junction-table record points to a course. The original many-to-many relationship has been divided into two one-to-many relationships.

The junction table allows any record in the users table to connect to any number of records in the courses table while retaining foreign-key links to both original tables.

one-to-manyone-to-manyUsersprimary keyUserCoursestwo foreign keysCoursesprimary key
How does a junction table convert many-to-many connections into two one-to-many relationships using primary and foreign keys?

The important change is not merely adding another column to an existing table. The junction table gives each connection its own record and stores foreign keys pointing to both original tables. Because many junction-table records can point to the same user and many can point to the same course, the two one-to-many relationships together represent the original many-to-many connection.

Design Checks Before Implementation

  1. Name the two entity types involved in the relationship.
  2. Ask whether one record on the first side can connect to multiple records on the second side.
  3. Ask whether one record on the second side can also connect to multiple records on the first side.
  4. If both answers are yes, mark the relationship as many-to-many in the entity-relationship diagram.
  5. Plan a junction table with foreign keys pointing to both original tables.
  6. Confirm that the junction table has converted the design into two one-to-many relationships.
  • Assuming every relationship is one-to-many

    The relationship allows multiple connections from users to courses and from courses to users.

    Fix: Ask the two-direction multiplicity questions before choosing the relationship type.

  • Adding only course_id to the user table

    A single foreign-key column can store only one value per row, so one user cannot point to multiple courses through that column.

    Fix: Use a junction table containing foreign keys to both users and courses.

  • Adding only user_id to the course table

    A course row can point to only one user through that single column, even though a course can contain many users.

    Fix: Represent the connections in a junction table.

  • Ignoring the diagram's cardinality indicators

    The two ends communicate different relationship patterns.

    Fix: A many indicator on both ends signals a many-to-many relationship.

Apply the Relationship Test

MEDIUM

A department has many employees, but each employee belongs to one department. Identify the relationship type and state where the foreign key belongs. Then compare it with a system where users can belong to many courses and courses can contain many users. Identify the second relationship type and name the table structure needed to implement it.

Hints
  • Check whether multiplicity exists in both directions.
  • For one-to-many, the foreign key is placed on the many side.
  • For many-to-many, use a junction table with foreign keys to both original tables.

The department-and-employees case is one-to-many: the employee side is the many side, so a foreign key can be placed in the employee table. The users-and-courses case is many-to-many: both sides can connect to multiple records, so a junction table is required.

Key Takeaways

  1. A one-to-many relationship has multiplicity in only one direction.
  2. A many-to-many relationship exists when records on both sides can connect to multiple records on the opposite side.
  3. Entity-relationship diagrams show many-to-many cardinality with many indicators at both ends of the relationship line.
  4. A single foreign-key column cannot directly store multiple related values for one row.
  5. A junction table resolves many-to-many relationships by creating two one-to-many relationships with foreign keys to both original tables.

Key Takeaways

  • Recognize many-to-many relationships by checking whether multiplicity exists in both directions.
  • Look for many indicators on both ends of the connecting line in an entity-relationship diagram.
  • Do not attempt to represent multiple related records with one foreign-key column.
  • Use a junction table to convert one many-to-many relationship into two one-to-many relationships.
  • Identifying the relationship pattern early helps prevent implementation problems and rework.