简体   繁体   English

SQL多个外键与多个表上的单个外键

[英]SQL multiple foreign keys vs single foreign key on multiple tables

I'm trying to figure out how to define foreign keys in my database. 我试图弄清楚如何在数据库中定义外键。

Let's say I have three tables: 假设我有三个表:

  • Site (this is the site where a company resides), 网站(这是公司所在的网站),
  • Warehouse (on this site there can be multiple Warehouses), and 仓库(在此站点上可以有多个仓库),并且
  • WarehouseLocation (in that warehouse there are multiple locations, eg shelves) WarehouseLocation(在该仓库中有多个位置,例如货架)

Now, 现在,

  • Site-Warehouse is a one-to-many relationship Site-Warehouse是一对多关系
  • Warehouse-WarehouseLocation is a one-to-many relationship Warehouse-WarehouseLocation是一对多关系

When would I describe WarehouseLocation with multiple foreign keys, one to Warehouse.id and one to Site.id ? 我什么时候用多个外键来描述WarehouseLocation ,一个到Warehouse.id ,一个到Site.id

Site --[ Warehouse
  |         ---
  |          |
  +----[ WarehouseLocation

When would I just use: 什么时候使用:

 Site --[ Warehouse --[ WarehouseLocation

In the first option when I lookup a WarehouseLocation I would need the Site.id and the Warehouse.id . 在第一个选项中,当我查询WarehouseLocation我将需要Site.idWarehouse.id

In the second option when I lookup a WarehouseLocation I would need the Warehouse.id , but to lookup the Warehouse I would need the Site.id 在第二个选项中,当我查询WarehouseLocation我需要Warehouse.id ,但是要查询仓库,我需要Site.id

I'm confused about which option is suitable in what situation. 我对哪种选择适合哪种情况感到困惑。 Can someone give me some hints of the pros and cons of both options? 有人可以给我一些两种选择的利弊提示吗?

TL;DR TL; DR

The second option is what you should be looking at. 第二个选项是您应该查看的内容。 That is, 那是,

A WareHouseLocation table would have only the WareHouseID as the foreign key and the WareHouse table would have the SiteID as the foreign key. WareHouseLocation表将仅具有WareHouseID作为外键,而WareHouse表将具有SiteID作为外键。

Explanation 说明

You have to look at it from a functional perspective and not querying perspective. 您必须从功能角度而不是查询角度来看它。 A WareHouseLocation specifies the location of a Warehouse . WareHouseLocation指定Warehouse的位置。 Hence, the relationship makes sense(ie the foreign key seems appropriate). 因此,这种关系是有意义的(即外键似乎是适当的)。 However, if you think about it, the WarehouseLocation really has nothing to do with the Site . 但是,如果您考虑一下, WarehouseLocation确实与Site无关。 Hence, the relationship purely from a functional perspective doesn't make a lot of sense. 因此,纯粹从功能的角度来看,这种关系没有多大意义。

However, from a querying perspective, it looks great as you would need to retrieve SiteID and WareHouseID in most, if not all situations, when querying over the WareHouseLocation table. 但是,从查询的角度看,它看起来很棒,因为在大多数情况下(如果不是全部情况下)在WareHouseLocation表上进行查询时,您需要检索SiteIDWareHouseID Having them readily available in the same table would reduce JOIN s and making querying a much easier task. 使它们在同一表中随时可用将减少JOIN ,并使查询变得容易得多。 This seems to be the crux of your dilemma in terms of database design. 就数据库设计而言,这似乎是您难题的症结所在。

Database design is quite a complex subject and a lot of considerations go into into it, some of which are very specific to the project itself. 数据库设计是一个非常复杂的主题,其中涉及很多考虑因素,其中一些因素非常适合项目本身。 A general rule of thumb is to keep databases as normalized as possible ( especially if you are reading textbooks ). 一般的经验法则是保持数据库尽可能规范化尤其是在阅读教科书时 )。 In practice, however, a lot of database designers prefer to denormalize databases to an extant atleast. 但是,实际上,许多数据库设计人员都希望对数据库进行非规范化,以达到至少现有的水平。 Normalizing/Denormalizing a database is quite an opinionated subject, so I won't delve on it here. 对数据库进行规范化/非规范化是一个自以为是的主题,因此在此我将不再赘述。 You can read more about it in the following posts : 您可以在以下文章中阅读有关它的更多信息:

How far to take normalization in database design? 在数据库设计中标准化要走多远?

How does one know when to stop normalizing? 如何知道何时停止规范化?

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM