views:

119

answers:

1

In Master-Detail relationships, do Detail-tables contain ID columns?

Or, should it?

In Northwind database, OrderDetail doesn't have an ID column.

+1  A: 

Any data table that you have that contains something you want to be able to retrieve should always contain a primary key - that's it's main job: uniquely identify a single row.

Just because the details belong to a master row doesn't mean they're not full data rows themselves - they need to be identifiable in some way.

So yes - I would recommend any detail table to have some form of a primary key ("ID"). Whether that's somehow connected to the master-ID or not is something you might decide on a case-by-case basis - but there was to be a way to identify each detail row independently somehow.

If you have a link table for a M:N relationship, typically, you'd only have the two foreign key columns (for tables 1 and 2) in there. That's typically good enough, since the combination of those two FK is unique.

The only reason to add a separate ID to such a table would be if you need to add additional pieces of information that describe the relationship per se. But even then, as long as the (key1, key2) combination is unique, that's typically good enough.

marc_s
In Northwind database, OrderDetail doesn't have an ID column.And what is your comment about having an ID column in the Join-table of a M-to-M relationship?
JMSA
Yes, Northwind.[Order Details] uses the combination of (OrderID,ProductID) as its PK. That works okay as long as you never ever will have two order lines referencing the same product. I don't think this setup is very smart - I personally would use a separate OrderDetailsID in this case.
marc_s