Selecting from three Relation with Relational Algebra

断了今生、忘了曾经 提交于 2019-12-11 09:42:43

问题


I have for ex this Tables

Customer{cid, name, phone, address}
Employee{eid, name, positon, salary}
Resp_for{cid,eid}

Now I want to ask in relational Algebra that it should print out all employees that are resposible for the Customer: 'Obama'

I am trying it but not sure that I understand the relational algebra clearly, so I create this:

PROJECT   (SELECT                 (Customer x Employeee x Resp_for))
       E.eid       C.name = 'Obama'
                 AND R.cid = C.cid
                 And R.eid = E.eid

so how it looks as sql query?

SELECT E.eid
FROM Customer JOIN Employee JOIN Resp_for
WHERE C.name = 'Obama'
      AND R.cid = C.cid
      And R.eid = E.eid

is that correct?


回答1:


The correct SQL query for This wil be:-

SELECT E.EID FROM CUSTOMERS C, EMPLOYEE E, RESP_FOR R
WHERE E.EID = R.EID
AND R.CID = C.CID
AND C.NAME = 'OBAMA';

NOW YOU CAN CONVERT THIS QUERY TO RELATIONAL ALGEBRA.




回答2:


That is correct, though without putting the table aliases (R, C and E) against their respective tables it is unlikely to run.

There is a new form for expressing joins which is more common these days:

SELECT E.eid
FROM Customer C
INNER JOIN Resp_for R
    ON R.cid = C.cid
INNER JOIN Employee E
    ON E.eid = R.eid
WHERE C.name = 'Obama'

Same meaning, different format and IMHO easier to assimilate.



来源:https://stackoverflow.com/questions/23781004/selecting-from-three-relation-with-relational-algebra

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