数据库设计-实体关系模型

发布于 2024-11-26 20:45:55 字数 702 浏览 4 评论 0原文

我有以下问题需要表达。

有人员、工作场所和地点。每个人可以被分配到多个工作场所。每个工作场所可以有多人。每个工作场所只有一个站点。到目前为止,一切都很好。但我的问题是每个人在特定地点只有一个工作场所。

我如何在 ERM 中表达这一点?

到目前为止我的想法:

idea

我只是无法表达“一个人在特定地点只有一个工作场所”-这种方法有问题。

实施解决方案:

Table Person with Prs_ID (PK)
Table Site with Site_ID (PK)
Table Workplace with Plc_ID (PK)
Table Person_Site with Prs_Site_PrsID (PK, FK), Prs_Site_SiteID (PK, FK), Prs_Site_PlcID (FK)
Unique Index on Prs_Site_PlcID

我认为这应该可以解决问题。 现在我该如何在 ERM 中表达这一点?

编辑:

我以为这可以解决问题,但事实并非如此。这样我就无法将一个工作场所分配给两个不同的人,因为 Prs_Site_PlcID 列上有一个唯一索引。回到最初...

I have the following problem that I need to express.

There are persons, workplaces and sites. Each person can be assigned to multiple workplaces. Each workplace can have multiple persons. Each workplace has exactly one site. So far so good. But my problem is that each person has only one workplace at a specific site.

How can I express this in an ERM?

My idea so far:

idea

I just can't express the "one person has only one workplace at a specific site"-problem with this approach.

Implementation Solution:

Table Person with Prs_ID (PK)
Table Site with Site_ID (PK)
Table Workplace with Plc_ID (PK)
Table Person_Site with Prs_Site_PrsID (PK, FK), Prs_Site_SiteID (PK, FK), Prs_Site_PlcID (FK)
Unique Index on Prs_Site_PlcID

I think this should solve the problem.
Now how can I express this in an ERM?

Edit:

I thought it would solve the problem but it doesn't. With this I can't assign one workplace to two different persons, because there is a unique index on the Prs_Site_PlcID column. Back to the beginning...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

離人涙 2024-12-03 20:45:55

在此处输入图像描述

注意唯一索引 Ak1(备用键)(SiteID, WorkplaceID )Workplace 上,并传播到 PersonWorkplace

--
-- PostgreSQL
--
create table Site      (SiteId      integer not null);
create table Person    (PersonId    integer not null);
create table Workplace (WorkplaceID integer not null, SiteID integer not null);
create table PersonWorkplace
(PersonID integer not null, SiteID integer not null, WorkplaceID integer not null);

alter table Site   add constraint pk_Sit primary key (SiteID);
alter table Person add constraint pk_Prs primary key (PersonID);

alter table Workplace
  add constraint  pk_Wpl primary key (WorkplaceID)
, add constraint fk1_Wpl foreign key (SiteId) references Site (SiteId)
, add constraint ak1_Wpl unique (SiteID, WorkplaceID);

alter table PersonWorkplace
  add constraint  pk_PrsWpl primary key (PersonId, SiteID)
, add constraint fk1_PrsWpl foreign key (PersonId) references Person (PersonID)
, add constraint fk2_PrsWpl foreign key (SiteID, WorkplaceID) references Workplace (SiteID, WorkplaceID);

enter image description here

Note unique index Ak1 (alternate key) (SiteID, WorkplaceID) on Workplace which is propagated to PersonWorkplace.

--
-- PostgreSQL
--
create table Site      (SiteId      integer not null);
create table Person    (PersonId    integer not null);
create table Workplace (WorkplaceID integer not null, SiteID integer not null);
create table PersonWorkplace
(PersonID integer not null, SiteID integer not null, WorkplaceID integer not null);

alter table Site   add constraint pk_Sit primary key (SiteID);
alter table Person add constraint pk_Prs primary key (PersonID);

alter table Workplace
  add constraint  pk_Wpl primary key (WorkplaceID)
, add constraint fk1_Wpl foreign key (SiteId) references Site (SiteId)
, add constraint ak1_Wpl unique (SiteID, WorkplaceID);

alter table PersonWorkplace
  add constraint  pk_PrsWpl primary key (PersonId, SiteID)
, add constraint fk1_PrsWpl foreign key (PersonId) references Person (PersonID)
, add constraint fk2_PrsWpl foreign key (SiteID, WorkplaceID) references Workplace (SiteID, WorkplaceID);
安静被遗忘 2024-12-03 20:45:55

我认为线索就在问题中。

你说每个工作场所都有一个Site,那么关系就是:

多人有多个工作场所

一个工作场所有一个Site;

实现建议:

Person table
-----------
person_id primary key
.....

Workplace table
--------------
workplace_id primary_key
site_id (unique index)

person_workplace table
-------------------
person_id
workplace_id

site table
--------------
site_id primary key

site_id 列上的唯一索引工作场所表中确保每个工作场所都与不同的站点关联。

I think the clue is in the question.

You say that each work place has one site - the relationships thus are:

many persons have many workplaces

One workplace has one Site;

Implementation suggestion:

Person table
-----------
person_id primary key
.....

Workplace table
--------------
workplace_id primary_key
site_id (unique index)

person_workplace table
-------------------
person_id
workplace_id

site table
--------------
site_id primary key

The unique index on the site_id column in the workplace table ensures that each workplace is associated with a different site.

鱼忆七猫命九 2024-12-03 20:45:55

您需要一个中间表 EMPLOYEES,它代表一个人在工作场所为雇主工作的时间。一个人可以是多名雇员,即为多个雇主工作。白天工作/夜间工作,或连续工作。 EMPLOYEE 不是 person 实体的同义词,而是 person-at-employer 的表示。

YOu need an intermediate table EMPLOYEES which represents a PERSON's stint at a worksite working for an employer. A single person can be many employees, i.e. work for more than one employer. Day job / night job, or jobs in succession. EMPLOYEE is not a synonym for the person entity but is a representation of person-at-employer.

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