SQL - 我是否应该使用连接表?

发布于 2024-11-30 05:18:00 字数 569 浏览 1 评论 0原文

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

第一个表如下所示:

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

第二个表如下所示:

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

每个品牌将至少有一个型号,并且每个型号仅属于一个品牌。

问题是,我应该创建一个像这样的连接表

 BRANDS_MODELS // table name
 RecordID // pk
 BrandID
 ModelID

还是应该修改 MODELS 表以包含像这样的 BrandID

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

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-12-07 05:18:00

如果一个模型只属于一个品牌,那么您可以将 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.

碍人泪离人颜 2024-12-07 05:18:00

根据您到目前为止所说的,我将省略连接表并在 MODELS 表中使用普通外键。

但是,如果模型可以移动品牌,并且您需要维护当前的连接和历史记录,则连接表比仅在外键更改时保留整个 MODELS 行的历史记录具有优势。此外,如果存在其他可能与关系“实体”相关联而不是与模型实体相关联的事物,那么拥有一个联结表可能更有意义。您始终可以对联结表中的 ModelID 进行唯一约束,以确保同一型号不会链接到多个品牌。因此,尽管需要联结表来有效实现多对多关系,但它对于关系本身具有属性的一对多关系也很有用。

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.

兔小萌 2024-12-07 05:18:00

连接表用于多对多关系,这似乎不太适合这里。

例如,您不想启用本田思域和丰田思域的创建。这是汽车品牌/型号关系的示例,但应该适合您的品牌/型号关系。

Junction tables are used for many-to-many relationships which does not seem to be a good fit here.

For example, you would not want to enable the creation of a Honda Civic and a Toyota Civic. That's an example of car's make/model relationship but should fit your brand/model relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文