Many to Many Relation Design - Intersection Table Design

database-design, sql, t-sql

Solution

The second version is the best for me, it avoids the creation of an extra index.

Problem

I'm wondering what a better design is for the intersection table for a many-to-many relationship. The two approaches I am considering are: ``` CREATE TABLE SomeIntersection ( IntersectionId UNIQUEIDENTIFIER PRIMARY KEY, TableAId UNIQUEIDENTIFIER REFERENCES TableA NOT NULL, TableBId UNIQUEIDENTIFIER REFERENCES TableB NOT NULL, CONSTRAINT IX_Intersection UNIQUE(TableAId, TableBId ) ) ``` or ``` CREATE TABLE SomeIntersection ( TableAId UNIQUEIDENTIFIER REFERENCES TableA NOT NULL, TableBId UNIQUEIDENTIFIER REFERENCES TableB NOT NULL, PRIMARY KEY(TableAId, TableBId ) ) ``` Are there benefits to one over the other? EDIT 2:****Please Note: I plan to use Entity Framework to provide an API for the database. With that in mind, does one solution work better with EF than the other? EDIT: On a related note, for a intersection table that the two columns reference the same table (example below), is there a way to make the two fields differ on a record? ``` CREATE TABLE SomeIntersection ( ParentRecord INT REFERENCES TableA NOT NULL, ChildRecord INT REFERENCES TableA NOT NULL, PRIMARY KEY(TableAId, TableBId ) ) ``` I want to prevent the following ``` ParentRecord ChildRecord ================================= 1 1 --Cyclical reference! ```

Original source