[英]How to use FKs correctly in SQL Tables
Table Department
: 餐桌
Department
:
DeptId
Name
Table Team
表
Team
TeamId
*DeptId (FK)
Name
Table Employee
表
Employee
EmpId
*DeptId
*TeamId
I am making some updates on an old project, but I don't know why the old programmer designed those tables like this (like putting both DeptId
and TeamId
in the Employee
table). 我正在对一个旧项目进行一些更新,但是我不知道为什么旧程序员为什么要像这样设计这些表(例如将
DeptId
和TeamId
放在Employee
表中)。
I find this useless because I can get the department from the Team
table, and there's no need to put both FK IDs into the Employee
table, TeamId
is enough. 我发现这没什么用,因为我可以从
Team
表中获取部门,并且不需要将两个FK ID都放入Employee
表中,而TeamId
就足够了。
Is there any other reason that could force me to put both FKs in that table? 还有其他原因可以迫使我将两个FK都放在该表中吗?
Thank you. 谢谢。
As the data model is written, an employee could be a member of a team that is not in his or her department. 在编写数据模型时,员工可能是不在其部门中的团队的成员。
That is probably possible, for instance, if the employee is temporarily on loan. 例如,如果员工暂时借贷,那可能是可能的。
My bigger problem with the data model is that the relationships between employee and team and employee and department vary over time. 数据模型的最大问题是员工与团队,员工与部门之间的关系会随时间变化。 So, I would have three tables for each entity.
因此,我将为每个实体提供三个表。 The only relationship in the tables would be between
team
and department
(because that presumably does not change over time). 表中唯一的关系是
team
和department
之间的关系(因为假定它不会随时间变化)。
Then I would have two junction tables, one employeeDepartments
and one employeeTeams
that capture the changing relationships over time. 然后,我将有两个联结表,一个
employeeDepartments
和一个employeeTeams
,它们捕获随着时间变化的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.