SQL - 我应该使用联结表吗? [英] SQL - Should I use a junction table or not?

查看:116
本文介绍了SQL - 我应该使用联结表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的SQL Server 2008数据库。我有两个相关的表。

I am creating a new SQL Server 2008 database. I have two two tables that are related.

第一个表如下所示:

 BRANDS // table name
 BrandID // pk
 BrandName // varchar

第二个表如下所示:

 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

或者我应该修改MODELS表以包括像这样的BrandID

Or should I modify the MODELS table to include the BrandID like this

 MODELS // table name
 BrandID // 
 ModelID // pk
 ModelDescription // varchar

谢谢! / p>

Thanks!

推荐答案

如果一个模型只属于一个品牌,那么你可以把FK放在模型表上。第一种方式,连接表,是一个多对多的关系。

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.

这篇关于SQL - 我应该使用联结表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆