Many to Many Relations in Database

In summary, a junction table should include any field that is functionally-dependent on the primary keys.
  • #1
WWGD
Science Advisor
Gold Member
7,061
10,690
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
  • #2
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.
 
  • #3
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;
 
  • #4
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.
 
  • #5
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).
 
  • #6
Thanks, all.
 
  • #7
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:

Related to Many to Many Relations in Database

1. What is a many-to-many relationship in a database?

A many-to-many relationship refers to a type of relationship between two tables in a database where each record in one table can be linked to multiple records in another table, and vice versa. It is a common type of relationship in relational databases and is represented using a junction or associative table.

2. How do you create a many-to-many relationship in a database?

To create a many-to-many relationship in a database, you need to first identify the two tables that need to be linked. Then, you need to create a junction or associative table that contains the primary keys of both tables. This table acts as a bridge between the two tables and allows for multiple connections between the records in each table.

3. Can a many-to-many relationship exist between more than two tables?

Yes, a many-to-many relationship can exist between more than two tables in a database. In this case, you will need to create multiple junction or associative tables to connect the records between the different tables.

4. What is the purpose of a many-to-many relationship in a database?

The purpose of a many-to-many relationship in a database is to model complex data relationships where one record in a table can be associated with multiple records in another table, and vice versa. This allows for more efficient and flexible data management, as it reduces data duplication and allows for more accurate data retrieval.

5. How do you query data from a many-to-many relationship in a database?

To query data from a many-to-many relationship in a database, you will need to use JOIN statements in your SQL query. This will allow you to retrieve data from multiple tables based on the relationships defined in the junction or associative table. You can also use aggregate functions to perform calculations on the data from the linked tables.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
698
  • Programming and Computer Science
Replies
1
Views
873
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
716
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top