我们如何实现 IS-A 关系?
我们通过添加一个表的 PK(作为 FK)到另一个表来实现一对多关系。 我们通过将 2 个表的 PK 添加到第三个表来实现多对多关系。
我们如何实现 IS-A 关系?
这些实体是技术人员和行政人员,两者都是雇员。 我可以在表中使用额外的字段 EMPLOYEE(id, name, surname, role, ...AdminFields..., ...TechFields...)
但我想探索 IS-A 选项。
编辑:我按照唐尼的建议做了,但没有角色字段。
We implement an One-to-Many relationship by adding one Table's PK, as FK to the other Table.
We implement a Many-to-Many relationship by adding 2 Table's PKs to a third Table.
How do we implement an IS-A Relationship ?
The Entities are TECHNICIAN and ADMINISTRATIVE which both are EMPLOYEE.
I could just use an extra field in the Table
EMPLOYEE(id, name, surname, role, ...AdminFields..., ...TechFields...)
but i would like to explore the IS-A option.
EDIT: I did as Donnie suggested, but without the role field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
IS-A 关系也称为 gen-spec 设计模式。 Gen-spec 是“泛化专业化”的缩写。
gen-spec 的关系建模与 gen-spec 的对象建模不同,因为关系模型没有内置继承。
这是一篇很好的文章,展示了如何将 gen-spec 实现为表的集合。
http://www.javaguicodexample.com/erdrelationalmodelnotes1.html
特别注意主键的方式都设置在专门的表中。这就是使用这些表格如此简单的原因。
您可以通过 googlin“泛化专业化关系建模”找到很多其他文章。
The IS-A relationship is also known as the gen-spec design pattern. Gen-spec is short for "generalization specialization".
Relational modeling of gen-spec is different from object modeling of gen-spec because the relational model doesn't have inheritance built in.
Here is a good article that shows how to implement gen-spec as a collection of tables.
http://www.javaguicodexample.com/erdrelationalmodelnotes1.html
Pay particular attention to the way primary keys are set up in the specialized tables. That's what makes using these tables so easy.
You can find lots of other articles by googlin "generalization specialization relational modeling".
我总是使用
role
字段来完成此操作,然后使用可选关系。即,表
EMPLOYEE(id,...通用字段...,角色)
然后,对于每个角色:
表
ROLE1(employeeid,...特定字段...)< /code>
这允许您通过单个查询获取一般员工信息,并且需要联接才能获取特定于角色的信息。这样做的一个(较大的)缺点是,如果您需要一份包含所有角色信息的超级报告,那么您就会陷入一堆外部联接中。
I've always done this with a
role
field, and then optional relationships.I.e., table
EMPLOYEE (id, ...generic fields... , role)
And then, for each role:
table
ROLE1 (employeeid, ...specific fields...)
This allows you to get general employee information with a single query, and requires joins to get at the role-specific information. The one (bigish) downside to this is if you need one super-report with all of the role information on it you get stuck with a bunch of outer joins.
如果您有一个 OO 应用程序需要连接到关系后端数据库,我建议您获取 Martin Fowler 的 企业应用架构模式。
他的网站上还有一些相关的注释和图表。具体来说,模式单表继承、类表继承 和具体表继承 描述在数据表中映射 IS-A 的三种策略。
如果您使用 Hibernate 或 JPA,它们支持所有这些的映射,尽管它们有不同的名称。
在这个特定的例子中,我根本不会使用 IS-A。
像员工角色这样的事情最好建模为 HAS-A,因为
有多重角色。
更轻松。
If you have an OO application that you need to connect to a relational back-end database, I'd recommend getting Martin Fowler's Patterns of Enterprise Application Architecture.
He also has some relevant notes and diagrams on his website. Specifically, the patterns Single Table Inheritance, Class Table Inheritance and Concrete Table Inheritance describe three tactics for mapping IS-A in data tables.
If you're using Hibernate or JPA, they support mappings for all of these, though they have different names for them.
In this specific instance, I wouldn't use IS-A at all though.
Things like employee roles are better modeled as HAS-A, as
have multiple roles.
easier.
本文描述了将泛化映射到模式设计中的一些策略。
http://www.sztaki.hu/conferences/ADBIS/3-Eder.pdf
摘要副本:
This paper describes some strategies for mapping generalizations to into schema design.
http://www.sztaki.hu/conferences/ADBIS/3-Eder.pdf
A copy of the abstract:
为什么不将其实现为一对零/一表关系?假设您有一个表,表示名为 Vehicle 的基类,其主键为 VehicleID。然后,您可以拥有任意数量的代表 Vehicle 的所有子类的卫星表,并且这些表也以 VehicleID 作为主键,具有从 Vehicle->Subclass 的 1->0/1 关系。
或者,如果您想让它更简单,并且您确信您只会有几个子类,并且更改的可能性不大,您可以在带有鉴别器类型字段的单个表中表示整个结构。
Why not implement this as a one-to-zero/one table relationship? Let's say you have a table representing a base class called Vehicle, with a primary key of VehicleID. Then, you can have any number of satellite tables representing all the subclasses of Vehicle, and those tables also have VehicleID as their primary key, having a 1->0/1 relationship from Vehicle->Subclass.
Or, if you want to make it simpler and you know for sure that you'll only ever have a few sub classes and there's not much chance of that changing, you could just represent the whole structure in a single table with a discriminator type field.
大多数 ORM 使用单列鉴别器实现 IS-A 关系,根据特定列中的值选择要实例化的子类。对于您的示例,您可能并不是真正指角色,因为通常一个人可以担任许多不同类型的角色。角色通常被建模为has-a关系。如果您确实尝试使用is-a关系(或子类化)来实现它,那么您不可避免地最终不得不做一些更复杂的事情来处理一个人担任混合职位的情况 - 即,一个秘书同时兼任本地 IT 人员,需要两者的权限或属性。
Most ORMs implement the IS-A relationship using a single column discriminator, choosing which subclass to instantiate based on the value in a particular column. With respect to your example, you probably don't really mean role, since typically a person can fill many different types of roles. Roles would typically be modeled as a has-a relationship. If you do try to implement it using is-a relationships (or subclassing) you inevitably end up having to do something more complicated to handle cases where you have a person filling a hybrid position -- i.e., a secretary who also functions as the local IT person, needing permissions or attributes of both.
这取决于您要构建单层次结构还是多层次结构。这是一个硬编码设计,我相信这就是您想要的。
对于单声道(子表有一个父表),其中子表是父表,子表中的 FK 和 PK 相同,并且该键也是父表中的 PK。
对于poly(子表有多个父表),其中子IS-Aparent-1和子IS-Aparent-2,您将有一个复合键(意味着多个主键使表记录唯一),其中规则与每个键的单一层次结构相同。
It depends if you are building a mono-hierarchy or poly-hierarchy. This is a hard coded design, which I believe is what you wanted.
For mono (child table has one parent table), where child IS-A parent, the FK and PK is the same in the child table, and that key is also the PK in the parent table.
For poly (child table has multiple parent tables), where child IS-A parent-1 and child IS-A parent-2, you'll have a composite key (meaning multiple primary keys to make table record unique), where the rule is the same as a mono-hierarchy for each key.
我按照唐尼的建议做了,但没有角色字段,因为它使事情变得复杂。这是最终的实现:
DDL:
ER 图:
在此模型中,没有通用类型的员工。在这里,员工只能是行政或技术人员。
I did as Donnie suggested, but without the role field, because it complicates things. This is the final implementation:
DDL:
ER Diagram:
In this model there are no Employees of Generic Type. Here, an Employee can only be Administrative or Technical.