What foreign keys to add in 1:N relationship - Logical Model

旧街凉风 提交于 2019-12-24 18:13:53

问题


I'm confused about marking down bidirectional 1:N relationship in a logical model.

We have two tables: Employee and Department. Every employee "knows" what department he's working at and every department "knows" what employees work there - in other words a collection of Employee identifiers.

From a formal point of view, do I mark this down by adding FK to both tables?

|EMPLOYEE    |           |DEPARTMENT|
|____________|           |__________|
|PK EMP_ID   |>O------||-|PK DEPT_ID|
|FK DEPT_ID  |           |FK EMP_ID |
|NAME        |           |NAME      |
|...         |           |...       |

回答1:


A FK (foreign key) holds when subrow values in one place must appear somewhere else.

It's up to you do decide what info you want in what tables, ie what business relationship/association you want each table to represent. After you have your table meanings, what business situations can arise & the table meanings determine what database states can arise, ie what the constraints are and what the cardinalities are. We tell the DBMS so it can prevent updates to states that cannot arise.

In this case, what would it mean to have emp_id in Department? If the table means (has characteristic predicate) "department dept_id has employee emp_id..." then (a) dept_id is redundant in Employee & (b) dept_id is not a PK in Department & (c) Department violates normalization principles. If the table means "department dept_id has manager emp_id..." then the PK/FK make sense. But then you happen to run afoul of typical SQL DBMSs (needlessly) not dealing with FK cycles. But you seem to just want "department dept_id has name name...". So no emp_id column in Department. Or maybe you want Employee & Department without each other's ids plus Works_in(emp_id, dep_id) "employee emp_id works in department dept_id" (which involves what FKs?).

PS "Knows" is not a helpful concept. (You seem to suspect this since you put it in scare quotes.) You seem to be trying to capture that you have some meaning(s) in mind involving both employees & departments. Decide what predicates you need to record your situations. What is going on when a FK holds is that given some meanings you picked, if certain values satisfy a certain relationship/association then they also satisfy a certain other relationship/association. "Relationship" used to mean FK, although very common, arises from misunderstanding of the relational model and ER modeling.



来源:https://stackoverflow.com/questions/44764745/what-foreign-keys-to-add-in-1n-relationship-logical-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!