Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.

Similar presentations


Presentation on theme: "Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with."— Presentation transcript:

1 Relational Algebra II

2 Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with the same heading as A –With body consisting of all tuples t of A s.t. condition X  Y is true in the result The following expressions are equivalent A WHERE c1 AND c2  (A WHERE c1) INTERSECT (A WHERE c2) A WHERE c1 OR c2  (A WHERE c1) UNION (A WHERE c2) A WHERE NOT c  A MINUS (A WHERE c)

3 Semantics – where(cont.) Ex) S WHERE CITY = ‘ Paris ’ (original) (result) Ex) SP WHERE S# = S#( ‘ S7 ’ ) OR P# = P#( ‘ P7 ’ ) (result) S#SNAMESTATUSCITY S2Jones10Paris S3Brakes30Pairs S#P#QTY

4 Semantics - project A{X, …, Z} –Projection of relation with respect to attributes, X, …, Z –Duplicate tuples are eliminated Ex) S {CITY} Ex) {S WHERE CITY = ‘ London ’ }{S#} CITY London Paris Athenes S# S1 S4

5 Semantics - join In general, Join means natural join –All possible tuples that are combination of two relations such that two tuples contributing to any given combination have a common value for the common attributes Natural join needs common attributes between 2 relations Natural join is not primitive operation –A projection of a restriction of a product Ex) S JOIN P  ((S TIMES (P RENAME CITY AS PCITY)) WHERE CITY = PCITY) {ALL BUT PCITY}

6 Semantics -  -join Join on the basis of some comparison operator other than ‘ = ‘ –(A TIMES B) WHERE X  Y Ex) ((S RENAME CITY AS SCITY) TIMES (P RENAME CITY AS PCITY)) WHERE SCITY > PCITY S#SNAMESTATUSSCITYP#PNAM E COLO R WEIGHTPCITY S2Jones10ParisP1NutRed12.0London S2Jones10ParisP4ScrewRed14.0London S2Jones10ParisP6CogRed19.0London S3Blakes30ParisP1NutRed12.0London S3Blakes30ParisP4ScrewRed14.0London S3Blekes30ParisP6CogRed19.0London

7 Semantics - equijoin When  is =, the  -join is equijoin The result includes two attributes in which the values in the attributes are equal If one of the two attributes is eliminated, it is the natural join

8 Semantics – outer join Keep all tuples in one of the relations –Left outer join Keep all tuples in the left of the operator –Right outer join Keep all tuples in the right of the operator Ex) S LEFT OUTER JOIN P S P S#CITY S1 S2 S3 London Paris Athenes P#CITY P1 P2 London Paris S#P#CITY S1 S2 S3 P1 P2 null London Paris Athenes

9 Semantics - divide A DIVIDEBY B PER C –A relation with heading of A Body consisting of A ’ s tuples such that –They also exist in C as part –The part except A ’ s also is in B Ex) A B C S# S1 S2 S3 S4 S5 S#P# S1 S2 P1 P2 P3 P1 S# S1 P# P1 P2 P# P1 S# S1 S2

10 Semantics Associativity and Commutativity –UNION, INTERSECT, TIMES, JOIN, but not MINUS Ex) (A UNION B) UNION C  A UNION (B UNION C) A UNION B  B UNION A

11 Examples Get supplier names for suppliers who supply part P2 –Supplier name  S –Who supply what  SP –So, Join operation is needed –((SP JOIN S) WHERE P# = P#( ‘ P2 ’ )){SNAME}

12 Examples (cont.) Get supplier names for suppliers who supply at least one red part –Red part  P –P WHERE COLOR=COLOR( ‘ Red ’ ) –Additional condition: ‘ supply ’ and ‘ supplier name ’  join S and SP operation to the above result is needed (((P WHERE COLOR=COLOR( ‘ Red ’ )){P#} JOIN SP) JOIN S){SNAME}

13 Example (cont.2) Get supplier names for suppliers who supply all parts –Part supply  SP –All parts  P –Suppliers  S So, S {S#} DIVIDEDBY P {P#} PER SP {S#, P#}  supplier number So, join it with S –(( S {S#} DIVIDEDBY P {P#} PER SP {S#, P#}) JOIN S ) {SNAME}

14 Example Get supplier names for suppliers who supply all parts: S {S#} DIVIDEDBY P {P#} PER SP {S#, P#} Ex) S{S#} P{P#} SP{S#,P#} S# S1 S2 S3 S4 S5 S#P# S1 … P1 P2 P3 P4 P5 P6 … S# S1 P# P1 P2 P3 P4 P5 P6


Download ppt "Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with."

Similar presentations


Ads by Google