两个实体之间的多重关系,这是一种好习惯吗?

Multiple relationships between two entities, is this good practice?(两个实体之间的多重关系,这是一种好习惯吗?)

本文介绍了两个实体之间的多重关系,这是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下关于办公室及其员工的典型场景:

I have the following typical scenario regarding an office and its staff:

  • 每个员工都属于一个办公室
  • 每个办公室只有一名经理(一名工作人员)

ER 模型

如您所见,这导致关系被记录两次,一次是 office 表中的外键指向经理,另一次是 staff 表中指向工作人员工作的办公室.

As you can see, this results in the relationship being recorded twice, once for the foreign key in the office table to point to the manager, and also in the staff table pointing to the office that staff member works for.

我已经研究过其他建模方法,但还是有点迷茫.请有人建议一种合适的建模方法,或者我的方法是否适合该场景.

I have looked into alternative ways of modelling this but am still a bit lost. Please could someone advice a suitable way of modelling this, or if my method is acceptable for the scenario.

非常感谢

推荐答案

并不是说关系[被]记录了两次",而是这些表之间实际上有两个关系—这很好.我唯一担心的是,经理可以属于他(她)的经理的同一个办公室吗?(与此相关的是:每个员工都有一个办公室,每个办公室都有一个员工经理,这真的是真的吗?)如果是这样,你有一个循环依赖: 在办公室存在之前不能设置经理办公室,但是在经理存在之前不能设置办公室的经理.只要一个或另一个字段可以为空,您就可以通过应用程序逻辑(INSERT 一个,然后 INSERT 另一个,然后 UPDATE 第一个),但它有点难看.但是,如果这些是存在的关系,那么您就无能为力了.

It's not that "the relationship [is] recorded twice", but that you actually have two relationships between these tables — which is perfectly fine. My only concern is, can a manager belong to the same office that (s)he's the manager of? (And relatedly: is it really true that every staff member has an office and every office has a manager who is a staff member?) If so, you have a circular dependency: you can't set the manager's office until the office exists, but you can't set the office's manager until the manager exists. As long as one or the other field is nullable, you can work around this by application logic (INSERT one, then INSERT the other, then UPDATE the first one), but it's a bit ugly. But if those are the relationships that exist, then there's not much you can do about it.

这篇关于两个实体之间的多重关系,这是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:两个实体之间的多重关系,这是一种好习惯吗?

基础教程推荐