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.
If your framework wants all tables to have an ID, then go for it. If your team's database standards dictate all tables must have an ID then go for it.
Many-to-many relationshipsThe primary key of the junction table consists of the foreign keys from both table A and table B. For example, the "Authors" table and the "Titles" table have a many-to-many relationship that is defined by a one-to-many relationship from each of these tables to the "TitleAuthors" table.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
With a simple two-column many-to-many mapping, I see no real advantage to having a surrogate key. Having a primary key on (col1,col2)
is guaranteed unique (assuming your col1
and col2
values in the referenced tables are unique) and a separate index on (col2,col1)
will catch those cases where the opposite order would execute faster. The surrogate is a waste of space.
You won't need indexes on the individual columns since the table should only ever be used to join the two referenced tables together.
That comment you refer to in the question is not worth the electrons it uses, in my opinion. It sounds like the author thinks the table is stored in an array rather than an extremely high performance balanced multi-way tree structure.
For a start, it's never necessary to store or get at the table sorted, just the index. And the index won't be stored sequentially, it'll be stored in an efficient manner to be able to be retrieved quickly.
In addition, the vast majority of database tables are read far more often than written. That makes anything you do on the select side far more relevant than anything on the insert side.
No surrogate key is needed for link tables.
One PK on (col1, col2) and another unique index on (col2, col1) is all you need
Unless you use an ORM that can't cope and dictates your DB design for you...
Edit: I answered the same here: SQL: Do you need an auto-incremental primary key for Many-Many tables?
An incremental primary key could be needed if the table is referenced. There might be details in the many-to-many table which needed to be pulled up from another table using the incremental primary key.
for example
PartDevice
----------
ID (PK/auto-increment)
PartID (FK)
DeviceID (FK)
Other Details
It's easy to pull the 'Other Details' using PartDevice.ID as the FK. Thus the use of incremental primary key is needed.
The shortest and most direct way I can answer your question is to say that there will be a performance impact if the two tables you are linking don't have sequential primary keys. As you stated/quoted, the index for the link table will either become fragmented, or the DBMS will work harder to insert records if the link table does not have its own sequential primary key. This is the reason most people put a sequentially incrementing primary key on link tables.
So it seems like if the ONLY job is to link the two tables, the best PK would be the dual-column PK.
But if it serves other purposes then add another NDX as a PK with a foreign keys and a second unique index.
Index or PK is the best way to make sure there are no duplicates. PK lets tools like Microsoft Management Studio do some of the work (creating views) for you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With