Вы находитесь на странице: 1из 2

06-02525 Databases 2 Spring Semester 2011

The University of Birmingham School of Computer Science c Achim Jung & Uday Reddy & Ata Kab an 2011

Exercise Sheet 4
Exercise 1: Outer Joins
Given below are two tables T1 and T2 . T1 : A B 1 2 4 5 7 8 C 3 6 9 T2 : B 2 2 2 6 C 3 3 6 7 D 10 11 10 12

a. Calculate the (natural) inner join T1 T2 of the two tables. b. Which tuples of T1 and T2 are dangling tuples in this calculation? c. Calculate the (natural) outer join of the two tables. (This is denoted T1 T2 .) d. Which tuples of the outer join are included if we are only interested in the left outer join?

Exercise 2: Equations of relational algebra


Consider the following equations concerning the interaction between the relational algebra operators (intersection), (natural join), and (projection). Which of them are always true, and which of them may fail? In the rst case, your answer should consist of a justication for your belief (see overleaf for an example). In the second case, you should indicate an example where the equation fails. Where an equation is true, say which of the two sides would generally evaluate faster in a database management system. a. (R S ) T = R T S T b. N (R S ) = N (R) N (S ) (where N is some set of attribute names from the schema of R and S ) c. C1 C2 (R S ) = C1 (R) C2 (S ) where the condition C1 only mentions attributes of R and C2 only mentions attributes of S . d. R (S T ) = R S R T

Exercise 3: Understanding relational algebra


Explain each of the following relational algebra expressions in plain English, and translate them into SQL. a. name (numbers>100 (lecturing courses)) name (level=1 (courses)) b. {lastname,name} (year=1999 (staff lecturing courses)) {lastname,name} (level=2 (staff lecturing courses))

Exercise 4: From SQL to relational algebra


Translate the following SQL queries into relational algebra. a. SELECT c.name FROM lecturing l1, lecturing l2, courses c WHERE l1.cid=l2.cid AND l1.cid=c.cid AND l1.sid=l2.sid AND l1.year=1999 AND l2.year=2000; SELECT s.lastname FROM staff s WHERE s.sid NOT IN (SELECT l.sid FROM lecturing l);

b.

Exercise 5: Outer joins in SQL


The problem What was the percentage of rst class marks overall in 2003? (from Exercise 4) cannot be solved using normal inner joins unless nested queries are used. Can it be solved using outer joins?

Example justication for algebraic properties


Consider the associativity law (R S ) T = R (S T )). We can justify the fact that it is true as follows. In general, the column names of a join table R S are given by c ol(R S ) = c ol(R) c ol(S ). The rows r ows(R S ) include all and only those combinations of r ows(R) and r ows(S ) which have matching values in c ol(R) c ol(S ). We rst check the two tables have the same column names. c ol((R S ) T ) = (c ol(R) c ol(S )) c ol(T ). c ol(R (S T )) = c ol(R) (c ol(S ) c ol(T )). These two are equal by the associativity of . As mentioned above, the r ows(R S ) are the combinations of r ows(R) and r ows(S ) which have matching values in c ol(R) c ol(S ). Hence r ows((R S ) T ) consists of the combinations of r ows(R S ) and r ows(T ) which have matching values in (c ol(R) c ol(S )) c ol(T ) = (c ol(R) c ol(T )) (c ol(S ) c ol(T )). That means that r ows((R S ) T ) consists of the combinations of r ows(R), r ows(S ) and r ows(T ) which have matching values in c ol(R) c ol(S ), c ol(R) c ol(T ) and c ol(S ) c ol(T ). Doing a similr analysis for the right hand side, we nd that the rows of R (S T ) are the combinations of the rows of R, S and T which have matching values in c ol(R) c ol(S ), c ol(R) c ol(T ) and c ol(S ) c ol(T ). Since both the sides have the same set of column names and the same set of rows, they are equal as tables.

Вам также может понравиться