Assume that we have two tables: Roles and Reports. And there exists
a many-to-many relationship between them. Of course, the only solution
that comes to my mind is to create a cross-table, let’s name it RoleReport.
I can see two approaches to the structure of that table:
1. Columns: RoleReportId, RoleId, ReportId
PK: RoleReportId
2. Columns: RoleId, ReportId
PK: RoleId, ReportId
Is there any real difference between them (performance or whatever else)?
You will need a composite
UNIQUEindex on (RoleId, ReportId) anyway.There is no point in not doing it a
PRIMARY KEY.If you do it a
CLUSTERED PRIMARY KEY(which is default), this will be better performance-wise, since it will be less in size.A clustered primary key will contain only two columns in each record:
RoleIDandReportID, while a secondary index will contain three columns:RoleID,ReportIDandRoleReportID(as a row pointer).You may want to create an additional index on
ReportIDwhich may be used to search allRolesfor a givenReport.There would be some point in making a surrogate key for this relationship if the two following conditions held:
Dateor anything else)FOREIGN KEYIn this case it would be nicer to have a single-column
PRIMARY KEYto refer to inFOREIGN KEYrelationships.Since you don’t seem to have this need, just make a composite
PRIMARY KEY.