Many to Many Relations in Database

Click For Summary

Discussion Overview

The discussion revolves around the implementation of many-to-many relationships in relational databases, specifically focusing on the creation and structure of junction tables. Participants explore the standard practices for designing these tables and how to query data involving them.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions whether a junction table should contain only the primary keys of the related tables or if it can include additional fields, suggesting that it may depend on the specifics of the database.
  • Another participant notes that they typically use just the two primary key fields in their junction tables but acknowledges that querying requires knowledge beyond the junction table itself.
  • A participant shares their experience with a classical-music collection database, detailing their use of a junction table that contains only numeric IDs as primary keys and provides an example query for retrieving performances by a specific performer.
  • Another participant describes a more complex database setup involving a "Track" table that links to multiple entities, suggesting that their junction table serves a different conceptual purpose compared to others.
  • One participant introduces the idea that a bridge/junction table should include any fields that are functionally dependent on the primary keys to avoid violating normalization principles, specifically referencing the third normal form.

Areas of Agreement / Disagreement

Participants express differing views on the structure of junction tables, particularly regarding the inclusion of additional fields beyond primary keys. There is no consensus on whether a junction table should strictly contain only primary keys or if it should also include functionally dependent fields.

Contextual Notes

Some participants mention normalization principles and functional dependencies, but the specifics of these concepts and their application to the discussion remain unresolved.

WWGD
Science Advisor
Homework Helper
Messages
7,798
Reaction score
13,104
I am pulling my hair trying to find a straight answer to this, after looking it up in
different books, websites:
Say we have a many-to-many relationship in a RDB (Relational DB). Is there a standard
way of creating a junction, bridge, etc. table? From what I know,. the bridge table will contain
the primary keys of both table. Will the bridge table contain _exactly_ these two fields, or does
this depend on the particulars of the databases involved?

I would also appreciate some help in this: once we have a junction table. Say we want to
conduct a query between the two tables that are in many-to-many relationships. How do we then
conduct a query, do we do it on the join of one of the many-many tables and the junction table?

Thanks.
 
Last edited:
Technology news on Phys.org
WWGD said:
Is there a standard
way of creating a junction, bridge, etc. table? From what I know,. the bridge table will contain
the primary keys of both table. Will the bridge table contain _exactly_ these two fields, or does
this depend on the particulars of the databases involved?
I would usually just use those two fields.
WWGD said:
I would also appreciate some help in this: once we have a junction table. Say we want to
conduct a query between the two tables that are in many-to-many relationships. How do we then
conduct a query, do we do it on the join of one of the many-many tables and the junction table?
That depends. If your junction table just contain the primary keys, setting up a query would usually involve some knowledge outside the junction table. I usually set up a query using all tables and then specify what records I want from one table. The query would then return all the relevant records from the other table.
 
In my database for my classical-music collection, I have a many-to-many relationship between "performer" and "performance", implemented with a junction table "performance_performer" that contains only the numeric IDs (which I use as primary keys) from both tables. A simple query for all performances by a specific performer might look like this:

Code:
select performance.description
from performance, performer, performance_performer
where performer.lastname = "Heifetz"
and performance_performer.performerid = performer.id
and performance.id = performance_performer.performanceid;
 
jtbell said:
In my database for my classical-music collection, I have a many-to-many relationship between "performer" and "performance", implemented with a junction table "performance_performer" that contains only the numeric IDs (which I use as primary keys) from both tables.
I also have a music collection database. In my case I have a table named "Track" that links to several tables: Album, Composer and Performer. In addition, it contains the track number and the track title.
 
In your case, "track" is a physical entity which also can be used to relate other entities. In my case, "performance_performer" is a purely conceptual entity that expresses a many-to-many relationship. A single performance can have more than one performer taking part in it (e.g. soloist(s) and conductor for a concerto), and a single performer can appear in many different performances (usually of different compositions).
 
Thanks, all.
 
I was told today that a bridge/junction table should include, together with both primary keys, any field that is functionally-dependent on those two keys. I think the argument is that not including it would violate some normalization; I think the third.
 
Last edited:

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K