Download presentation
Presentation is loading. Please wait.
1
STRUCTURE OF PRESENTATION :
1. Basic database concepts ** Entr’acte 2. Relations 8. SQL tables 3. Keys and foreign keys 9. SQL operators I 4. Relational operators I 10. SQL operators II Relational operators II 11. Constraints and predicates 12. The relational model A. SQL constraints SQL vs. the relational model References Copyright C. J. Date 2013
2
SOME ADDITIONAL OPERATORS :
MATCHING and NOT MATCHING EXTEND Image relations Aggregation, summarization, and related matters Copyright C. J. Date 2013
3
1. MATCHING : Many (most?) queries etc. involving join at all really require semijoin … E.g.: Get supplier information for suppliers who supply at least one part: S MATCHING SP /* result on next page */ Copyright C. J. Date 2013
4
S MATCHING SP : SNO SNAME STATUS CITY S1 Smith 20 London S2 Jones 10
Paris S3 Blake 30 S4 Clark Copyright C. J. Date 2013
5
MATCHING (cont.) : Many (most?) queries etc. involving join at all really require semijoin … E.g.: Get supplier information for suppliers who supply at least one part: S MATCHING SP /* result on next page */ Shorthand for ( S JOIN SP ) { SNO , SNAME , STATUS , CITY } rx MATCHING ry ( rx JOIN ry ) { A1, ..., An } where { A1, ..., An } = heading of rx Copyright C. J. Date 2013
6
1 (cont.). NOT MATCHING : Many (most?) queries etc. involving difference at all really require semidifference … E.g.: Get supplier information for suppliers who supply no parts at all: S NOT MATCHING SP /* result on next page */ Copyright C. J. Date 2013
7
S NOT MATCHING SP : SNO SNAME STATUS CITY S5 Adams 30 Athens
Copyright C. J. Date 2013
8
NOT MATCHING (cont.) : Many (most?) queries etc. involving difference at all really require semidifference … E.g.: Get supplier information for suppliers who supply no parts at all: S NOT MATCHING SP /* result on next page */ Shorthand for S MINUS ( S MATCHING SP ) rx NOT MATCHING ry rx MINUS ( rx MATCHING ry ) Copyright C. J. Date 2013
9
BY THE WAY : If the relations denoted by rx and ry are of the same type (i.e., have the same heading), then rx NOT MATCHING ry degenerates to rx MINUS ry E.g., consider S { CITY } NOT MATCHING P { CITY } /* analogous remark NOT true of semijoin */ Copyright C. J. Date 2013
10
2. EXTEND : For each part, get full part information, together with part weight in grams: EXTEND P : { GMWT := WEIGHT * 454 } PNO PNAME COLOR WEIGHT CITY GMWT P1 Nut Red 12.0 London 5448.0 P2 Bolt Green 17.0 Paris 7718.0 P3 Screw Blue Oslo P4 14.0 6356.0 P5 Cam P6 Cog 19.0 8626.0 Copyright C. J. Date 2013
11
ANOTHER EXAMPLE : Get PNO and gram weight for parts with gram weight > 7000.0: ( ( EXTEND P : { GMWT := WEIGHT * 454 } ) WHERE GMWT > ) { PNO , GMWT } Tutorial D syntax (EXTEND): EXTEND rx : { attribute assignment commalist } Copyright C. J. Date 2013
12
FURTHER EXAMPLES : EXTEND S : { TAG := 'Supplier' } SNO SNAME STATUS
CITY TAG S1 Smith 20 London Supplier S2 Jones 10 Paris S3 Blake 30 S4 Clark S5 Adams Athens Copyright C. J. Date 2013
13
EXTEND ( P JOIN SP ) : { SHIPWT := WEIGHT * QTY } .. .. PNAME
.. .. PNAME Nut . WEIGHT 12.0 SNO PNO QTY SHIPWT S1 P1 300 3600.0 etc., etc. Copyright C. J. Date 2013
14
RENAME revisited : RENAME is really just shorthand!
S RENAME { CITY AS SCITY } Equivalent to: ( EXTEND S : { SCITY := CITY } ) { ALL BUT CITY } SNO SNAME STATUS SCITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 S4 Clark S5 Adams Athens RENAME is really just shorthand! Copyright C. J. Date 2013
15
What if part weights were recorded in grams, not pounds?
EXTEND P : { WEIGHT := WEIGHT * 454 } PNO PNAME COLOR WEIGHT CITY P1 Nut Red 5448.0 London P2 Bolt Green 7718.0 Paris P3 Screw Blue Oslo P4 6356.0 P5 Cam P6 Cog 8626.0 Copyright C. J. Date 2013
16
3. IMAGE RELATIONS : Image relation =
“image” in some relation of some tuple (usually a tuple in some other relation) E.g., image in SP of tuple in S for S4: ( SP WHERE SNO = 'S4' ) { ALL BUT SNO } PNO QTY P2 P4 P5 200 300 400 Very useful and widely applicable concept! So we define a shorthand ... Copyright C. J. Date 2013
17
S WHERE ( !!SP ) { PNO } = P { PNO }
image in SP of “current tuple” in S relational equality “Get suppliers who supply all parts” !!! SNO SNAME STATUS CITY S1 Smith 20 London Copyright C. J. Date 2013
18
RECALL : t2 := ( t1 { SNO } ) , t3 := ( SP RENAME { PNO AS x } ) ) :
Get part numbers for parts supplied by all suppliers in Paris WITH ( t1 := ( S WHERE CITY = 'Paris' ) , t2 := ( t1 { SNO } ) , t3 := ( SP RENAME { PNO AS x } ) ) : ( P WHERE ( t3 WHERE x = PNO ) { SNO } ⊇ t2 ) { PNO } Notes: WITH is just a syntactic trick Note use of relational comparison op “⊇” There’s a neater way to formulate this query /* see image relations, later */ Copyright C. J. Date 2013
19
Get part numbers for parts supplied by all suppliers in Paris
( P WHERE ( !!SP ) { SNO } ⊇ ( S WHERE CITY = 'Paris' ) { SNO } ) { PNO } Copyright C. J. Date 2013
20
ANOTHER EXAMPLE : S { SNO } /* suppliers */ SP { SNO , PNO }
/* supplier supplies part PJ { PNO , JNO } /* part is used in project J { JNO } /* projects Get all (sno,jno) pairs such that: SNO sno currently appears in S, and ... JNO jno currently appears in J, and … Supplier sno supplies all parts used in project jno ( S JOIN J ) WHERE !!PJ !!SP Pretty straightforward … Copyright C. J. Date 2013
21
IMAGE RELATIONS : “Very useful and widely applicable concept!”
We’ll see more examples quite soon … Copyright C. J. Date 2013
22
4. AGGREGATE OPERATORS : An aggregate operator is not, in general, a relational operator (because the result usually isn’t a relation) … It’s an operator that derives a single value from the “aggregate” (i.e., the set or bag) of values of some attribute of some relation—or, for COUNT, from the entire relation. E.g.: X := COUNT ( S ) ; /* X = 5 */ Y := COUNT ( S { STATUS } ) ; /* Y = 3 */ Z := SUM ( SP { QTY } ) ; /* Z = 1000 */ Tutorial D syntax (aggregate operator invocation): agg op name ( rel exp [ , exp ] ) Copyright C. J. Date 2013
23
Tutorial D EXAMPLES : SUM ( SP { QTY } ) /* 1000 */ ( SP , QTY )
/* 3100 */ AVG ( SP , 3 * QTY ) /* 775 */ Legal agg op names include: COUNT SUM AVG MAX MIN AND OR XOR The exp can include attribute refs (in practice, almost always does) The exp must be omitted for COUNT ... Otherwise, it can be omitted only if the relation denoted by rel exp is of degree one, as in the first example above Copyright C. J. Date 2013
24
Aggregate operators are useful:
In summarizations (i.e., aggregating something for each tuple in some projection of some relation, loosely speaking) In WHERE clauses (“generalized restriction”) Note: In both cases, image relations are almost always involved too Copyright C. J. Date 2013
25
SUMMARIZATION : For each supplier, get SNO and number of parts supplied: EXTEND S { SNO } : { PCT := COUNT ( !!SP ) } /* note the image relation reference */ SNO PCT S1 6 S2 2 S3 1 S4 3 S5 count of an empty set ... Copyright C. J. Date 2013
26
SUMMARIZATION (cont.) :
For each supplier, get SNO and total shipment quantity: EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } /* note the image relation reference */ SNO TOTQ S1 1300 S2 700 S3 200 S4 900 S5 sum of an empty set of integers ... Copyright C. J. Date 2013
27
SUMMARIZATION (cont.) :
For each Paris supplier, get supplier details and total, maximum, and minimum shipment quantity: EXTEND ( S WHERE CITY = 'Paris' ) : { TOTQ := SUM ( !!SP , QTY ) , MAXQ MAX ( !!SP , QTY ) , MINQ MIN ( !!SP , QTY ) } /* note use of “multiple EXTEND” */ Copyright C. J. Date 2013
28
ASIDE : Tutorial D currently supports explicit SUMMARIZE as well:
EXTEND S { SNO } : { PCT := COUNT ( !!SP ) } SUMMARIZE S BY { SNO } : { PCT := COUNT ( ) } EXTEND S { SNO } : { TOTQ := COUNT ( !!SP ) } SUMMARIZE S BY { SNO } : { TOTQ := SUM ( QTY ) } But “summaries” (COUNT(), SUM(QTY)), etc. are somewhat suspect ... Copyright C. J. Date 2013
29
“GENERALIZED RESTRICTION” :
Get suppliers with fewer than three shipments: S WHERE COUNT ( !!SP ) < 3 /* result = S2, S3, S5 */ Copyright C. J. Date 2013
30
Get suppliers for whom the total shipment quantity is less than 1000:
S WHERE SUM ( !!SP , QTY ) < 1000 /* result = S2, S3, S4, S5 */ Copyright C. J. Date 2013
31
( EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } )
Get SNO and total shipment quantity for suppliers where total quantity > 250: ( EXTEND S { SNO } : { TOTQ := SUM ( !!SP , QTY ) } ) WHERE TOTQ > 250 Copyright C. J. Date 2013
32
ONE FINAL POINT : Result if argument empty for: COUNT 0
SUM AVG MAX MIN AND OR XOR 0 /* of appropriate type */ undefined undefined undefined TRUE FALSE FALSE Copyright C. J. Date 2013
33
SOME ADDITIONAL OPERATORS :
MATCHING and NOT MATCHING EXTEND Image relations Aggregation, summarization, and related matters Copyright C. J. Date 2013
34
EXERCISES : What do the following expressions denote? P MATCHING S
S NOT MATCHING ( SP WHERE PNO = 'P2' ) EXTEND P : { SCT := COUNT (!!SP) } P WHERE SUM ( !!SP , QTY ) < 500 Copyright C. J. Date 2013
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.