Junction table: how to model many-to-many in SQL Server

A junction table, or bridge table, stores a many-to-many relationship as one row per pair. How to build one in SQL Server, its key, and the columns it grows.

What is a junction table?

A junction table is a table that resolves a many-to-many relationship between two tables: it stores one row for each pair of related rows, with a foreign key to each table. It goes by other names too: bridge table, associative table, link table, join table. ER modeling calls it an associative entity.

A hotel needs one. A family takes two rooms for the same stay, so one booking holds several rooms, and room 101 is booked by a different guest every few nights, so one room has many bookings. Bookings and rooms are many-to-many, and neither table can hold the other’s key. The hotel management system template has the finished design; the scripts below build it up from three tables.

Why can’t a foreign key hold a many-to-many relationship?

The first design most people write puts the room on the booking:

CREATE TABLE dbo.Room (
  RoomId int NOT NULL CONSTRAINT PK_Room PRIMARY KEY,
  Number nvarchar(10) NOT NULL
);
CREATE TABLE dbo.Guest (
  GuestId  int NOT NULL CONSTRAINT PK_Guest PRIMARY KEY,
  FullName nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Booking (
  BookingId  int  NOT NULL CONSTRAINT PK_Booking PRIMARY KEY,
  GuestId    int  NOT NULL,
  RoomId     int  NOT NULL,
  CheckInOn  date NOT NULL,
  CheckOutOn date NOT NULL,
  CONSTRAINT FK_Booking_Guest_GuestId FOREIGN KEY (GuestId)
    REFERENCES dbo.Guest (GuestId),
  CONSTRAINT FK_Booking_Room_RoomId FOREIGN KEY (RoomId)
    REFERENCES dbo.Room (RoomId)
);
dbo.Guest, dbo.Room and dbo.Booking; dashed lines from Guest and from Room to Booking, no mark at either parent and a ring and a foot at Booking; a legend under the diagram.
One foreign key, one line: a ring and a foot at dbo.Booking, no mark at dbo.Room. Every booking has exactly one room.

A foreign key column holds one value per row, so Booking.RoomId names one room. Read from the room, the line says a room has zero or more bookings ; read from the booking, the end at dbo.Room has no mark , which says a booking has exactly one room, because RoomId is NOT NULL. Ana Ruiz books rooms 101 and 102 for the same two nights, and the second room has nowhere to go:

INSERT INTO dbo.Room (RoomId, Number)
VALUES (1, N'101'), (2, N'102');
INSERT INTO dbo.Guest (GuestId, FullName)
VALUES (1, N'Ana Ruiz');
INSERT INTO dbo.Booking
  (BookingId, GuestId, RoomId, CheckInOn, CheckOutOn)
VALUES (100, 1, 1, '2026-06-12', '2026-06-14');
INSERT INTO dbo.Booking
  (BookingId, GuestId, RoomId, CheckInOn, CheckOutOn)
VALUES (100, 1, 2, '2026-06-12', '2026-06-14');
Msg 2627, Level 14, State 1, Line 8
Violation of PRIMARY KEY constraint 'PK_Booking'. Cannot insert duplicate key in object 'dbo.Booking'. The duplicate key value is (100).
The statement has been terminated.

Booking 100 already exists, so the row for room 102 is a duplicate key. The usual workarounds cost more than the error. A second booking number splits one stay into two, with two check-ins and two bills. A RoomId2 column caps a booking at two rooms, and every query has to look in both columns. Turning the key around, Room.BookingId, gives each room a single booking for as long as the room exists.

Many-to-many needs a foot at both ends of the line. A foreign key can put one only at its own table, because the column it references is unique and so its end is never more than one. The way out is two one-to-many relationships that meet in a third table.

How to create a junction table in SQL Server

dbo.Booking loses its RoomId, and a new table, dbo.BookingRoom, takes one row per room on a booking. This script creates both, in place of the booking table above; dbo.Room and dbo.Guest stay as they were:

CREATE TABLE dbo.Booking (
  BookingId  int  NOT NULL CONSTRAINT PK_Booking PRIMARY KEY,
  GuestId    int  NOT NULL,
  CheckInOn  date NOT NULL,
  CheckOutOn date NOT NULL,
  CONSTRAINT FK_Booking_Guest_GuestId FOREIGN KEY (GuestId)
    REFERENCES dbo.Guest (GuestId)
);
CREATE TABLE dbo.BookingRoom (
  BookingId int NOT NULL,
  RoomId    int NOT NULL,
  CONSTRAINT PK_BookingRoom PRIMARY KEY (BookingId, RoomId),
  CONSTRAINT FK_BookingRoom_Booking_BookingId
    FOREIGN KEY (BookingId)
    REFERENCES dbo.Booking (BookingId),
  CONSTRAINT FK_BookingRoom_Room_RoomId FOREIGN KEY (RoomId)
    REFERENCES dbo.Room (RoomId)
);
dbo.Guest, dbo.Booking, dbo.Room and dbo.BookingRoom; solid lines from Booking and from Room to BookingRoom, no mark at either parent and a ring and a foot at BookingRoom, which has round corners and a primary key of BookingId and RoomId; a dashed line from Guest to Booking.
dbo.BookingRoom has a foreign key to each side and the pair as its primary key, so both its lines are solid and its corners are round.

Each foreign key is an ordinary one-to-many line. Read together, they say a booking has zero or more rooms and a room has zero or more bookings, which is the many-to-many stored as rows:

INSERT INTO dbo.Room (RoomId, Number)
VALUES (1, N'101'), (2, N'102'), (3, N'103');
INSERT INTO dbo.Guest (GuestId, FullName)
VALUES (1, N'Ana Ruiz'), (2, N'Tom Berg');
INSERT INTO dbo.Booking
  (BookingId, GuestId, CheckInOn, CheckOutOn)
VALUES (100, 1, '2026-06-12', '2026-06-14'),
       (101, 2, '2026-06-20', '2026-06-21');
INSERT INTO dbo.BookingRoom (BookingId, RoomId)
VALUES (100, 1), (100, 2), (101, 1);
INSERT INTO dbo.BookingRoom (BookingId, RoomId)
VALUES (100, 1);
INSERT INTO dbo.BookingRoom (BookingId, RoomId)
VALUES (101, 9);
Msg 2627, Level 14, State 1, Line 11
Violation of PRIMARY KEY constraint 'PK_BookingRoom'. Cannot insert duplicate key in object 'dbo.BookingRoom'. The duplicate key value is (100, 1).
The statement has been terminated.
Msg 547, Level 16, State 1, Line 13
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_BookingRoom_Room_RoomId". The conflict occurred in database "Hotel", table "dbo.Room", column 'RoomId'.
The statement has been terminated.

Ana’s booking 100 gets rooms 101 and 102. Tom Berg’s booking 101 gets room 101 a week later, so that room now has two bookings. Room 101 on booking 100 a second time fails on PK_BookingRoom, and the message prints the pair. Room 9 doesn’t exist, so the foreign key to dbo.Room refuses it with Msg 547. Every row has to name a real booking and a real room, and each pair goes in once.

To read the relationship, join through the junction. The rooms on booking 100:

SELECT r.Number
FROM dbo.BookingRoom AS br
JOIN dbo.Room AS r ON r.RoomId = br.RoomId
WHERE br.BookingId = 100;

The query returns 101 and 102. The bookings of one room come from the same join with the filter on br.RoomId instead.

Which primary key should a junction table have?

The pair. PRIMARY KEY (BookingId, RoomId) is a composite key, two columns that together uniquely identify each row, and here it is the business rule “a room is on a booking once” written as a constraint. It also makes both columns NOT NULL, so half a pair is never stored.

Give the table an identity column as its only key, BookingRoomId int IDENTITY, and that rule is gone. On a scratch copy of this table, the pair (100, 1) went in twice with no error, and any query that counts the rooms of a booking or adds up their rates would count room 101 twice. If an ORM or an API wants a one-column id, add it and keep the pair as a unique constraint. Surrogate key vs natural key weighs that choice in general.

Column order matters for reads. PK_BookingRoom is a clustered index sorted by BookingId, then RoomId, so finding the rooms of a booking is an index seek. The bookings of a room are not: the plan for WHERE RoomId = 1 is a clustered index scan, which reads every row. An index that starts with the other column fixes it:

CREATE INDEX IX_BookingRoom_RoomId ON dbo.BookingRoom (RoomId);

With it, the same query is an index seek on IX_BookingRoom_RoomId. The index lists only RoomId, and the query still gets BookingId from it without touching the table, because a nonclustered index on a clustered table stores the clustered key in each of its rows.

When a junction table grows its own columns

Some facts belong to neither side. The rate Ana agreed for room 101 isn’t the room’s rate, which changes with the season, and it isn’t the booking’s, since her booking has two rooms at two prices. It belongs to the pair, and so does the number of adults sleeping in each room. Those columns go on the junction table. This script builds dbo.BookingRoom again with them, in place of the two-column version:

CREATE TABLE dbo.BookingRoom (
  BookingId   int          NOT NULL,
  RoomId      int          NOT NULL,
  NightlyRate decimal(8,2) NOT NULL,
  Adults      tinyint      NOT NULL,
  CONSTRAINT PK_BookingRoom PRIMARY KEY (BookingId, RoomId),
  CONSTRAINT FK_BookingRoom_Booking_BookingId
    FOREIGN KEY (BookingId)
    REFERENCES dbo.Booking (BookingId),
  CONSTRAINT FK_BookingRoom_Room_RoomId FOREIGN KEY (RoomId)
    REFERENCES dbo.Room (RoomId)
);
dbo.Guest, dbo.Booking, dbo.Room and dbo.BookingRoom; BookingRoom has primary key BookingId and RoomId and the columns NightlyRate and Adults; solid lines from Booking and from Room to BookingRoom.
The same key and the same two solid lines; NightlyRate and Adults sit under the key as facts about the pair.

The key and both lines are the same as before. With columns of its own, though, the junction table is an entity like any other, and other tables start to point at it. Copying the rate at booking time also keeps an old bill right after the hotel changes its prices.

Room nights: a key that isn’t the pair

A hotel sells nights, and a room can’t be sold twice for the same night. Two date ranges that overlap are hard to refuse with a constraint, so each room on a booking gets one row per night, keyed by the room and the night:

CREATE TABLE dbo.RoomNight (
  RoomId    int  NOT NULL,
  NightOf   date NOT NULL,
  BookingId int  NOT NULL,
  CONSTRAINT PK_RoomNight PRIMARY KEY (RoomId, NightOf),
  CONSTRAINT FK_RoomNight_BookingRoom_BookingId_RoomId
    FOREIGN KEY (BookingId, RoomId)
    REFERENCES dbo.BookingRoom (BookingId, RoomId)
);
dbo.Guest, dbo.Booking, dbo.Room, dbo.BookingRoom and dbo.RoomNight; a dashed line from BookingRoom to RoomNight with no mark at BookingRoom and a ring and a foot at RoomNight, whose primary key is RoomId and NightOf and whose BookingId is a foreign key column outside it.
The key of dbo.RoomNight is RoomId and NightOf. Its foreign key to dbo.BookingRoom is BookingId and RoomId, and BookingId sits outside the key, so the line is dashed.

dbo.RoomNight points at a row of the junction, so it carries the junction’s whole key, (BookingId, RoomId). RoomId does two jobs: it is half of that foreign key and half of the night’s primary key. Because BookingId stays out of the key, the relationship is non-identifying, a dashed line in crow’s foot notation.

INSERT INTO dbo.Room (RoomId, Number)
VALUES (1, N'101'), (2, N'102'), (3, N'103');
INSERT INTO dbo.Guest (GuestId, FullName)
VALUES (1, N'Ana Ruiz'), (2, N'Tom Berg');
INSERT INTO dbo.Booking
  (BookingId, GuestId, CheckInOn, CheckOutOn)
VALUES (100, 1, '2026-06-12', '2026-06-14'),
       (101, 2, '2026-06-13', '2026-06-14');
INSERT INTO dbo.BookingRoom
  (BookingId, RoomId, NightlyRate, Adults)
VALUES (100, 1, 129.00, 2), (100, 2, 99.00, 1),
       (101, 1, 129.00, 1);
INSERT INTO dbo.RoomNight (RoomId, NightOf, BookingId)
VALUES (1, '2026-06-12', 100), (1, '2026-06-13', 100),
       (2, '2026-06-12', 100), (2, '2026-06-13', 100);
INSERT INTO dbo.RoomNight (RoomId, NightOf, BookingId)
VALUES (1, '2026-06-13', 101);
INSERT INTO dbo.RoomNight (RoomId, NightOf, BookingId)
VALUES (3, '2026-06-13', 100);
Msg 2627, Level 14, State 1, Line 16
Violation of PRIMARY KEY constraint 'PK_RoomNight'. Cannot insert duplicate key in object 'dbo.RoomNight'. The duplicate key value is (1, 2026-06-13).
The statement has been terminated.
Msg 547, Level 16, State 1, Line 18
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_RoomNight_BookingRoom_BookingId_RoomId". The conflict occurred in database "Hotel", table "dbo.BookingRoom".
The statement has been terminated.

Booking 100 takes rooms 101 and 102 for the nights of June 12 and 13. This time Tom’s booking 101 arrives on the 13th, while Ana is still there, and it has room 101 on its junction row as well; dbo.BookingRoom accepts that because it holds no dates. His night of the 13th is refused: (1, 2026-06-13) is already in PK_RoomNight, so a double booking shows up as a duplicate key. The last row puts room 103 on booking 100, which never reserved it, and Msg 547 names dbo.BookingRoom. The foreign key checks the room and the booking together, so a night can only go to a room its booking holds. Nothing checks that a night falls between check-in and check-out; the application writes the nights from those two dates.

The bill comes from the same two tables. Each night joins its junction row on both columns and picks up that room’s rate:

SELECT SUM(br.NightlyRate) AS RoomTotal
FROM dbo.RoomNight AS n
JOIN dbo.BookingRoom AS br
  ON br.BookingId = n.BookingId AND br.RoomId = n.RoomId
WHERE n.BookingId = 100;

For booking 100 that is two nights at 129.00 and two at 99.00, a total of 456.00.

How ERDsmith helps

The rules in this post all sit in keys. The pair stops a room going on a booking twice, and (RoomId, NightOf) stops a room being sold twice a night. The first design’s limit showed in its first picture as the bare end at dbo.Room, exactly one room per booking, long before a guest asked for a second room. ERDsmith draws each line and its ends from the columns and keys behind it:

  • Draw both relationships into a new dbo.BookingRoom as identifying and the pair becomes its primary key, each one’s columns joining the key’s end, so the key’s column order is the order you drew them in.
  • Draw a relationship from dbo.BookingRoom to dbo.RoomNight and the night gets both of the junction’s key columns, BookingId and RoomId, in key order: a night can’t point at half a pair.
  • A relationship points at its parent’s primary key or at a unique index you pick, so the night’s foreign key can target PK_BookingRoom, the pair the junction stores once.