数据库结构问题:与祖父母/父母/子表有关系的表
我正在开发一个可追溯系统,用于记录多级产品制造过程中的某些活动。祖父母项目由 2 或 4 个父项目组成,而父项目又由 2 个子项目组成。
在每个级别、制造过程中的不同点,都会对所有三种类型的项目进行泄漏测试 - 针对每种项目记录的信息都是相同的:泄漏率、通过或失败标志、时间戳等。
所以我选择一个LeakTests 表,带有一个通用 FK 字段来保存与测试相关的项目 ID,以及一个指示符来显示 FK 引用的表:
PK: LeakTestID, Int
FK: LeakTestItemID, Int
FK: LeakTestTypeID, Int
LeakageRate, Float
Result, Bit
我是否将每种类型的 FK 存储在不同的表中场地?
PK: LeakTestID, Int
FK: ChildID, Int
FK: ParentID, Int
FK: GrandparentID, Int
LeakageRate, Float
Result, Bit
或者我是否选择 3 x 泄漏测试表,每个级别的项目一个。
我可以看到每种方法的优点和缺点,并且我已经多次改变了主意。
有什么想法吗?恐怕这个系统的需求还没有明确定义,而我的部分工作就是与业务和下游客户进行角力,以将双方固定下来。但事实上,我无法确定数据将如何使用以及需求是否/如何可能发生变化。
澄清:我认为 g-parent/parent/child 位已经让人困惑了 - 这 3 层不是相同的层,它们完全不同,例如:
ATS:
PK: ATSID, Int
MeasurementA char()
MeasurementB char()
MeasurementC char()
SleevedPair:
PK: SleevedPairID, Int
MeasurementD char()
MeasurementE char()
SleevedItem:
PK: SleevedItemID
MeasurementG char()
MeasurementH char()
MeasurementI char()
MeasurementJ char()
MeasurementK char()
...只是碰巧每个对象都有一个或更多与之相关的泄漏测试。
I'm developing a traceability system that records certain activity in the manufacture of a multi-level item. The grand-parent item consists of 2 or 4 parent items, which in turn consists of 2 child items.
At each level, at various points in the manufacturing process, leak tests are conducted on all three types of item - the information recorded against each will be the same: Leakage rate, pass or failure flag, timestamps etc.
So do I opt for one LeakTests table, with a generic FK field to hold the ID of the item the test relates to, along with an indicator to show which table the FK refers to:
PK: LeakTestID, Int
FK: LeakTestItemID, Int
FK: LeakTestTypeID, Int
LeakageRate, Float
Result, Bit
Do I store FKs for each type in a different field?
PK: LeakTestID, Int
FK: ChildID, Int
FK: ParentID, Int
FK: GrandparentID, Int
LeakageRate, Float
Result, Bit
Or do I opt for 3 x Leak test tables, one for each level of item.
I can see various advantages and disadvantages with each, and I've changed my mind several times.
Any thoughts? I'm afraid that the requirements for this system are barely defined, and part of my job is to wrestle with the business and the downstream customers to pin both sides down. But as it is, I can't be sure how the data will be used and if/how the requirements are likely to change.
Clarification: I think the g-parent/parent/child bit has thrown people - the 3 tiers are not identical tiers, they are entirely different, eg:
ATS:
PK: ATSID, Int
MeasurementA char()
MeasurementB char()
MeasurementC char()
SleevedPair:
PK: SleevedPairID, Int
MeasurementD char()
MeasurementE char()
SleevedItem:
PK: SleevedItemID
MeasurementG char()
MeasurementH char()
MeasurementI char()
MeasurementJ char()
MeasurementK char()
...it just happens that each object has one or more leak tests associated with them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个当然都有优点,但我想我喜欢你的最后一个选择,为每个级别的对象使用泄漏表。我的推理是这样的:
你的第一个结构,它标识了 typeID 和 itemID,是不错的,但会导致比 3 个单独的表更大、更难使用的表。
第二个选项有时会有不必要的字段。对于祖父母级别的对象,您必须记录父母和孩子的空值,同样,对于每个孩子,您必须记录父母和祖父母,这可能是其他地方存在的信息。
我不是专家,只是我的两分钱。
There are certainly advantages to each, but I think I like your last option, using a leak table for each level of object. Here's my reasoning:
Your first structure, which identifies a typeID and itemID is decent but would result in a larger, harder to use table than 3 separate ones.
The second option would have sometimes unnecessary fields. For ever grandparent level object you would have to log nulls for parent and child, likewise for every child you would have to log both the parent and grandparent, which is presumably information that exists elsewhere.
I'm no expert though, just my two cents.
我的祖父母(史蒂夫)是我父母(安妮)的父母。
没有理由存储
我可以通过检查我父母的父母来派生我的祖父史蒂夫。
同样,无需存储我的女儿 Amy 和 Sue,因为我只需检查第一个表中的 ParentID 列以获取我的 ID (ParentID = 3)。如果这些不明确的要求发生变化,这最终是灵活的。层次结构唯一不支持的是多个父母。除非每个节点最多可以有一个子节点,否则您只需翻转树并将parentID 定义为ChildID,然后每个子节点可以有多个父节点。但每个父母只能带一个孩子。如果您需要许多父母和许多孩子,则需要一个多对多映射表。
My grandparent (Steve) is my parent's (Anne) parent.
There's no reason to store
I can derive my grandfather Steve by checking my parent's parent.
Likewise, there's no need to store my daughters are Amy and Sue because I just check the parentID column in the first table for my ID (ParentID = 3). This is ultimately flexible if these ambiguous requirements change. The only thing which the hierarchy won't support is multiple parents. Unless every node can have at most one child then you just flip the tree and define the parentID as ChildID and then you can have multiple parents per child. But only one child per parent . If you need many parents and many children you need a many-to-many mapping table.