简体   繁体   English

复杂的SQL表关系?

[英]Complex SQL Table Relationships?

I have a problem about database table and query of it. 我对数据库表及其查询有疑问。 Let me tell what this is about. 让我说说这是什么。 It is about project-manager-employee relationship. 这是关于项目-经理-雇员的关系。 Now Let me tell in statements what their relationship... 现在让我在陈述中告诉他们他们之间的关系...

Manager = Manager is a project leader or could be manager of MORE than ONE project. 经理 =经理是项目负责人,或者可以是多个项目的经理。

Employee = (s)He is a person who only works on project or projectS which is managed by Manager. 雇员 =(s)他是仅在由经理管理的项目或projectS上工作的人。

Project = Project is a project which managed by only ONE Manager. 项目 =项目是仅由ONE Manager管理的项目。

Employee - Manager Relationship 员工-经理关系

Manager is also a employee in company right? 经理也是公司的雇员吗? According to this I want their relationship like -> one Employee can be managed by more than ONE Manager. 据此,我希望他们的关系->一名雇员可以由多个经理管理。
All managers are in the same title I mean there is no manager of a manager. 所有经理都具有相同的头衔,我的意思是没有经理。 There is only manager(s) for an employee. 员工只有经理。 For example 5 employees can be managed by a Manager. 例如,一个经理可以管理5名员工。

Employee - Project Relationship 员工-项目关系

Employee can have a project that make sense, also employee can have more than ONE project that's make sense too. 员工可以拥有一个有意义的项目,员工也可以拥有一个以上有意义的项目。 So project can have more than ONE employee too. 因此,项目也可以拥有一个以上的员工。 (reminder: Project can have more than ONE Employee, BUT can NOT be managed by more than ONE Manager). (提醒:项目可以有一位以上的员工,但不能由一位以上的经理来管理)。

Manager - Project Relationship 经理-项目关系

Manager can have more than ONE project. 经理可以拥有多个项目。 BUT Project can NOT be managed by more than ONE Manager. 不能由多个管理器来管理项目。

PS: According to project, same employee in different projects CAN BE managed by more than one manager See my example below... PS:根据项目,不同项目中的同一名员工可以由多个经理来管理。请参见下面的示例...

Android App PROJECT's Manager ----> Patrick Android App PROJECT的经理 ----> Patrick

Android App PROJECT's Employees -----> John, Susan, Megan , Joe. Android App PROJECT的员工 -----> John,Susan, Megan和Joe。

Another Project... 另一个项目...

IOS App PROJECT's Manager ----> Ryan IOS App PROJECT的经理 ----> Ryan

IOS App PROJECT's Employees ----> Claire, Walter, Robert, Megan IOS App PROJECT的员工 ---->克莱尔,沃尔特,罗伯特, 梅根

Now let me show my tables for that... I can't really know what should I do... 现在让我显示我的桌子...我真的不知道该怎么办...

员工-经理-项目现况

In that table I tried to do that "manager_id(FK)" refer to "employee_id(PK)" because Manager is a employee in company. 在该表中,我尝试将“ manager_id(FK)”引用为“ employee_id(PK)”,因为Manager是公司的雇员。

And I have "manager_id(FK)" in Project table to refers "manager_id" in employee table. 我在项目表中有“ manager_id(FK)”来引用员工表中的“ manager_id”。

And which is proj_employee table I didn't clearly understand. 那是我不清楚的proj_employee表。 I found this way in examples from internet. 我在互联网的示例中找到了这种方式。 That way we call them in query. 这样,我们在查询中称呼它们。

NOW let me post my queries I tried but never get a solution... 现在,让我发布我尝试过但无法解决的问题...

        select
    e.name as employee_name,
    pm.name as manager_name
from Proj_Employee as pe
    left outer join Employee as e on e.employee_id = pe.emp_id
    left outer join Project as p on p.project_id = pe.project_id
    left outer join Employee as pm on pm.employee_id = p.manager_id
where pe.project_id = <project id>

I would like to ask this... I want a query which shows me EmployeeName, ManagerName OF choosen project. 我想问一下...我想要一个查询,该查询向我显示EmployeeName,ManagerName OF所选项目。 And if my table is wrong Could you guide me or tell me how can I do that? 如果我的桌子不对,您能指导我或告诉我该怎么做吗?

I have knowledge but couldn't figure it out how can I make this work. 我有知识,但无法弄清楚如何进行这项工作。

I think I see the problem (finally). 我想我终于明白了这个问题。

You said, "And I have "manager_id(FK)" in Project table to refers "manager_id" in employee table." 您说:“并且我在Project表中有“ manager_id(FK)”来引用employee表中的“ manager_id”。

I think that may be the problem. 我认为可能是问题所在。 I think you want manager_id in the project table to be a foreign key to the employee_id column in the employee table, NOT the manager_id column. 我想你想manager_idproject表是一个外键employee_id列的employee表,而不是manager_id列。 But you've got it right in your query. 但是,您已经在查询中找到了正确的位置。


SQL Fiddle demo here: http://www.sqlfiddle.com/#!2/e31fb/1 SQL Fiddle演示在这里:http://www.sqlfiddle.com/#!2/e31fb/1


Not really related to your question, but I just want to take the opportunity to point out that this is a case where having the primary key column in each table named id helps us make "wrong" SQL look like "wrong" SQL. 与您的问题并没有真正的关系,但我只是想借此机会指出,在这种情况下,每个表中的名为id的主键列都会使我们使“错误的” SQL看起来像“错误的” SQL。 "Making Wrong Code Look Wrong" - Joel Spolsky “使代码看起来不正确”-Joel Spolsky

If we expect that: 如果我们期望:

primary keys will be of the form: id 主键的形式为: id

foreign keys will of the form: parent_table_id , or parent_role_id 外键将采用以下形式: parent_table_idparent_role_id

the vast majority of joins will be from foreign key to primary key 绝大多数联接将从外键到主键

then we expect that most joins will be of the form 那么我们期望大多数联接将采用以下形式

child.parent_id = parent.id

and that foreign key definitions will be of the form 并且外键定义将采用以下形式

FK_child_parent_or_role (parent_or_role_id) REFERENCES parent (id)

This makes a foreign key definition like this: 这使得外键定义如下:

FK_project_manager (manager_id) REFERENCES employee (manager_id)

look "wrong" to us. 向我们看“错”。 We expect to see the reference to the id column. 我们希望看到对id列的引用。

You can get a list of (project_name, manager_name, employee_name) with something like this: 您可以使用以下内容获取(project_name,manager_name,employee_name)的列表:

SELECT p.project_name
     , e.name as employee_name
     , m.name as manager_name
FROM project p
INNER JOIN employee m ON p.manager_id = m.employee_id
LEFT JOIN proj_employee pe ON p.project_id = pe.project_id
LEFT JOIN employees e ON pe.emp_id = e.employee_id;

You will get something like this for two projects, one having no employees: 对于两个项目,您将获得类似的内容,其中一个没有员工:

 project_name  | employee_name | manager_name 
---------------+---------------+--------------
 Stack O       | Alice         | Eve
 Stack O       | Bob           | Eve
 Lemon Squeezy |               | Alice

There is no reason to have outer joins that I can see. 我没有理由拥有外部联接。

select p.Project_Name, m.name as Manager, e.name as Employee
from Project P
join Employee M on m.employee_id = p.manager_id
join project_employee pe on pe.project_id = p.project_id
join Employee E on e.employee_id = pe.employee_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM