正常化还是不正常化?
我有一个数据库,其中有 4 个表。
A-> B-> C-> D
目前我的方式是,A 的主键是 B 中的外键。B 会有它自己的主键,这是 C 中的外键,等等。
但是,C 无法链接到A 没有 B。
问题是,我的程序的核心功能涉及从 A 和 D 中提取匹配条目。
我是否应该在 D 中也包含 A 的主键
这样做会产生不必要的数据重复,因为 A->B-> ;C->D 是层次结构。 请参阅图片了解 D 的外观。
I have a DB in which there are 4 tables.
A -> B -> C -> D
Current the way I have it is, the Primary Key of A is a foreign key in B. And B would have it's own Primary Key, which is a foreign key in C, etc etc.
However, C can't be linked to A without B.
The problem is, a core function of my program involve pulling matching entries from A and D.
Should I include the primary key of A in D too
Doing so will create unnecessary data duplication 'coz A->B->C->D are hierarchy.
see pic for what D would look like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你将所有 D 与给定的 A 相关,我会将其保持标准化。
但是,如果您想要此类 D 的特定子集,并且很容易提前知道哪些子集,但稍后会很耗时(例如,如果您想要来自最新 C 的所有 D 和最新 B 的所有 D),我会预先将此快捷方式存储在某处。
它不必位于 D 本身中(特别是如果您不希望所有 D 都与 A 连接)。
如果您想让查询更易于阅读和编写,请考虑视图。
如果您想通过这样做来提高性能,请尝试一切并进行测量。 (而且我不是 SQL 性能调优方面的专家,所以除此之外我没有具体的建议)
If you take all D-s in relation with given A, I would keep it normalized.
But if you want specific subset of such D-s and its easy to know which in advance, but time consuming later (eg. if you want all D-s from newest C from newest B), I would prefare storing this shortcut somewhere.
It does not have to be in D itself (esp. if you don't want all D-s connected with A).
If you want to do it to make your queries easier to read and write, then consider view.
If you want to do it to increase performance, try everything and measure it. (And I'm not expert in performance tuning of SQL, so I have no specific advice beyond that)