Sql-server – the best approach to mapping between two tables with many to many relationship

database-designsql server

What is the best approach to mapping between two tables with many to many relationship? Is it best to create third table with id of each table as foreign key ?
For example student table and course table each student have many courses.

Best Answer

An intersection table such as you've described is the standard way to implement a many to many relationship in a physical relational database.

The intersection table has foreign keys to each of the parent tables that it's joining. These may be simple or compound foreign keys. That doesn't matter.

The primary key of the intersection table is the combination of the two foreign keys to the parent tables. For this reason, the primary key of an intersection table is always a compound foreign key.