Name CWID Homework Assignment 1 September 16th, 2014 CS425 - Database Organization Results Please leave this empty! 1.1 1.7 1.2 1.8 1.3 1.9 1.4 1.5 1.6 1.10 1.11 1.12 1.13 Sum Instructions • Try to answer all the questions using what you have learned in class • When writing a query, write the query in a way that it would work over all possible database instances and not just for the given example instance! • Some questions are marked as bonus. You do not have to answer these questions to get full points for the assignment. However, you can get bonus points for these questions! Consider the following database schema and example instance: ingredient recipe name Pasta and Meatballs Cheese Soup Burger inventor Le cook The french Cowboys kitchen Italian French American recipe Pasta and Meatballs Pasta and Meatballs Pasta and Meatballs Pasta and Meatballs Cheese Soup Cheese Soup Cheese Soup Burger Burger foodItem item Pasta Meatballs Tomato Sauce Onions Cheese Bread Ground Beef type Wheat product Meat Sauce Vegetables Diary Wheat product Meat calories 20 40 5 1 30 25 45 foodItem Pasta Meatballs Tomato Sauce Onions Onions Cheese Bread Bread Ground Beef ounces 50 10 5 1 4 15 20 10 20 stock foodItem Pasta Meatballs Tomato Sauce Tomato Sauce Cheese shop Aldi Aldi Aldi Walmart Treasury Island price 5 10 3 3 15 Hints: • Underlined attribute form the primary key of a relation • The attribute recipe of relation ingredient is a foreign key to relation recipe. The attribute foodItem of relation ingredient is a foreign key to relation foodItem. • The attribute foodItem of relation stock is a foreign key to relation foodItem. DB - Fall 2014: Page 2 (of 7) Part 1.1 Relational Algebra (Total: 100 Points) Question 1.1.1 (6 Points) Write a relational algebra expression that returns the food items required to cook the recipe “Pasta and Meatballs”. For each such food item return the item paired with the number of ounces required by the recipe. Solution πf oodItem,ounces (σrecipe=0 Pasta and Meatballs0 (ingredient)) Question 1.1.2 (6 Points) Write a relational algebra expression that returns food items that are sold at “Aldi” and their price. Solution πf oodItem,price (σshop=0 Aldi0 (stock)) Question 1.1.3 (6 Points) Write a relational algebra expression that returns food items (item) that are of type “Wheat product” or of type “Meat” and have at least 20 calories per ounce (attribute calories). Solution πitem (σ(type=0 Wheat product0 ∨type=0 M eat0 )∧calories≥20 (f oodItem)) DB - Fall 2014: Page 3 (of 7) Question 1.1.4 (9 Points) Write a relational algebra expression that returns the items and their price for all items of type “Wheat product” sold at Aldi. Solution πitem,price (σshop=0 Aldi0 ∧type=0 W heatproduct0 (f oodItem./ρitem←f oodItem (stock))) Question 1.1.5 (9 Points) Write a relational algebra expression that returns the names of all recipes that contain meat products (food items of type “Meat”). Solution πrecipe (σtype=0 M eat0 (ingredient ><f oodItem=item f oodItem)) Question 1.1.6 (10 Points) Write a relational algebra expression that returns all recipes that contain both “Onions” and “Cheese”. Solution πrecipe (σf oodItem=0 Onions0 (ingredient)) ∩ πrecipe (σf oodItem=0 Cheese0 (ingredient)) DB - Fall 2014: Page 4 (of 7) Question 1.1.7 (10 Points) Write a relational algebra expression that returns the food items that are ingredients for “Cheese Soup” but not for “Burgers”. Solution πf oodItem (σrecipe=0 Cheese Soup0 (ingredient)) − πf oodItem (σrecipe=0 Burger0 (ingredient)) Question 1.1.8 (7 Points) Write a relational algebra expression that returns the total number of ounces for all ingredients per recipe. Solution recipe Gsum(ounces) (ingredient) Question 1.1.9 (10 Points) Write a relational algebra expression that returns the average price of food items per type. For example, this expression should return tuples like (Wheat product, 34.5). Solution typeP rice ← f oodItem ><item=f oodItem stock q ←type Gavg(price) (typeP rice) DB - Fall 2014: Page 5 (of 7) Question 1.1.10 (14 Points) Write a relational algebra expression that returns the number of food item types for which the average calories for all food items of this type is higher than 40. Solution avgCal ←type Gavg(calories) (f oodItem) q ← Gcount(∗) (σavg(calories)>40 (avgCal)) Question 1.1.11 (13 Points) Write a relational algebra expression that returns the total calories per recipe (assume that calories in the foodItem table are given per ounce). Solution tCal ← πrecipe,ounces∗calories→ttlCal (ingredient ><f oodItem=item f oodItem) q ←recipe Gsum(ttlCal) (tCal) Question 1.1.12 (BONUS QUESTION) (5 Points) Write a relational algebra expression that returns the cheapest price for making the recipe “Burger”. Assume that the price in the stock table is given per ounce. Also assume that there might be more than one entry for each food item (e.g., bread and ground Beef) in the stock table, i.e., a food item may be sold by different shops. Solution DB - Fall 2014: Page 6 (of 7) burgerP rice ← πf oodItem,ounces∗price→ttlP rice (σrecipe=0 Burger0 (ingredient./stock)) minP rice ←f oodItem Gmin(ttlP rice) AS t (burgerP rice) q ← Gsum(t) (minP rice) Question 1.1.13 (BONUS QUESTION) (5 Points) Write a relational algebra expression that returns all recipes that can be cooked from ingredients bought from a single shop. Solution shopIng ←recipe,shop Gcount(∗) AS n (ingredients./stock) recIng ←recipe Gcount(∗) AS n (ingredients) q ← πrecipe (shopIng./recIng) DB - Fall 2014: Page 7 (of 7)
© Copyright 2025 Paperzz