Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

database - SQL - Should I use a junction table or not?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...