Sunteți pe pagina 1din 2

ETH Zurich

Systems Group
Prof. D. Kossmann

FS 2014
Data Modeling and Databases
Exercise Sheet 3

Relational Algebra and SQL


This exercise sheet will be discussed on 18.03.2014 and 21.03.2014.

Relational Algebra - Task 1

Consider the following relational schema:


Reader ( RDNR, Surname, Firstname, City, Birthdate )
Book ( ISBN, Title, Author, NoPages, PubYear, PublisherName )
Publisher ( PublisherName, PublisherCity )
Category ( CategoryName, BelongsTo )
Copy ( ISBN, CopyNumber, Shelf, Position )
Loan ( ReaderNr, ISBN, Copy, ReturnDate )
BookCategory ( ISBN, CategoryName )
Formulate the following queries in relational algebra:
a) Which are the last names of the readers in Zurich?
b) Which books (Author, Title) are from publishers in Zurich, Bern or New York?
c) Which books (Author, Title) has the reader Lemmi Schmoker borrowed?
d) Which books in the category Alps do not belong to the category Switzerland? Do not take
into account subcategories!
e) Which readers (Surname, Firstname) have borrowed books that were published in their home town?
f) Which readers (Surname, Firstname) have borrowed at least a book that has been borrowed also by
the reader Lemmi Schm
oker (the reader Lemmi Schmoker should not be included in the results)?

Relational Algebra - Task 2

Consider the following relational schema:


Cities (Name, State)
Stations (Name, NoPlatforms, CityName, State)
Itinerary (ItNr, Length, StartStation, DestinationStation)
Connections (FromStation, ToStation, ItNr, Departure, Arrival)
Suppose that the relation Connections already contains the transitive closure. For example, if there
is a train from Zurich via Bern and Fribourg to Geneva, then there exists a relation tuple for Zurich
1

Bern, ZurichFribourg and ZurichGeneva (the same for the other intermediate stations).
Formulate the following queries in relational algebra:
a) Find all the direct connections from Zurich to Geneva
b) Find all the single-transfer connections from Zurich to Locarno. The transfer station can be any of
the stations but the connecting trains should run on the same day. (You can use a function DAY()
on the attributes Departure and Arrival in order to determine the day)
c) What changes if the relation Connections does not contain a summary tuple for the transitive closure. For example the route Zurich Geneva is represented only by ZurichBern, BernFribourg
and FribourgGeneva.

Relational Algebra - Task 3

Express the outer joins using the basic operations of relational algebra.

SQL - Task 1

Formulate the questions of the first exercise in SQL.

SQL - Task 2

Given the relational schema in the first exercise express the following questions in SQL
a) List all the publishers and their respective books.
b) Which book has the maximum number of pages?
c) Which authors have written more than 5 books?
d) Which book has more pages than twice the average of the number of pages of all books?
e) Which categories do not have any subcategories?
f) Which author has written more books? (*)
g) Which reader has borrowed all the books (by ISBN, not copies) from the author Ephraim Kishon?(*)
h) For which of the books there is at least one copy available?(*)
i) Which are the ten oldest books?(**)
j) Which are all the subcategories of the Sport category (direct or non-direct categories)?(**)
(*) Difficult task, optional, solvable with the lecture knowledge
(**) Difficult task, optional, additional knowledge on SQL is necessary

SQL - Task 3

Formulate in SQL the following modifications to the database of the first exercise:
a) The reader Max Muster borrows the copy with CopyNumber 4 of the book with ISBN 123456.
b) Delete the books that are published after 2013.
c) Change the return date of all the books in the category Databases that should be returned before
15.03.2013 so that they can be kept for 30 days longer (Assume that you can add days to dates in
SQL).

S-ar putea să vă placă și