Download MIE253 - Lab 6 Relational Algebra and SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

SQL wikipedia , lookup

Database model wikipedia , lookup

Relational algebra wikipedia , lookup

Relational model wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Transcript
MIE253 - Lab 6 Relational Algebra and SQL
MIE - University of Toronto
Feb 13, 2010
This lab relates SQL with Relational Algebra in the context of the
databases that you have seen in the labs.
Using the DBPedia movie database (used in previous labs), answer the
questions below. For each, give the SQL and Relational Algebra expressions. It is recommended that you debug your SQL query by comparing
and validating the expected query result (by examining the tuples in the
database). Additionally, you are occasionally asked to add tuples to the
database to satisfy the query and you should understand which relations to
add the tuples to.
1. List the first and last names of stars whose first name contains the
string “y”. The SQL for this question is as follows:
SELECT star.fname, star.lname
FROM star
WHERE fname LIKE ’%y%’;
The predicate ’LIKE’ is true when the string matches the pattern. In
the pattern, ’%’ stands for a substring of any length, and ’$’ stands
for any character.
πfname,lname σfname LIKE ’%y%’ (star)
2. Using made-up data, add 2 movies with the year 2013, 2 movies with
the year 2014, and 2 movies with the year 2015. Make one of the 2015
movies have a name containing “y”, and the other not. List the movie
names of movies that will be made between 2013 and 2015, but not
in 2014, while excluding movies from 2015 whose name contains the
string “y”.
Solution:
SELECT movie.name
FROM movie
WHERE movie.year >= 2013
AND movie.year <= 2015
AND movie.year <> 2014
AND NOT (movie.year = 2015 AND movie.name LIKE ’%y%’);
πname σCND (movie)
where CND is the year and name conditions as in the SQL expression.
3. List the starURIs of stars that acted in movies whose title contains
the word “The”. Indicate the relations that contain the required data.
Solution:
SELECT movieStar.starURI
FROM movieStar, movie
WHERE movieStar.movieURI = movie.movieURI
AND movieName LIKE ’%The%’
πstarURI σname LIKE ’%The%’ (movie ./ moviestar)
4. Add tuples to the database to get at least 5 starURIs of star that acted
in movies whose title contains “Shining” and was made before 1990 or
in movies whose title contains “Matrix” and was made after 1990.
Solution:
SELECT movieStar.starURI
FROM movieStar, movie
WHERE movieStar.movieURI = movie.movieURI
AND ( (movieName LIKE ’%Shining%’ AND movie.year < 1990)
OR (movieName LIKE ’%Matrix%’ AND movie.year > 1990) )
πstarURI σCND (movie ./ moviestar)
5. List the first and last names of stars that acted in movies made before
1990. Indicate the relations that contain the required data.
Solution:
SELECT star.fname, star.lname
FROM star, movieStar, movie
WHERE star.starURI = movieStar.starURI
AND movieStar.movieURI = movie.movieURI
AND movie.year < 1990
πfname, lname σyear < 1990 (star ./ moviestar ./ movie)
2
6. List the last name of stars that acted in movies made before 1990 and
whose first name is “Jack”. Add 2 movies and 3 actors to the database
so that 6 tuples are returned by this query (you also have to add the
appropriate connections between movies and actors).
Solution:
SELECT star.lname
FROM star, movieStar, movie
WHERE star.starURI = movieStar.starURI
AND movieStar.movieURI = movie.movieURI
AND star.fname like ’Jack’
AND movie.year < 1990;
πlname σyear < 1990 AND fname LIKE ’Jack’ (star ./ moviestar ./ movie)
3