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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)