INSTALL NUMBER 12 Answers to Puzzle Corner Problems

advertisement
INSTALL NUMBER 12
Answers to Puzzle Corner Problems
(Installment Number 7-11)
中正大學資管所
碩一 690530023
郭 溥 淵
Introduction
 Reading review(Installment 7-11)
 The DEE and DUM problem
 The empty argument problem
 The general unification theorem
 Expression transformation problem
 Technical correspondence
 Conclusion
 Q&A
Reading review(Installment 7-11)
 Installment 7: Table with no columns
 Installment 8: Empty bag and identity crises
 Installment 9: The power of the keys
 Installment 10: Expression transformation
 Installment 11: Expression transformation
The DEE and DUM problem
 Source:”Tables with No Columns”
(Installment 7)
 Problem statement: What are the effects of
DEE and DUM on the relational algebra
operation union,intersection, difference,
restrict, project, division, extend, and
summarize?
The DEE and DUM problem
 Solution:
Union DEE
DUM
OR
1
0
DEE
DEE
DEE
1
1
1
DUM
DEE
DUM
0
1
0
The DEE and DUM problem
 Solution:
Intersection
DEE
DUM
AND 1
0
DEE
DEE
DUM
1
1
0
DUM
DUM DUM
0
0
0
The DEE and DUM problem
 Solution:
Difference DEE
DUM
AND
NOT
1
0
DEE
DUM
DEE
1
0
1
DUM
DUM
DUM
0
0
0
The DEE and DUM problem
 Solution of restrict and project:
•
•
•
Any restriction of DEE yields DEE if the
restriction condition is true, DUM if it is false.
Any restriction of DUM yields DUM.
Projection of any_table over no columns yields
DUM if the original table is empty, DEE otherwise.
In particular, projection of DEE or DUM,
necessarily over no columns at all, return its input.
The DEE and DUM problem
 Solution of division:
•
•
•
•
•
Any table T divided by DEE yields T.
Any table T divided by DUM yields an empty
table with the same heading as T.
DEE divided by any table T yields T.
DUM divided by any tale T yields an empty table
with the same heading as T.
Any nonempty table divided by itself yields DEE.
An empty table divided by itself yields DUM.
The DEE and DUM problem
 Solution of extend and summarize:
•
•
Extending DEE or DUM to add a new column
yields a relation of one column and the same
number of rows as its input.
Summarizing DEE or DUM(necessarily over no
columns at all) yields a relation of one column and
the same number of rows as input.
The empty argument problem
 Source:”Empty Bags and Identity
crises” (Installment 8)
 Problem statement: Give the correct “empty
argument” treatment for each of the
following functions. Note: In Case 1-4 the
argument is intended to be a bag of number;
in Case 5-6 it is a bag of tables all having
the same(specified) heading.
The empty argument problem
1. Sum of the squares
Solution:
0
2.Standard deviation
Solution:
Undefined
The empty argument problem
3. Median
Solution:
Undefined
4.Geometric mean
Solution:
1
The empty argument problem
 Source:”Empty Bags and Identity
crises” (Installment 8)
 Problem statement: Give the correct “empty
argument” treatment for each of the
following functions. Note: In Case 1-4 the
argument is intended to be a bag of number;
in Case 5-6 it is a bag of tables all having
the same(specified) heading.
The empty argument problem
5. Union
Solution:
An empty table with the specified
heading.
6.Intersection
Solution:
A table with the specified heading and
with body equal to the Cartesian
product to all underlying domains.
The empty argument problem
 SELECT CURRENT_TIME
WHERE 1=0 ;
 EXTEND DUM ADD CURRENT_TIME
AS X
 Those two query are equivalent.
The general unification theorem
 Source:”The power of keys”
(Installment 9)
 Problem statement: Darwen’s work on FD
and key inheritance makes use of the
following theorem. Let A, B, C, and D be
subsets of the set of columns of relation R
such that AB and CD. Then A∪(CB)B∪D . Prove this theorem.
The general unification theorem
 Solution:
• Self-determination: AA
• Joint dependence: AB&AC≡AB∪C
• Transitivity: AB&BC ==> AC
• Composition: AB&CD ==>A∪CB∪D
The general unification theorem
 Solution:
1. AB (given)
2.CD (given)
3.AB∩C (by joint dependence and 1)
4.C-BC-B (self-determination)
5.A∪(C-B)(B∩C)∪(C-B)
(by composition, 3, 4)
6.A∪(C-B)C (simplifying 5)
7.A∪(C-B)D (by transitivity, 6, 2)
8. A∪(C-B)B∪D (by composition, 1, 7)
The general unification theorem
 Solution:
1. AB (given)
2.CD (given)
3.AB∩C (by joint dependence and 1)
why?
因為AB,又B∩C屬於B的一部份,
故AB∩C。
The general unification theorem
 Solution:
4.C-BC-B (self-determination)
5.A∪(C-B)(B∩C)∪(C-B)
(by composition, 3, 4)
why?
Composition: AB&CD ==>A∪CB∪D
AB∩C ---3
C-BC-B ---4
故A∪(C-B)(B∩C)∪(C-B)
The general unification theorem
 Solution:
6.A∪(C-B)C (simplifying 5)
why?
(B∩C)∪(C-B)就是C
所以A∪(C-B)C
7.A∪(C-B)D (by transitivity, 6, 2)
why?
因為A∪(C-B)C ,又CD
所以A∪(C-B)D
The general unification theorem
 Solution:
8. A∪(C-B)B∪D (by composition,1,7)
why?
Composition: AB&CD ==>A∪CB∪D
AB
---1
A∪(C-B)D ---2
所以A∪(C-B)B∪D
 完成證明
Expression transformation problem
 Source:”Expression Transformation”
(Installment 10 & 11)
 Problem statement:Prove the following
statements
• A sequence of restrictions against a given relation
can be transformed into a single restriction.
• A sequence of projections against a given relation
can be transformed into a single projection.
• A restriction of a projection can be transformed into
a restriction.
Expression transformation problem
 Solution:
(a)
• ( R WHERE C1 ) WHERE C2
• R WHERE C1 AND C2
If C1 and C2 are both restriction condition for
relation R, then above two expressions are obviously
equivalent.
Expression transformation problem
 Solution:
(b)
• ( R [ L1 ] ) [L2]
• R [ L2 ]
If L1 is a subset of the heading of relation R and L2
is a subset of L1,then the above two expressions are
obviously equivalent.
Expression transformation problem
 Solution:
(c)
• R [ L ] WHERE C
• ( R WHERE C ) [ L ]
If L and C are, respectively, a subset of heading of
relation R and restriction condition for relation R
that involves only columns mentioned in L, then the
above two expressions are obviously equivalent.
Expression transformation problem
 SELECT E#
FROM DEPT, EMP
WHERE NOT ( DEPT.D# = EMP.D#
AND EMP.D# = ‘D1’) ;
 Show the answer
DEPT D#
EMP E# D#
D2
E1
--
Expression transformation problem
(a) Show the correct real-world answer to this
query.
DEPT D#
EMP E#
D2
E1
D#
--
Expression transformation problem
 SELECT E#
FROM DEPT, EMP
WHERE NOT ( DEPT.D# = EMP.D#
AND EMP.D# = ‘D1’) ;
 Show the answer
DEPT
EMP
D#
E#
D#
D2
E1
--
The answer is E1.
Expression transformation problem
(b) Show the answer delivered by the query as
stated.
DEPT D#
EMP E#
D2
E1
D#
--
Expression transformation problem
 SELECT E#
FROM DEPT, EMP
WHERE NOT ( DEPT.D# = EMP.D#
AND EMP.D# = ‘D1’) ;
 Show the answer
DEPT
EMP
D#
E#
D#
D2
E1
--
The answer is empty.
Expression transformation problem
 SELECT E#
FROM DEPT, EMP
WHERE NOT ( DEPT.D# = EMP.D#
AND EMP.D# = ‘D1’) ;
 Show the answer
DEPT D#
EMP E# D#
D2
E1
--
Expression transformation problem
(c) Show the answer delivered if the optimizer
applies “predicate transitive closure”.
DEPT D#
EMP E#
D2
E1
D#
--
Expression transformation problem
Optimizer will transform the expression to :
SELECT E#
FROM DEPT,EMP
WHERE NOT ( DEPT.D# = EMP.D#
AND EMP.D# = D1
AND DEPT.D# = D1 ) ;
The answer is E1.
Technical correspondence
 Problem:
• SUMMARIZE EMP BY ( D# )
ADD AVG ( SAL )
AS AVGSAL
Technical correspondence
 Solution:
• IF exp1 THEN exp2 ELSE exp3
• SUMMARIZE EMP BY ( D# )
ADD ( IF IS_EMPTY ( EMP ) THEN 0
ELSE AVG ( SAL ) )
AS AVGSAL
Conclusion
Q&A
Thanks for your listening.
Download