Sie sind auf Seite 1von 3

CSC371: Database Systems – Spring 2019

Assignment – 1 [CLO-C2]: SOLUTION

Querying Relational Database using Relational Algebra


Deadline: March 17, 2019 Max. Marks: 25

In this assignment, you are to write relational algebra queries over a small sample database. It contains
four relations:

Person(name, age, gender) // name is a key

Visits(name, restaurant) // [name,restaurant] is a key

Eats(name, pizza) // [name,pizza] is a key

Serves(restaurant, pizza, price) // [restaurant,pizza] is a key

I have also added few tuples in each of the relations. The schema along with the data is already in the
provided database file (sample.db). You are to write relational algebra expressions over this database for
the following 9 queries. I have also provided the results for each of the query. You need to figure out the
query in relational algebra that must produce the same result.

To verify if your query would produce the same result, I recommend you to execute it using RA interpreter
developed by Prof. Jun Yang at Duke University. Behind the scenes, the RA translates relational algebra
expressions into SQL queries over the database stored in a DBMS (we will use SQLite for this assignment).
Since relational algebra symbols aren't readily available on most keyboards, RA uses a special syntax
described in Relational Algebra Syntax guide (See RA Guide.pdf on how to use RA to verify your queries).

RA interpreter has built in support for SQLite. For this assignment, you neither require any other
installation nor the properties file. sample.db file has everything you need for this assignment. You just
need to place it in the same directory as that of ra.jar. To view the data that I have added in each of
relations, please look at the Database.xlsx file. You do not need Database.xlsx file to run your queries on
RA interpreter, it’s just for you to view the data that is in sample.db file.

1 - Find all pizzas eaten by at least one female over the age of 19. [1.5 marks]
\project_{pizza} (\select_{gender = 'female' and age>19} (Person \join Eats));

Expected Query Result:

cheese
chicken tikka
Mushroom
2 - Find the names of all females who eat at least one pizza served by Rahat. (Note: The pizza need not
be eaten at Rahat.) [1.5 marks]
\project_{name} (\select_{restaurant='Rahat' and gender='female'} (Person \join Eats
\join Serves));

Expected Query Result:

Amna
Hamna

3 - Find all restaurants that serve at least one pizza for less than Rs.1000 that either Amna or Faria (or
both) eat. [1.5 marks]
\project_{restaurant} (\select_{(name='Faria' or name='Amna') and price<1000 } (Eats
\join Serves));

Expected Query Result:

Italian Oven
Pappasallis
Rahat

4 - Find all restaurants that serve at least one pizza for less than Rs.1000 that both Amna and Faria
eat. [1.5 marks]
\project_{restaurant} (\select_{name='Amna' and price<1000 } (Eats \join Serves))
\intersect \project_{restaurant} (\select_{name='Faria' and price<1000 } (Eats \join
Serves));

Expected Query Result:

Italian Oven

5 - Find the names of all people who eat at least one pizza served by Dominos but who do not visit
Dominos. [1.5 marks]
\project_{name} (\select_{restaurant='Dominos'} (Eats \join Serves)) \diff
\project_{name} (\select_{restaurant='Dominos' } (Person \join Visits));

Expected Query Result:

Amna
Bilal
Danish
Ehsan
Gauhar
6 - Find all pizzas that are eaten only by people younger than 24, OR that cost less than Rs.1000
everywhere they're served. [2.5 marks]
(\project_{pizza}(\select_{age<24}(Person \join Eats)) \diff
\project_{pizza}(\select_{age>=24}(Person \join Eats))) \union (\project_{pizza}
Serves \diff \project_{pizza} (\select_{price>=1000} Serves));

Expected Query Result:

cheese
fajita
sausage

7 - Find the age of the oldest person who eat mushroom pizza. [5 marks]
\project_{age1}(\rename_{age1} (\project_{age} (\select_{pizza = 'mushroom'} (Eats
\join Person))) \join_{age1>age2} \rename_{age2} (\project_{age} (\select_{pizza =
'mushroom'} (Eats \join Person)))) \diff \project_{age2}(\rename_{age1}
(\project_{age} (\select_{pizza = 'mushroom'} (Eats \join Person))) \join_{age1>age2}
\rename_{age2} (\project_{age} (\select_{pizza = 'mushroom'} (Eats \join Person)))) ;

Expected Query Result:

24

8 - Find all restaurants that serve ONLY pizzas eaten by people over 30. [5 marks]
\project_{restaurant} (\select_{age>30} (Person \join Eats \join Serves)) \diff
\project_{restaurant}(\project_{restaurant,pizza} Serves \diff
\project_{restaurant,pizza} (\select_{age>30} (Person \join Eats \join Serves)));

Expected Query Result:

Tehzeeb

9 - Find all restaurants that serve EVERY pizza eaten by people over 30. [5 marks]
\project_{restaurant} (\select_{age>30} (Person \join Eats \join Serves)) \diff
\project_{restaurant} ((\project_{restaurant} (\select_{age>30} (Person \join
Eats \join Serves)) \cross \project_{pizza} (\select_{age>30} (Person \join Eats
\join Serves))) \diff \project_{restaurant,pizza} Serves);

Expected Query Result:

Pappasallis
Pizza Hut
Tehzeeb

Das könnte Ihnen auch gefallen