关系查询:某公司有多名员工

Relationship query: A company has many employees

假设关系被描述为:

A company has many employees

A company has many departments

A department has many employees

所以,像这样;

Company -<< Departments >>- Employees

如果部门 table 的基本结构为:

// Pseduocode
company_id // Foreign key
department_id // Primary key
[employees] // Array or collection of employees

如果我们回到这个短语;

A Company has many employees

这是否意味着员工 table 也需要或需要 company_id 的推荐信?

因此,Employee 将是:

employee_id
company_id  //  I'm not sure if this is requried or not
department_id

我打算希望将此数据抽象为 contracts table,以防员工 freelancers/contracters 等或有多位员工。

但是现在..

我的问题是:

我的员工 table 需要参考 company table,还是通过 department table 暗示公司参考?

非常感谢

问题是:公司和员工之间是否需要直接连接?如果你这样做,添加它,如果不是那么是,通过部门隐含连接。

编辑:

从技术上讲,您的部门 table 不需要员工列表。 Employee table 的每一行都有一个 Department 的引用 ID,这就足够了。

查看 this 了解更多信息。

关系是通过 department 隐含的,因此您在概念上不需要它。添加它,将是非规范化的一个例子,并且会导致出现不一致。例如,您可能有 company_1department_1company_2。现在 employee_1 可能链接到 department_1company_2,因为应用程序代码中有一些 flaw/bug。无法将此约束表达为 SQL 模式,因此您必须使用更复杂的东西,例如触发器或应用程序代码检查等。

但是,有时您只需要有关公司和员工的信息,而不需要有关部门的信息。如果它真的对性能至关重要,那么与部门进行额外的联合以便为员工找到公司或公司的员工可能不会削减它,所以你只需要忍受非规范化。

您可能不需要link与员工为伍。部门和公司之间的关系已经做好了。您可能仅在存在需要此关系的特定情况下才需要它。