Converting Relational Algebra to corresponding SQL Query

天大地大妈咪最大 提交于 2019-12-11 08:08:04

问题


I am trying to understand how to convert properly from Relational Algebra into SQL queries

I find this is one of the things I struggle to convert between. Would anyone be able to share any tips?

Tables:

Room(_roomId_, numberOfBeds, price)

Guest(_guestId_, homeTown, age)

Reservation(_roomId_, _guestId_, date)

Relational Algebra:

Πprice(ΠroomId(ΠguestId(σage<20)Guest) Natural Join Reservation) Natural Join Room

Query: I managed to deduce the following from part of the statement:

SELECT roomId FROM (SELECT guestID from Guest WHERE age<20) Natural Join Reservation

Am I close?


回答1:


There are couple of ways that you could look at this. The first option might be easier if you are more familiar with databases then relational algebra, while the second will be easier (and more accurate for more complex problems) if you are familiar with relational algebra.

Tables First:

Start by figuring out your joins. You know that you have your three sets (tables) Guest, Reservation, and Room, all three of with are natural joined (inner joined). So you could start writing you query like so:

SELECT *
FROM Guest g
    INNER JOIN Reservation res
        ON g._guestId_ = res._guestId_
    INNER JOIN Room r
        ON res._roomId_ = r._roomId_;

Once that is complete, apply your conditions:

SELECT *
FROM Guest g
    INNER JOIN Reservation res
        ON g._guestId_ = res._guestId_
    INNER JOIN Room r
        ON res._roomId_ = r._roomId_
WHERE g.age < 20;

Alternatively you can put the condition for g.age in the join to Reservation, but it is recommended to put conditions in the WHERE clause for INNER JOIN.

Finally, you populate your SELECT:

SELECT g._guestId_,
    res._roomId_,
    r.price
FROM Guest g
    INNER JOIN Reservation res
        ON g._guestId_ = res._guestId_
    INNER JOIN Room r
        ON res._roomId_ = r._roomId_
WHERE g.age < 20;

Order of operations

For this, you write your query using order of operations. So everything inside a parenthesis gets executed first. Doing it this way, you start by writing the query against Guest:

SELECT g._guestId_
FROM Guest g
WHERE g.age < 20;

The next set would be Reservations, and that is natural joined:

SELECT g._guestId_,
    res._roomId_
FROM Guest g
    INNER JOIN Reservation res
        ON g._guestId_ = res._guestId_;

Finally, you come to the Room set, again natural joined:

SELECT g._guestId_,
    res._roomId_,
    r.price
FROM Guest g
    INNER JOIN Reservation res
        ON g._guestId_ = res._guestId_
    INNER JOIN Room r
        ON res._roomId_ = r._roomId_
WHERE g.age < 20;



回答2:


Should be something like this ..

SELECT price, _roomId_ FROM room 
inner join Reservation on (reservation._roomId_ = room._roomId_
inner join Guest  on (reservation._guestId = guest._guestId and age <20) ;

In your relation algebra notation you have the project for price and roomId (Select) then you have Natural Join between Guest and Reservation and Natural Join between this cartesian product and Room .. the natural join is espressed in sql by inner join on primary key of both side of the relation .. and last you have a condition on guest age this is tipically a condition managed by where clause or directly in join condition addinng an and clause



来源:https://stackoverflow.com/questions/36678487/converting-relational-algebra-to-corresponding-sql-query

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