时间表工具数据库标准化并确保数据完整性

发布于 2024-08-23 16:55:25 字数 1179 浏览 4 评论 0原文

我正在创建一个时间表应用程序。我有以下实体(除其他外):

  • 公司
  • 员工 = 与公司关联的员工
  • 客户 = 与公司关联的客户

到目前为止我有以下(缩写)数据库设置:

Company
 - id  
 - name

Employee
 - id  
 - companyId (FK to Company.id)  
 - name

Client
 - id  
 - companyId (FK to Company.id)  
 - name

现在,我希望员工与客户关联,但该客户与该员工工作的公司关联。您如何保证数据库级别的数据完整性?或者我应该仅仅依靠应用程序来保证数据完整性?

我考虑过创建一个像这样的多对多表:

EmployeeClient  
 - employeeId (FK to Employee.id)  
 - companyId  \  (combined FK to Client.companyId, Client.id)
 - clientId   /  

因此,当我插入员工的客户以及员工的公司 ID 时,当客户与员工的公司 ID 不相关时,数据库应该阻止这种情况发生。这有道理吗?因为这仍然不能保证该员工与公司有关联。你如何处理这些事情?

更新
场景如下:

  • 一家公司有多名员工。员工只会与一家公司相关联。

  • 一家公司也有多个客户。客户只会链接到一家公司。

    (可以这么说,公司是一个沙箱)。

  • 公司的员工可以与其公司的客户关联,但前提是该客户是公司客户的一部分。

换句话说:
该应用程序将允许公司创建/添加员工以及创建/添加客户(因此在员工和客户表中使用companyId FK)。接下来,公司将被允许将某些客户分配给其某些员工(EmployeeClient 表)。

想象一下,一名员工为一些客户从事项目,他/她可以为这些客户编写计费时间,但不得允许该员工为雇主(公司)未分配给他们的客户编写计费时间。因此,员工不会自动访问公司的所有客户,而只能访问公司为他们选择的客户。希望这能让我们对此事有更多的了解。

I'm creating a timesheet application. I have the following entities (amongst others):

  • Company
  • Employee = an employee associated with a company
  • Client = a client associated with a company

So far I have the following (abbreviated) database setup:

Company
 - id  
 - name

Employee
 - id  
 - companyId (FK to Company.id)  
 - name

Client
 - id  
 - companyId (FK to Company.id)  
 - name

Now, I want an employee to be associated with a client, but only if that client is associated with the company the employee works for. How would you guarantee this data integrity on a database level? Or should I just depend on the application to guarantee this data integrity?

I thought about creating a many to many table like this:

EmployeeClient  
 - employeeId (FK to Employee.id)  
 - companyId  \  (combined FK to Client.companyId, Client.id)
 - clientId   /  

Thus, when I insert a client for an employee along with the employee's company id, the database should prevent this when the client is not associated with the employee's company id. Does this make sense? Because this still doesn't guarantee the employee is associated with the company. How do you deal with these things?

UPDATE
The scenario is as followed:

  • A company has multiple employees. Employees will only be linked to one company.

  • A company has multiple clients also. Clients will only be linked to one company.

    (Company is a sandbox, so to speak).

  • An employee of a company can be linked to a client of it's company, but only if the client is part of the company's clientele.

In other words:
The application will allow a company to create/add employees and create/add clients (hence the companyId FK in the Employee and Client tables). Next, the company will be allowed to assign certain clients to certain of it's employees (EmployeeClient table).

Imagine an employee working on projects for a few clients for which s/he can write billable hours, but the employee must not be allowed to write billable hours for clients they are not assigned to by their employer (the company). So, employees will not automatically have access to all their company's clients, but only to those that the company has selected for them. Hopefully this has shed some more light on the matter.

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

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

发布评论

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

评论(2

痕至 2024-08-30 16:55:25

如果您想从数据库级别执行此操作,那么我会将逻辑放入存储过程中。然后,存储的过程代码将在适用的情况下将两者关联起来,但这意味着(假设您将员工的外键放入客户表中)客户仅与一名员工关联。这是你想要的吗?

另请注意,表中的员工通过其公司关联与所有此类客户间接关联。如果所有员工都自动与其公司的所有新客户关联,那么您可能只想编写一个查询来检查这一点。

If you want to do it from the database level then I would put the logic in a stored procedure. The stored proc code will then associate the two if applicable but this means that (given you put the foreign key to the employee in the client table) a client is only associated with one employee. Is this what you want?

Also take note though that an employee in your table is indirectly associated with all such clients via its company association. If all employees are automatically associated with all new clients of their company then perhaps you just want to write a query that checks for this.

逆光飞翔i 2024-08-30 16:55:25

(这不是一个答案,但它并不真正适合作为问题评论。)

为您的设计问题提供的数据引出了许多问题:

  • 员工是否与公司和客户相关联?或者...
  • 员工是否仅与客户相关联,并且(因此)公司与该客户相关联?
  • 如果员工和客户与公司相关联,那么员工是否与该公司的所有员工相关联,还是必须进行选择?

更新

就数据建模而言,似乎您所需要做的就是将 EmployeeClient 中的外键扩展为 Employee ,如下所示:

EmployeeClient
 - companyId
 - employeeId
 - clientId

所有三列上的复合主键。

(companyId, clientId) 上的外键进入客户端
将 (companyId, employeeId) 上的外键插入 Employee

因此,EmployeeClient 中定义的所有关系都要求 Client 和 Employee 共享相同的 clientId。

(This is not an answer, but it didn't really fit in as a question comment.)

The data presented for your design question begs a number of questions:

  • Are employees to be associated with companies and clients? Or...
  • Are employees only associated with clients, and (thus) the company associated with that client?
  • If employess and clients are associated with companies, is an employee thus associated with all employees of that company, or must you pick and choose?

Update

As far as data modelling is concerned, it seems like all you need to do is expand the foreign key in EmployeeClient into Employee like so:

EmployeeClient
 - companyId
 - employeeId
 - clientId

Compound primary key on all three columns.

Foreign key on (companyId, clientId) into Client
Foreign key on (companyId, employeeId) into Employee

Thus, all relations defined in EmployeeClient require both Client and Employee to share the same clientId.

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