我们如何实现 IS-A 关系?

发布于 2024-10-05 19:27:16 字数 285 浏览 3 评论 0原文

我们通过添加一个表的 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 技术交流群。

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

发布评论

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

评论(8

天邊彩虹 2024-10-12 19:27:20

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".

左秋 2024-10-12 19:27:20

我总是使用 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.

老子叫无熙 2024-10-12 19:27:20

如果您有一个 OO 应用程序需要连接到关系后端数据库,我建议您获取 Martin Fowler 的 企业应用架构模式

他的网站上还有一些相关的注释和图表。具体来说,模式单表继承类表继承具体表继承 描述在数据表中映射 IS-A 的三种策略。

如果您使用 Hibernate 或 JPA,它们支持所有这些的映射,尽管它们有不同的名称。

在这个特定的例子中,我根本不会使用 IS-A。

像员工角色这样的事情最好建模为 HAS-A,因为

  1. 您可能希望一个人来
    有多重角色。
  2. 改变一个人的角色将是
    更轻松。

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

  1. You might want a single person to
    have multiple roles.
  2. Changing a person's role will be
    easier.
执手闯天涯 2024-10-12 19:27:20

本文描述了将泛化映射到模式设计中的一些策略。

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:

The richer data models of
object relational databases opens many
more options for the logical design of
a database schema increasing the
complexity of logical database design
enormously. Focusing on generalization
constructs of conceptual models we
explore the performance implications
of the various design alternatives for
mapping generalizations into the
schema of an object-relational
database system.

岁月流歌 2024-10-12 19:27:20

为什么不将其实现为一对零/一表关系?假设您有一个表,表示名为 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.

撩起发的微风 2024-10-12 19:27:20

大多数 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.

听风吹 2024-10-12 19:27:20

这取决于您要构建单层次结构还是多层次结构。这是一个硬编码设计,我相信这就是您想要的。

对于单声道(子表有一个父表),其中子表是父表,子表中的 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.

给不了的爱 2024-10-12 19:27:19

我按照唐尼的建议做了,但没有角色字段,因为它使事情变得复杂。这是最终的实现:

DDL:

CREATE TABLE Employee (
ast VARCHAR(20) not null,
firstname VARCHAR(200) not null,
surname VARCHAR(200) not null,
...
PRIMARY KEY(ast)
);

CREATE TABLE Administrative (
employee_ast VARCHAR(20) not null REFERENCES Employee(ast),
PRIMARY KEY(employee_ast)
);

CREATE TABLE Technical (
employee_ast VARCHAR(20) not null REFERENCES Employee(ast),
...
PRIMARY KEY(employee_ast)
);

ER 图:

ERD

在此模型中,没有通用类型的员工。在这里,员工只能是行政或技术人员。

I did as Donnie suggested, but without the role field, because it complicates things. This is the final implementation:

DDL:

CREATE TABLE Employee (
ast VARCHAR(20) not null,
firstname VARCHAR(200) not null,
surname VARCHAR(200) not null,
...
PRIMARY KEY(ast)
);

CREATE TABLE Administrative (
employee_ast VARCHAR(20) not null REFERENCES Employee(ast),
PRIMARY KEY(employee_ast)
);

CREATE TABLE Technical (
employee_ast VARCHAR(20) not null REFERENCES Employee(ast),
...
PRIMARY KEY(employee_ast)
);

ER Diagram:

ERD

In this model there are no Employees of Generic Type. Here, an Employee can only be Administrative or Technical.

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