Many to Many Relation Design - Intersection Table Design

后端 未结 11 1466
野的像风
野的像风 2020-12-07 19:20

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 TA         


        
11条回答
  •  遥遥无期
    2020-12-07 20:02

    Use version-1 if your "intersection" actually IS an entity on its own, meaning:

    • it has additional properties
    • you may search for those objects (and not navigating a relation)

    User version-2 if it is purely N-M relation table. In which case also make sure that:

    • you have your PK (CLUSTERED) with the first column relating to the table your search more often: for example if your tables are Person-Address, then I would assume that you would search for all-addresses-of-a-person more often then for all-people-at-this-address. So you should put your PK to include PersonID first
    • you can still have another one-column UNIQUE identifier, but just:

      1. either do not make it a PK
      2. or make it a PK, but specify NON CLUSTERED, so that you can use CLUSTERED for the UNIQUE index covering two referencing columns
      3. unless you use GUIDs by your design, you could then stick to INT IDENTITY column type

    In both cases you may want to create another INDEX covering the 2 columns, but in another order, if you search from the other side of the relation often.

提交回复
热议问题