Sie sind auf Seite 1von 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 T 1 and T 2 .

T 1 : A
B
C
1
2
3
4
5
6
7
8
9
T 2 : B
C
D
2
3
10
2
3
11
2
6
10
6
7
12

a. Calculate the (natural) inner join T 1 ⊲⊳ T 2 of the two tables.

b. Which tuples of T 1 and T 2 are dangling tuples in this calculation?

c. Calculate the (natural) outer join of the two tables. (This is denoted T 1 ⊲⊳ T 2 .)

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 (intersec- tion), ⊲⊳ (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 justiﬁcation 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

b.

S ⊲⊳ T

π N (R

(where N is some set of attribute names from the schema of R and S )

S ) = π N (R ) π N (S )

c. σ C 1 C 2 (R × S ) = σ C 1 (R ) × σ C 2 (S ) where the condition C 1 only mentions attributes of R and C 2 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;

b. SELECT s.lastname FROM staff s WHERE s.sid NOT IN (SELECT l.sid FROM lecturing l);

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 justiﬁcation 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 ).

names. 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

We ﬁrst check the two

tables have the same column

c ol (R ⊲⊳ (S ⊲⊳ T )) = c ol (R ) (c ol (S ) c ol (T )).

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 )).

values in c ol (R ) c ol (S ). Hence r ows ((R ⊲⊳ S ) ⊲⊳ T ) consists of the

 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 )

Since both the sides have the same set of column names and the same set of rows, they are equal as tables.

c ol (S ), c ol (R ) c ol (T ) and c ol (S ) c ol (T ).