数据库设计:如何通过其他关系来限制an:m关系?
我认为最好根据一个例子来提问:有人员、客户和项目。
人与客户是一种: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个 SQL 示例:
Here's an example in SQL:
在此模型中,
Project
是Assignment
的子类型。例如,分配的类型可以是P = project
或O = Open
。重新分配约束应该在业务逻辑(应用程序层)中处理。从开放式分配切换到项目可以通过关闭该员工分配的期间 (
EndDate
) 并为该员工-客户定义type = project
的新分配来完成组合。In this model the
Project
is a sub-type of theAssignment
. For example, assignment can be of typeP = project
orO = Open
.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 oftype = project
for that employee-customer combination.