2 years ago 2 years ago Sql Share

Using a Junction Table for a Many to Many Relation in SQL

Using a table with only 2 fields to link a many-to-many relationship

I thought this Wikipedia page was helpful for this

https://en.wikipedia.org/wiki/Associative_entity

Like this

Using a Junction Table for a Many to Many Relation in SQL

Quoted from the wiki page (I made the font of the punch-line sentence bigger):

An associative entity is a term used in relational and entity–relationship theory. A relational database requires the implementation of a base relation (or base table) to resolve many-to-many relationships. A base relation representing this kind of entity is called, informally, an associative table [or junction table].

 

Using a Junction Table for a Many to Many Relation in SQL

 
An associative entity (using Chen notation)

As mentioned above, associative entities are implemented in a database structure using associative tables, which are tables that can contain references to columns from the same or different database tables within the same database.

An associative (or junction) table maps two or more tables together by referencing the primary keys (PK) of each data table. In effect, it contains a number of foreign keys (FK), each in a many-to-one relationship from the junction table to the individual data tables.
 
The PK of the associative table is typically composed of the FK columns themselves.
 

Associative tables are colloquially known under many names, including association tablebridge tablecross-reference tablecrosswalkintermediary tableintersection tablejoin tablejunction tablelink tablelinking tablemany-to-many resolvermap tablemapping tablepairing tablepivot table (as used incorrectly in Laravel - not to be confused with the correct use of pivot table in spreadsheets), or transition table

Hi Simon,

This is a great post - junction tables! Some of these ER diagrams were doing my head in the beginning, showing many-to-many relationships directly between two tables, which you cannot do in Microsoft Access without an associated/junction table.

My question, why do people make diagrams showing relationships that you cannot make in database software? This site for example. Do other database software tools allow the creation of many-to-many relationships directly between tables without the use of a junction table?
Thanks :)

As far as I know, in terms of a traditional "relational database" like that would use SQL (like nearly every "normal" [as in "ordinary/usual/common", not "normal" referring to "normalisation" lol though that also would apply here I guess] database was from the 1970s up to fairly recently when there are some newer styles which have become popular esp with "big data"), I think it (the junction table) is the only way it can be done. I know almost nothing about some of the newer tech like "NoSQL" or how things are done with that, it might be possible there(?)

Re why would anyone draw the ER that way - other than the possibility of my last sentence above (which is prob a good idea to research when there's time, esp since I noticed there are somel job ads which mention NoSQL... as a test I just typed nosql into seek.com.au and there are 306 jobs that mention it currently, as compared to 11855 jobs if you type sql), I think people would draw them like that only as a presentation tool for non-tech people (like some of management or clients) who want to see the top-level structure but don't know or need to know anything about the technical implementations.
 
That (as a presentation for non-DB-tech ppl) seems like a valid use - I don't think I'd myself draw them like that as an educational "tool" for DB students, since (like you said) I think it probably creates more confusion than it helps, I think I'd try to find some other more block-level diagram if I wanted to show a set of DB relations where some things are many-to-many without including the middle/junction table.... 
In my OP above I should prob also have bolded/made bigger this sentence from the wiki quote since it also is a "punch line" sentence I think


"A relational database requires the implementation of a base relation (or base table) [aka junction/linking/associative/middle table] to resolve many-to-many relationships."

Cover image by Shutterstock

Categories Sql,Coding,Databases,Skills
Byte.Yoga Homepage - Australian Cyber Security Web Magazine

Share This Page

If you liked this page, please share it with others! You can use the links to share on Facebook, Twitter, LinkedIn, Pinterest, and Email. Ther is also an RSS feed to get updates for the website.