Coding Notes

Ideas and thoughts from Seth Long

Understanding a SQL Junction Table

My blog is now located at http://www.WhoIsSethLong.com

I was recently asked about a sql junction table and thought I’d share my thoughts about them with the few people who read this.  First off, I am NOT a DBA.  So, if something is not correct or accurate please feel free to correct me.

Junction tables are used when dealing with many-to-many relationships in a SQL database.  If you’re wondering what exactly a many-to-many relationship is, let me try to briefly explain.  Suppose we are working at a school and have a table full of student names and another table full of classrooms.  Each of the students can belong to multiple classrooms or none at all.  Likewise, each classroom can have multiple students or none at all.  This is an example of a many-to-many relationship.

A junction table will allow us to create the many-to-many relationship and most importantly, let us keep from adding duplicate entries as you’ll soon see.

To start, lets create a student table and a classroom table.

CREATE TABLE Students
(
    StudentID int IDENTITY(1,1) PRIMARY KEY,
    StudentName nchar(50) NOT NULL
)

CREATE TABLE Classrooms
(
    ClassroomID int IDENTITY(1,1) PRIMARY KEY,
    RoomNumber int NOT NULL
)

Now that we have our two tables created we need to create the junction table that will link them together.  The junction table is created by using the primary key from the Classrooms and Students tables.

CREATE TABLE StudentClassroom
(
    StudentID int NOT NULL,
    ClassroomID int NOT NULL,
    CONSTRAINT PK_StudentClassroom PRIMARY KEY
    (
        StudentID,
        ClassroomID
    ),
    FOREIGN KEY (StudentID) REFERENCES Students (StudentID),
    FOREIGN KEY (ClassroomID) REFERENCES Classrooms (ClassroomID)
)

We have now created a table with columns for the StudentID and the ClassroomID.  This table also uses a combination of these two columns as the primary key.  This means that each student-classroom pair is unique.  Each student can belong to many classrooms, each classroom can belong to many students but each pair can only occur once.

You should also note that the columns in the junction table are setup as foreign keys to the Students and Classrooms tables.  This is important as it keeps us from adding students to a classroom that doesn’t exist or deleting a classroom from the database if there are still students belonging to it.

To see what students belong to what classrooms we can use the junction table and the following query:

SELECT StudentName, RoomNumber
FROM StudentClassroom
JOIN Students ON Students.StudentID = StudentClassroom.StudentID
JOIN Classrooms ON Classrooms.ClassroomID = StudentClassroom.ClassroomID

So, that’s a junction table in a nut shell.

- Seth Long

January 4, 2008 - Posted by Seth Long | SQL | | 13 Comments

13 Comments »

  1. This nice and simple explanation was exactly what I was looking for.. Thanks!

    Comment by David | March 7, 2008 | Reply

  2. It can be difficult to find simple answers in the huge pool of db information on the web. Thanks! This was really helpful for me.

    Comment by Jack | March 13, 2008 | Reply

  3. Thanks.

    Was helpful. :-)

    Comment by Ryno | March 18, 2008 | Reply

  4. Just a quick question.

    How would one populate the junction table in this case?

    Would you mind showing the query for that too please?

    Comment by Ryno | March 19, 2008 | Reply

  5. This is really simplest and to-the-point, and also comprehensive explanation of the Junction Table. It made my concept very clear about this subject.

    Keep it up, it is a great service to the community.

    Comment by Tariq Changgez | June 10, 2008 | Reply

  6. ur explaination is really very easy…
    i m reading junction tables first time from ur aticle nd its very clear to me

    thanks a lot

    Comment by Rakhi Bansal | July 10, 2008 | Reply

  7. Very well written. Thank-you

    Comment by Remi Nkar | July 16, 2008 | Reply

  8. Very useful thank you.
    I am also wondering though, how would you populate this table?

    Comment by Ben | September 1, 2008 | Reply

  9. Hi

    Can you add some code on how to update or insert new records?

    Comment by Matt | November 25, 2008 | Reply

  10. Wonderful, was frustratingly trying to create a junction table through the visual editor and more importantly set the relationships to the other two tables which seems impossible but to no avail….i should have been using sql….thanks so much

    Good explanation on the junction table!! You are a star disaster!!

    Comment by Lee | February 20, 2009 | Reply

  11. but SELECT StudentName, RoomNumber
    FROM StudentClassroom in this query

    SELECT StudentName, RoomNumber
    FROM StudentClassroom
    JOIN Students ON Students.StudentID = StudentClassroom.StudentID
    JOIN Classrooms ON Classrooms.ClassroomID = StudentClassroom.ClassroomID

    doesn’t seem to be correct isn’t it ?

    Comment by aarklon | July 4, 2009 | Reply

  12. [...] : How to implement a many-to-many relationship using Linq to Sql ? Database Design – Many-to-many Understanding a SQL Junction Table Coding Notes Oracle – What is a junction table and how is it used in Microsoft Access? junction table ebook [...]

    Pingback by Music Collection relationships? - dBforums | August 16, 2009 | Reply

  13. [...] added additional tables to the database for each of these groupings, with associated join tables (what’s a join table?). This would create multiple routes through the hierarchy and make it less clear where data should [...]

    Pingback by Will bids farewell: how to build a course database « Course Tools | September 16, 2009 | Reply


Leave a comment