Search code examples
relational-databaserelationalrelational-algebra

sql -> relational algebra


enter image description here

How do I convert this to relational algebra tree?

What are the logical steps? Do I first need to convert to relational algebra? Or can I go straight from sql to tree?


Solution

  • I would first convert to relational algebra, then convert to the tree.

    Look, the SELECT clause only wants three fields. That's a projection.

    The FROM clause has three relations. That's a Cartesian product.

    The WHERE clause gives a bunch of selections. This is the part where it helps to convert to relational algebra before converting to a tree.

    I have no idea what notation you use in class, but you probably want something that has a general form of

    projection((things-you-want), selection((criteria), selection((criteria),
      selection((criteria), aXbXc))))
    

    or projection of selection of selection of ... stuff resulting from cross products.

    Note, depending on how picky your instructor is, you may have to rename fields. Since both Show and Seat have showNo as an attribute, you may not be allowed to take the cross product before giving them unique names (alternative rules, attributes are uniquely identified by an implicit relation name prefix).

    Furthermore, depending on the purpose of the lesson, you may commute some of these operations. You can do a selection on Booking before taking the cross product as a means of restricting the date range. The end results will be equivalent.

    Anyway, is it really that much extra work to go from sql to relational algebra to tree? I have no doubt that with practice, you could skip the intermediate step. However, since you asked the question in the first place, I would suggest going through the motions. Remember the "show your work" requirement from junior high math teachers for the combining of simple terms that went away in high school? Same rule applies here. I say this as a former grader of CS assignments.