问题
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