是否可以有“捷径”?引用跳过表关系?

发布于 2024-11-02 01:57:32 字数 170 浏览 1 评论 0原文

抱歉,

我有以下关系,一个盒子属于一个插槽,一个属于一个架子,一个属于一个冰箱。相反,冰箱有很多架子,有很多槽,有一个盒子。

对于某些操作,不需要知道盒子在冰箱内的位置。如果我有一条从盒子到冰箱的捷径,这会简化一些查询。

在盒子表中放置冰箱的外键是不是很糟糕?如果是的话为什么?

Sorry for the title

I have the following relation, a box belongs to a slot, that belongs to a rack, that belongs to a fridge. The other way around, Fridges have many Racks, that have many slots, that have one box.

For some operations, knowing where the box is within the fridge is not necessary. It would simplify some queries if I had a short-cut from boxes to fridges.

Is it bad, and if so why, to have a foreign key to the fridge in the boxes table?

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

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

发布评论

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

评论(2

在风中等你 2024-11-09 01:57:32

这可能取决于关系更新的频率。如果关系可能经常更改,那么您将需要在多个位置更新关系,从而产生开销。这也会使事情变得稍微困难​​一些,因为如果您更改一个,则必须确保所有出现的情况也都更新。

但是,如果这些值不会经常更改,那么它似乎可以使您的查询更快。

看起来是数据库规范化的一个很好的例子,他们使用的例子相当不错接近你的。如果您对标准化感兴趣,Wikipedia 关于此的文章非常好。

It could depend on how often the relation will be updated. If the relation is likely to change frequently then you would have the overhead of having to update the relationship in several places. This can also make things slightly more difficult too because if you change one you have to make sure that all occurrences are updated as well.

However, if the values are not going to change that often it seems like it could make your queries faster.

This looks like it is a pretty good example of database normalization and the example that they use is fairly close to yours. If you are interested in normalization the Wikipedia article on this is really good.

血之狂魔 2024-11-09 01:57:32

通过在两个不同的地方记录冰箱与盒子的关联,您可能会产生矛盾。因此,您要么必须创建额外的约束逻辑以确保冗余数据一致,要么接受数据可能是错误的。

By recording the fridge association with a box in two different places you create the possibility of a contradiction arising. Therefore you would either have to create extra constraint logic to ensure the redundant data was consistent or accept that the data might be wrong.

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