首页 > 解决方案 > ER Diagram 1:N relationship Convert

问题描述

If I have Employees and project and relationship one-to-many (many for projects i.e Employees can be assigned to multiple projects and one project is assigned to exactly one employee) the table will be:

Employees (empid,name,phone)

Projects(proid,proname,cost,empid)

can I move proid key to employees table as :

Employees(empid,name,phone,proid)

Projects(proid,proname,cost)

This convert will be wrong or still right? and if the last convert is wrong, why it's wrong?

标签: erd

解决方案


您必须将其添加empidProjects表中。当您将proid列添加到Employees表中时,您只能引用一个项目。如果你有一个员工分配到多个项目,你会得到这样的东西:

Employees(1, "John", "123-456-789", 42)
Employees(1, "John", "123-456-789", 20)

如您所见,您的表中有两次员工,更不用说违反主键了。因此,您必须将员工 ID 保存在Projects表中。

Projects(42, "Research", 10000, 1);
Projects(20, "Analysis", 3000, 1);

推荐阅读