孩子的孩子中是否需要出现外键?

发布于 2024-09-24 02:15:25 字数 468 浏览 1 评论 0原文

我有一组包含子项的子项的表,如下所示:

客户端(PK ClientID)是属性(PK PropertyID,FK ClientID)的父项(一对多)

的父项(一对多)

,它是属性详细信息(PK PropDetailID) ,FK PropertyID)和案例(PK CaseID,FK PropertyID)。

父表的外键是否应该进一步重复?也就是说,我的表应该如下所示:

Clients (PK ClientID)

Property (PK PropertyID, FK Client ID)

PropertyDetail (PK PropDetailID, FK PropertyID, FK Client ID)

Case (PK CaseID, FK PropertyID, FK ClientID)

吗?如果这两种设置都没有标准化,那么标准化的方法是什么?

I have a set of tables with children of children, like so:

Clients (PK ClientID) which is parent (one to many) to

Property (PK PropertyID, FK ClientID) which is parent (one to many) to

Property Detail (PK PropDetailID, FK PropertyID) and Case (PK CaseID, FK PropertyID).

Should the foreign keys for the parent tables be repeated further down? That is, should my tables look like this:

Clients (PK ClientID)

Property (PK PropertyID, FK Client ID)

PropertyDetail (PK PropDetailID, FK PropertyID, FK Client ID)

Case (PK CaseID, FK PropertyID, FK ClientID)

instead? And if neither setup is normalized, what's the normalized way to do this?

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

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

发布评论

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

评论(3

淡淡绿茶香 2024-10-01 02:15:26

不,外键不应重复,因为您可以通过简单的联接来访问此信息。将其添加到孙子中会增加冗余,当两者不同步时可能会出现问题。你的第一个设计看起来比第二个更好。

根据 property 一词的含义,您可能正在使用 实体属性值 (EAV) 模型来存储客户端属性。在某些情况下 EAV 模型是合适的,但一般来说您应该尽量避免使用它。如果可能,请尝试使用固定模式。

进一步阅读:

No, the foreign keys should not be repeated because you can access this information with a simple join. Adding it to the grandchildren adds redundancy which can give problems when the two get out of sync. Your first design looks better than your second.

Depending on the meaning of the word property it could be that you are using an entity attribute value (EAV) model to store client properties. There are some situations where an EAV model is appropriate but in general you should try to avoid it. If possible try to use a fixed schema instead.

Further reading:

诗笺 2024-10-01 02:15:26

您不需要同时拥有 PropertyDetail/Case 的外键。可以导航到这些。

You don't need to have both Foreign keys for PropertyDetail/ Case. These ones can be navigated to.

遮云壑 2024-10-01 02:15:26

无需进一步重复外键——您可以通过查看属性的 ClientID 来确定属性详细信息的 ClientID。

您需要的所有信息都可以通过简单的连接来确定。

There's no need to have the foreign keys repeated further down -- you can determine a property detail's ClientID by looking at the Property's ClientID.

All the information you need can be determined by simple joins.

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