Exercises to Chapter 14

advertisement
Exercises to Chapter 14
Ex. 14.6. Show equivalencies
b.
  ( A GF ( E )) A GF (  ( E )) , where 
uses only attributes from A
t    ( A GF ( E ))  tA GF ( E )   (t[ A])   (t[ A])  t[ A ]  F (t [ A ] | t   E  t [ A ]  t[ A ]) 
t[ A ]  F (t [ A ] | t   E  t [ A ]  t[ A ]   (t[ A]))  t[ A ]  F (t [ A ] | t     ( E )  t [ A ]  t[ A ]) 
tA GF (  ( E ))
where we used t[A] to show value of the part of the tuple t with attributes A; A means
set of attributes in tuple which are not in A (i.e., in complement of A). Thus far, we have
shown that Left Hand Side (LHS) set is included into the Right Hand Side (RHS) set.
Let’s show, that RHS  LHS:
tA G F (  ( E ))  t[ A ]  F (t [ A ] | t     ( E )  t [ A ]  t[ A ])  t[ A ]  F (t [ A ] | t   E  t [ A ]  t[ A ]   (t[ A]))
tA G F ( E )   (t[ A])  t    ( A G F ( E ))
Ex. 14.8. For each of the following pairs of expressions, give instances of relations that
show that expressions are not equivalent.
a.  A ( R  S ) and  A ( R)   A ( S )
R and S should have the same schema for applicability of the set difference operation.
Let’s assume that schema has attributes A,B and
R:
A
B
A1
B1
S:
A
B
A1
B2
Then
R-S:
A
B
A1
B1
And  A ( R  S ) :
A
A1
Let’s calculate 2nd set expression:
 A (R) :
A
A1
 A (S ) :
A
A1
And  A ( R)   A ( S ) :
A
(empty relation, no tuples). So, these 2 expressions on given above instances gave
different results, and such expressions are not equivalent
b.  B4 ( A Gmax( B ) ( R )) and A Gmax( B ) ( B4 ( R ))
Let’s consider
R:
A
B
1
2
3
3
A
1
2
1
2
Gmax( B ) ( R ) :
A
B
1
1
2
2
3
2
And  B4 ( A Gmax( B ) ( R )) :
A
B
1
1
2
2
3
2
Let’s consider 2nd expression.
 B4 ( R) :
A
B
1
1
2
2
3
1
3
2
And A Gmax( B ) ( B4 ( R )) :
A
B
1
1
2
2
3
2
This is the same as for the 1st expression. But now let’s consider the following relation
R:
A
B
1
2
3
3
1
2
1
2
3
10
In this case
A G max( B ) ( R ) :
A
B
1
1
2
2
3
10
And  B4 ( A Gmax( B ) ( R )) :
A
B
1
1
2
2
Let’s consider 2nd expression.
 B4 ( R) :
A
B
1
1
2
2
3
1
3
2
And A Gmax( B ) ( B4 ( R )) :
A
B
1
1
2
2
3
2
st
So, 1 result has 2 tuples, and 2nd result has 3 tuples, hence these 2 expressions are not
equivalent (we have constructed counter example for equivalence).
c. In the preceding expressions, if both occurrences of max were replaced by min would
the expressions be equivalent?
 B4 ( A Gmin( B ) ( R )) and A Gmin( B ) ( B4 ( R ))
If to consider the same example as in the previous case, we shall equality, for instance:
R:
A
B
1
2
3
3
3
In this case
A Gmin( B ) ( R ) :
1
2
1
2
10
A
B
1
1
2
2
3
1
And  B4 ( A Gmin( B ) ( R )) :
A
B
1
1
2
2
3
1
Let’s consider 2nd expression.
 B4 ( R) :
A
B
1
2
3
3
And
A
1
2
1
2
A
Gmin( B ) ( B4 ( R)) :
B
1
1
2
2
3
1
Results are the same.
We can prove it considering:
tA Gmin( B ) ( B4 ( R ))  t[ B ]  min( t [ B ] | t    B4 ( R )  t[ A]  t [ A]) 
min( t [ B ] | t   R  t [ B ]  4  t[ A]  t [ A]) 
t[ B ]  4  min( t [ B ] | t   R  t[ A]  t [ A]) 
t   B4 ( A Gmin( B ) ( R ))
And in the opposite direction:
t   B4 ( A Gmin( B ) ( R ))  t[ B ]  4  min( t [ B ] | t   R  t[ A]  t [ A]) 
min( t [ B ] | t   R  t [ B ]  4  t[ A]  t [ A]) 
t[ B ]  min( t [ B ] | t    B4 ( R )  t[ A]  t [ A]) 
tA Gmin( B ) ( B4 ( R ))
In the case of choosing max(B), it may happen that if at first we apply aggregation, then
for some attribute A we may get max(B)>=4 (in our example, for A=3 we had
max(B)=10), and such records will be filtered out by the next selection of records with
B<4. From the other side, if at first to select tuples with B<4, then next aggregation will
use only values less than 4 (we had 2 such tuples for A=3: with B=1,2), and in the result
we shall have record with such value of A(we had tuple with A=3, B=2 in one result and
no tuples with A=3 in the other)
But in the case of aggregation on min, if tuples satisfying B<4 exist, then min value will
also exist together with them, order of operations does not affect on the result.
Download