数据库设计:如何通过其他关系来限制an:m关系?

发布于 2024-10-07 15:59:12 字数 573 浏览 3 评论 0原文

我认为最好根据一个例子来提问:有人员、客户和项目。

人与客户是一种:m 的关系。人们可以为多个客户工作。

项目与客户是 1:n 的关系。一个项目始终属于某个客户,但一个客户当然可以拥有多个活动项目。

人与客户是一种关系,但受到项目与客户以及人与客户任务的限制。

更多详情: 我们的一些人员为多个客户工作,但只为这些客户的几个项目工作。

假设客户 A 有项目 1、2、3,客户 B 有项目 4、5、6。

现在 Fred 为客户 A 工作项目 1,为客户 B 工作项目 5 和 6。 相反,Tim 在项目 2,3 上为客户 A 工作,在项目 6 上为客户 B 工作。 我们的特别人员 Nick 只为客户 B 工作,但目前未分配给任何项目。客户可以稍后将他分配给一个项目。

现在,我如何确保使用良好的关系数据库设计,我可以将人员分配给没有项目的客户(例如尼克),并且稍后可以将他们分配给任何客户的项目 - 仅限于他们被分配的项目到。

那么我是否需要设计我的表,以便数据库模型确保在不首先将 Nick 分配给客户 A 的情况下不可能将 Nick 分配给项目 1,2 或 3?

感谢您的任何想法:)

I think it's best to ask based on an example: There are people, customers and projects.

People to customers is a n:m relationship. People can work for multiple customers.

Projects to customers is a 1:n relationship. One project always belongs to a certain customers, but a customer of course can have multiple active projects.

People to customers is a n:m relationship, but restricted by project to customer and people to customer assignments.

More details:
Some of our people work for multiple customers, but only for a few projects of those customers.

Say customer A has projects 1,2,3 and customer B has projects 4,5,6.

Now Fred works for customer A on project 1 and for customer B on projects 5 and 6.
Tim, instead, works for customer A on project 2,3 and for customer B on project 6.
Our special guy Nick works only for customer B, but is currently NOT assigned to any project. The customer can assign him to a project later on.

Now, how can I ensure, using a good relational database design, that I can assign people to customers (like Nick) without a project, AND, later on, can assign them to projects of any customer - restricted to those that they are assigned to.

So do I need to design my tables so that the database model ensures that it is not possible to assign Nick to project 1,2 or 3 without first assigning him to customer A?

Thanks for any ideas :)

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

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

发布评论

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

评论(2

回忆追雨的时光 2024-10-14 15:59:12

下面是一个 SQL 示例:

CREATE TABLE Project
 (ProjectID INT NOT NULL PRIMARY KEY, CustomerID INT NOT NULL,
 UNIQUE (ProjectID, CustomerID));

CREATE TABLE EmployeeProject
 (EmployeeID INT NOT NULL, ProjectID INT NOT NULL, CustomerID INT NOT NULL,
  FOREIGN KEY (EmployeeID, CustomerID) REFERENCES EmployeeCustomer (EmployeeID, CustomerID),
  FOREIGN KEY (ProjectID, CustomerID) REFERENCES Project (ProjectID, CustomerID),
  PRIMARY KEY (EmployeeID, ProjectID));

Here's an example in SQL:

CREATE TABLE Project
 (ProjectID INT NOT NULL PRIMARY KEY, CustomerID INT NOT NULL,
 UNIQUE (ProjectID, CustomerID));

CREATE TABLE EmployeeProject
 (EmployeeID INT NOT NULL, ProjectID INT NOT NULL, CustomerID INT NOT NULL,
  FOREIGN KEY (EmployeeID, CustomerID) REFERENCES EmployeeCustomer (EmployeeID, CustomerID),
  FOREIGN KEY (ProjectID, CustomerID) REFERENCES Project (ProjectID, CustomerID),
  PRIMARY KEY (EmployeeID, ProjectID));
木落 2024-10-14 15:59:12

在此模型中,ProjectAssignment 的子类型。例如,分配的类型可以是 P = projectO = Open

  • 每项任务(开放或项目)仅属于一位客户。
  • 可以有多名员工在不同的时间段执行一项任务。

重新分配约束应该在业务逻辑(应用程序层)中处理。从开放式分配切换到项目可以通过关闭该员工分配的期间 (EndDate) 并为该员工-客户定义 type = project 的新分配来完成组合。

替代文字

In this model the Project is a sub-type of the Assignment. For example, assignment can be of type P = project or O = Open.

  • Each assignment (open or project) belongs to one customer only.
  • There can be several employees working on one assignment at different time-periods.

The re-assigning constraint should be handled in the business logic (application layer). Switching from an open assignment to a project can be done by closing the period for that employee assignment (EndDate) and defining a new assignment of type = project for that employee-customer combination.

alt text

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