Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS SQL creating many-to-many relation with a junction table

I'm using Microsoft SQL Server Management Studio and while creating a junction table should I create an ID column for the junction table, if so should I also make it the primary key and identity column? Or just keep 2 columns for the tables I'm joining in the many-to-many relation?

For example if this would be the many-to many tables:

MOVIE Movie_ID Name etc...  CATEGORY Category_ID Name etc... 

Should I make the junction table:

MOVIE_CATEGORY_JUNCTION Movie_ID Category_ID Movie_Category_Junction_ID 

[and make the Movie_Category_Junction_ID my Primary Key and use it as the Identity Column] ?

Or:

MOVIE_CATEGORY_JUNCTION Movie_ID Category_ID 

[and just leave it at that with no primary key or identity table] ?

like image 544
ab1428x Avatar asked Feb 03 '13 19:02

ab1428x


People also ask

How do you create a table with many-to-many relationships in SQL?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

Can you join 2 tables to create a many-to-many relationship?

For those relationships, you simply connect the appropriate fields with a line. To create many-to-many relationships, you need to create a new table to connect the other two. This new table is called an intermediate table (or sometimes a linking or junction table).

Can you have a many-to-many relationship in SQL?

Many-to-Many relationship lets you relate each row in one table to many rows in another table and vice versa. As an example, an employee in the Employee table can have many skills from the EmployeeSkill table and also, one skill can be associated with one or more employees.

What do you have to do to create a many-to-many relationship between tables A and B?

Many-to-many relationships In a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa. You create such a relationship by defining a third table that is called a junction table. The primary key of the junction table consists of the foreign keys from both table A and table B.


1 Answers

I would use the second junction table:

MOVIE_CATEGORY_JUNCTION Movie_ID Category_ID 

The primary key would be the combination of both columns. You would also have a foreign key from each column to the Movie and Category table.

The junction table would look similar to this:

create table movie_category_junction (   movie_id int,   category_id int,   CONSTRAINT movie_cat_pk PRIMARY KEY (movie_id, category_id),   CONSTRAINT FK_movie        FOREIGN KEY (movie_id) REFERENCES movie (movie_id),   CONSTRAINT FK_category        FOREIGN KEY (category_id) REFERENCES category (category_id) ); 

See SQL Fiddle with Demo.

Using these two fields as the PRIMARY KEY will prevent duplicate movie/category combinations from being added to the table.

like image 145
Taryn Avatar answered Sep 30 '22 06:09

Taryn