English 中文(简体)
Database design: How to restrict a n:m relation by other relations?
原标题:

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 :)

最佳回答

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));
问题回答

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





相关问题
SQL Server - How many users do I *really* need?

I m setting up an application, and I m looking into purchasing a license for SQL Server. My question is pretty simple (though may have a complicated answer...) How many users accounts do I really ...

Object Graph / Persistence Question

I m currently working with an online application that requires multiple screens/steps in order to complete. The application domain is modeled using an object graph. Persistence to and from a database ...

Why does stack get truncated in Exception.StackTrace?

Why does the high part of the stack (in Exception.StackTrace) gets truncated? Let s see a simple example: public void ExternalMethod() { InternalMethod(); } public void InternalMethod() { try ...

ASP.NET MVC and Service Oriented Architecture

I would like to know how do i incorporate a feature like wcf within and MVC application. My current idea of the architecture is as follows: EntityFramework -> ASP.NET MVC (Views) ...

热门标签