How to find all pizzerias that serve every pizza eaten by people over 30?

后端 未结 9 1967
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 07:29

I\'m following the Stanford Database course and there\'s a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Re

相关标签:
9条回答
  • 2020-12-29 07:54

    Definitely this is the concept of division operator in relational algebra.

    But I tried on that course. The RA Relational Algebra Syntax doesn't support dev operator. So I used diff and cross instead. Here is my solution:

    \project_{pizzeria}(Serves)
    \diff
    \project_{pizzeria}(
        (\project_{pizzeria}(Serves) 
        \cross 
        \project_{pizza}(\project_{name}(\select_{age>30}(Person))\join Eats))
        \diff
        \project_{pizzeria,pizza}(Serves)
    )
    
    0 讨论(0)
  • 2020-12-29 07:55

    Based on the assumption that all pizzerias serve at least one type of pizza, we will find that the group of pizzas that people over 30 do NOT EAT will be sold by all the pizzerias EXCEPT the one(s) who sell exclusively pizzas which people over 30 do EAT. Did it help?

    0 讨论(0)
  • 2020-12-29 08:05

    Try doing a join using conditions rather than a cross. The conditions would be sure that you match up the records correctly (you only include them if they are in both relations) rather than matching every record in the first relation to every record in the second relation.

    0 讨论(0)
  • 2020-12-29 08:09

    Hi there I found a solution without dividing operator:

    \project_{pizzeria}Serves
    \diff
    \project_{pizzeria}((\project_{pizza}(\select_{age < 30}Person\joinEats)
    \diff\project_{pizza}(\select_{age > 30}Person\joinEats))\joinServes);
    

    ========================================================================

    it's as simple as that. What did I do? in the second part I found pizza list that did not include pizzas that are eaten by those above 30.

    I joined them with pizzerias in order to see which pizzerias make pizza for younger people also.

    I differed that from the original list of pizzerias and the only one that makes pizza for those above 30 is Chicago Pizza.

    0 讨论(0)
  • 2020-12-29 08:12

    The solution is the join div operator http://en.wikipedia.org/wiki/Relational_algebra#Division_.28.C3.B7.29

    See http://oracletoday.blogspot.com/2008/04/relational-algebra-division-in-sql.html

    0 讨论(0)
  • 2020-12-29 08:13
    1. On slide 6, note that n is (3 1 7).

    2. On the next slide, o / n results in (4 8).

    3. If o would also have (12 3) and (12 1) but not (12 7), 12 would not be part of o / n.

    You should be able to fill in an example in the formula on Slide 16 and work it out.

    1. In your case, we take ɑ to be:

      Chicago Pizza   cheese  cheese
      Chicago Pizza   cheese  supreme
      Chicago Pizza   supreme cheese
      Chicago Pizza   supreme supreme
      Dominos         cheese  cheese
      Dominos         cheese  supreme
      
    2. Then we take β to be:

      cheese cheese
      cheese supreme
      supreme cheese
      supreme supreme
      
    3. The result of ɑ / β would then be:

      Chicago Pizza
      

    Dominos is not part of this because it misses (supreme cheese) and (supreme supreme).

    0 讨论(0)
提交回复
热议问题