DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Query-First Approach in Cassandra
  • Lessons from Migrating an Oracle Database to AWS RDS
  • Handling Embedded Data in NoSQL With Java
  • Designing a Blog Application Using Document Databases

Trending

  • A Guide to Container Runtimes
  • Segmentation Violation and How Rust Helps Overcome It
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Enhancing Avro With Semantic Metadata Using Logical Types
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Handle a Many-to-Many Relationship in Database Design

How to Handle a Many-to-Many Relationship in Database Design

Handling a one-to-one relationship or one-or-many relationship can be done pretty simply. But many-to-many relationships are a bit different. Let’s look at an example.

By 
Ben Brumm user avatar
Ben Brumm
·
Updated May. 10, 22 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
543.8K Views

Join the DZone community and get the full member experience.

Join For Free

A many-to-many relationship occurs when multiple records in a table are related to multiple records in another table.

When normalizing a database or adding tables to an existing database, we need to be able to relate tables to each other.

There are three ways in which a table can be related to another table:

  1. One-to-one: A record in one table is related to one record in another table.
  2. One-to-many: A record in one table is related to many records in another table.
  3. Many-to-many: Multiple records in one table are related to multiple records in another table.

Handling a one-to-one relationship or a one-or-many relationship can be done by adding the primary key of one table into the other table as a foreign key.

However, for many-to-many relationships, it’s a bit different. Let’s have a look at an example.

Many-to-Many Relationships: An Example

Let’s say we are creating a database for a university (which is an example I’ve used often). We capture details about students who attend classes, among other things. The rules are:

  • A student can be enrolled in multiple classes at a time (for example, they may have three or four classes per semester).
  • A class can have many students (for example, there may be 20 students in one class).

This means a student has many classes, and a class has many students.

We can’t add the primary key of one table into the other, or both, because this only stores a single relationship, and we need many.

We couldn’t do this:

 Student ID   Class ID   Student Name 
1 3, 5, 9 John
2 1, 4, 5, 9 Debbie

This would mean we have one column for storing multiple values, which is very hard for maintenance and querying.

We also couldn’t have many columns for class ID values, as this would get messy and create a limit on the number of relationships.

 Student ID   Class ID 1   Class ID 2   Class ID 3   Student Name 
1 3 5 9 John
2 1 4 5 Debbie

Many-to-Many Relationships

So how do we capture this?

We use a concept called a joining table or a bridging table.

A joining table is a table that sits between the two other tables of a many-to-many relationship. Its purpose is to store a record for each of the combinations of these other two tables. It might seem like a bit of work to create, but it’s simple to do and provides a much better data structure.

To create one for this example, we can create a new table called class_enrolment.

Now, the name of the table is important. It’s good to be descriptive with the table. I’ve seen joining tables named with the two names of the other two tables together (such as student_class). I think this is valid and will get the job done, but having a more descriptive name is helpful, as it tells you more about what the table is.

So, we have a new table called class_enrollment. It stores two columns: one for each of the primary keys from the other table.

Our table would look like this:

 Student ID   Class ID 
1 3
1 5
1 9
2 1
2 4
2 5
2 9

This stores separate records for each combination of student and class. Our student and class tables remain the same:

Student:

 Student ID   Student name 
1 John
2 Debbie

Class:

 Class ID   Class name 
1 English
2 Maths
3 Spanish
4 Biology
5 Science
6 Programming
7 Law
8 Commerce
9 Physical Education

Having our data structure in this way makes it easier to add more relationships between tables and to update our students and classes without impacting the relationships between them.

Database design Relational database

Opinions expressed by DZone contributors are their own.

Related

  • Query-First Approach in Cassandra
  • Lessons from Migrating an Oracle Database to AWS RDS
  • Handling Embedded Data in NoSQL With Java
  • Designing a Blog Application Using Document Databases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!