this is undoubtedly a newbie question, but I haven't been able to find a satisfactory answer. When creating a link table for many-to-many relationships, is it better to create a unique id or only use two foreign keys of the respective tables (compound key?).
Looking at different diagrams of the Northwind database for example, I've come across both 'versions'. That is: a OrderDetails table with fkProductID and fkOrderID and also versions with an added OrderDetailsID.
What's the difference? (does it also depend on the DB engine?). What are the SQL (or Linq) advantages/disadvantages?
Thanks in advance for an explanation.
Tom
To select records from tables with Multiple foreign keys, you need JOINs. In the same way, you can select values of the Name and Gender columns from the Employee table and the Name column from the Office table using two LEFT JOIN statements on the lookup table Employee_Office.
These foreign key fields are populated with data as records in the join table are created from either table it joins. A typical example of a many-to many relationship is one between students and classes. A student can register for many classes, and a class can include many students.
The FOREIGN KEY constraint differs from the PRIMARY KEY constraint in that, you can create only one PRIMARY KEY per each table, with the ability to create multiple FOREIGN KEY constraints in each table by referencing multiple parent table.
The short answer is no, a table is not allowed to contain multiple primary keys , as that goes against the fundamental principles of relational database design (see: [database normalisation](https://en.wikipedia.org/wiki/Database_normalisation) and [Third normal form](https://en.wikipedia.org/wiki/Third_normal_form) ).
ORMs have been mandating the use of non-composite primary keys to simplify queries...
At first glance, it makes deleting or updating a specific order/etc easier - until you realize that you need to know the applicable id value first. If you have to search for that id value based on an orders specifics then you'd have been better off using the criteria directly in the first place.
In this example, a primary key constraint will ensure that the two columns--fkProductID and fkOrderID--will be unique and indexed (most DBs these days automatically index primary keys if the clustered index doesn't already exist) using the best index possible for the table.
The lone primary key approach means the OrderDetailsID
is indexed with the best index for the table (SQL Server & MySQL call them clustered indexes, to Oracle they're all just indexes), and requires an additional composite unique constraint/index. Some databases might require additional indexing beyond the unique constraint... So this makes the data model more involved/complex, and for no benefit:
I don't see the benefit in a single column primary key over a composite primary key. More work for additional overhead with no net benefit...
I'm used to use PrimaryKey column. It's because the primary key uniquely identify the record. If you have a cascade-update settings on table relations, the values of foreign keys can be changed between "SELECT" and "UPDATE/DELETE" commands sent from application.
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