I am creating a new SQL Server 2008 database. I have two two tables that are related.
The first table looks like this:
BRANDS // table name
BrandID // pk
BrandName // varchar
The second table looks like this:
MODELS // table name
ModelID // pk
ModelDescription // varchar
Every brand will have at least one model and every model will belong to only one brand.
The question is, should I create a junction table like this
BRANDS_MODELS // table name
RecordID // pk
BrandID
ModelID
Or should I modify the MODELS table to include the BrandID like this
MODELS // table name
BrandID //
ModelID // pk
ModelDescription // varchar
Thanks!
If a model belongs to only one brand then you can put the FK to brand on the model table (your second approach). The first way, with the junction table, is for a many-to-many relation.
Based on what you've said so far, I would leave out the junction table and use an ordinary foreign key in the MODELS table.
But if a model could move brands and you needed to maintain a current junction and history, a junction table has advantages over keeping history of the entire MODELS row when just a foreign key changes. Also if other things exist which might be associated with the relationship "entity" more than the MODEL entity it might make more sense to have a junction table. You can always make a unique constraint on ModelID in the junction table to ensure that the same model is not linked to multiple brands. So although a junction table is required to effectively implement a many-to-many relationship, it can also be useful for one-to-many relationships where that relationship itself has attributes.
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