Presentation is loading. Please wait.

Presentation is loading. Please wait.

INSTALLMENT NUMBER 21 Relational Comparisons

Similar presentations


Presentation on theme: "INSTALLMENT NUMBER 21 Relational Comparisons"— Presentation transcript:

1 INSTALLMENT NUMBER 21 Relational Comparisons
資管所 碩一 林思元 2001,12

2 Relational Comparisons
THE BASIC IDEA NEW CONDITIONAL EXPRESSIONS SOME RESTRICTION EXAMPLES SOME MORE SHORTHANDS ADDITIONAL BENEFITS CONCLUDING REMARKS

3 THE BASIC IDEA What does it mean to assign one object of this type to another? int a = 3 ; int b = 5 ; What does it mean to compare two objects of this type? a  b

4 THE BASIC IDEA What new comparison operators we need?
we need the things like “equals”, “subset of”, “proper subset of”, “superset of”, “proper superset of”

5 THE BASIC IDEA Define a new kind of conditional or truth-valued expression that incorporates those comparison operators (Orthogonality!)Permit those conditional expressions to be used wherever conditional expressions are currently permitted

6 NEW CONDITIONAL EXPRESSIONS
rel-exp-1 rel-θ rel-exp-2 rel-θ is any of the following comparison operators:  (equals)  (not equals)  (subset of)  (proper subset of)  (superset of)  (proper superset of)

7 NEW CONDITIONAL EXPRESSIONS
the negation of “A is a proper subset of B” is certainly not “A is a superset of B” ( “” and “” are not inverses of each other) A = {1,2} B = {3,4}

8 NEW CONDITIONAL EXPRESSIONS
1. S [ CITY ]  P [ CITY ] Is the projection of suppliers(S) over CITY the same as the projection of parts(P) over CITY? 2. S [ S#]  SP [ S# ] Are there any suppliers who don’t supply any parts?

9 SOME RESTRICTION EXAMPLES
Example 1:Find suppliers who supply all parts ( SP [ S#, P# ] DIVIDEBY P [ P# ] ) JOIN S

10 SOME RESTRICTION EXAMPLES
SP P ÷  S JOIN  S# P# 1 A B 2 3 P# A B S# 1 S# …. 1 2 3 S# 1 S# …. 1

11 SOME RESTRICTION EXAMPLES
T : SP RENAME S# AS X ; ANS : S WHERE ( T WHERE X  S# ) [ P# ]  P [ P# ] ; SP T S# P# …. 1 A B 2 3 X P# …. 1 A B 2 3

12 SOME RESTRICTION EXAMPLES
ANS : S WHERE ( T WHERE X  S# ) [ P# ]  P [ P# ] ; S S# …. 1 2 3 P# A B ? = P# A B S# …. 1 只有 1 為 true 1 T 2 X P# …. 1 A B 2 3 3

13 SOME RESTRICTION EXAMPLES
S WHERE ( ( SP RENAME S# AS X ) WHERE X  S# ) [ P# ]  P [ P# ] ( MATCHING rel-exp ) [ cols] S WHERE ( MATCHING SP ) [ P# ]  P [ P# ]

14 SOME RESTRICTION EXAMPLES
S J SP PJ S# …. 1 2 J# …. 1 2 S# P# …. 1 2 J# P# …. 1 2

15 SOME RESTRICTION EXAMPLES
( ( S TIMES J ) WHERE ( MATCHING SP ) [ P# ]  ( MATCHING PJ ) [ P# ] ) [ S#, J# ] a relation with heading {S#,J#} such that supplier S# supplies all parts used in project J#

16 SOME RESTRICTION EXAMPLES
( ( S TIMES J ) WHERE ( MATCHING SP ) [ P# ]  ( MATCHING PJ ) [ P# ] ) [ S#, J# ] S# J# …. 1 2 S  J

17 SOME RESTRICTION EXAMPLES
( ( S TIMES J ) WHERE ( MATCHING SP ) [ P# ]  ( MATCHING PJ ) [ P# ] ) [ S#, J# ] S  J SP PJ S# J# …. 1 2 S# P# …. 1 2 J# P# …. 1 2

18 SOME RESTRICTION EXAMPLES
1 2 S# J# 1 P# 2 true ? P# 1 2 P# 1 2 ? true S# J# 1 2 P# 1 P# 2 ? false P# 1 2 P# 1 ? false

19 SOME RESTRICTION EXAMPLES
( ( S TIMES J ) WHERE ( MATCHING SP ) [ P# ]  ( MATCHING PJ ) [ P# ] ) [ S#, J# ] a relation with heading {J#,S#} such that project J# uses all parts supplied by supplier S#

20 SOME RESTRICTION EXAMPLES
1 2 P# 2 false ? P# 1 2 P# 1 2 ? false P# 1 P# 2 ? false P# 1 2 P# 1 S# J# 2 ? true

21 SOME RESTRICTION EXAMPLES
Example 3:the pairs-of-suppliers problem Given relation SP { S#,P# } showing which suppliers (S#) supply which parts (P#), give (a) an SQL solution, (b) a relational algebra solution, to the problem of finding all pairs of supplier numbers, Sx and Sy say, such that Sx and Sy supply exactly the same set of parts each.

22 SOME RESTRICTION EXAMPLES
Example 3:the pairs-of-suppliers problem ( RX TIMES RY ) WHERE ( SP WHERE S#  SX ) [ P# ]  ( SP WHERE S#  SY ) [ P# ] Appending an additional condition “AND SX  SY” would produce a slightly tidier result; specifically, it would (a) eliminate pairs of the form (Sx, Sx), and (b)ensure that the pairs (Sx,Sy) and (Sy,Sx) do not both appear.

23 SOME MORE SHORTHANDS whether the given relation contain zero rows
IS_EMPTY ( rel-exp ) whether a given row r appear within a given relation R {r}  R r IN R

24 ADDITIONAL BENEFITS We wouldn’t need the quantifiers!
- using relational comparisons EXISTS X ( P )  NOT ( IS_EMPTY ( X WHERE P ) ) FORALL X ( P )  IS_EMPTY ( X WHERE NOT ( P ) )

25 ADDITIONAL BENEFITS We wouldn’t need the quantifiers!
- using aggregate function COUNT EXISTS X ( P )  COUNT ( X WHERE P )  0 FORALL X ( P )  COUNT ( X WHERE P )  COUNT ( X )

26 CONCLUDING REMARKS The idea of relational comparisons is not new.
However, I’m not aware of any implementation of the relational algebra per se that includes support for such comparisons.

27 CONCLUDING REMARKS Puzzle corner problem:
Find suppliers who supply at least all parts supplied by supplier S2. Find supplier numbers for suppliers who supply at least all parts supplied by at least one supplier who supplies at least one London part.

28 CONCLUDING REMARKS Puzzle corner problem:
Find suppliers who supply at least all parts supplied by supplier S2. S WHERE ( MATCHING SP ) [ P# ]  ( SP WHERE S#  ‘S2’) [ P# ]

29 CONCLUDING REMARKS Puzzle corner problem:
Find supplier numbers for suppliers who supply at least all parts supplied by at least one supplier who supplies at least one London part. ( SP WHERE P# IN ( SP WHERE S# IN ( P WHERE CITY  ‘London’) [ P# ] ) [ S# ] ) [ P# ] ) [ S# ]

30 THE END ~


Download ppt "INSTALLMENT NUMBER 21 Relational Comparisons"

Similar presentations


Ads by Google