Download presentation
Presentation is loading. Please wait.
Published byDrusilla Horn Modified over 9 years ago
1
CS 338Relational Algebra4-1 Relational Algebra Lecture Topics Basic concepts and operations of relational algebra Reference, selection, projection Cross-product and union Textbook Chapter 4
2
CS 338Relational Algebra4-2 Relational Algebra Proposed by E.F. Codd (1972) as basic means of manipulating data in a relational database A procedural query language, with fundamental operations: –reference –selection –projection –cross product –set union –set difference –renaming Algebra: Set of operators mapping existing relations to new relations
3
CS 338Relational Algebra4-3 Reference: referring to an existing relation Notation: Id Value: the relation named by Id E.g. “The Vendors” Vendor Result: (a duplication of the Vendor table) Reference VnoVnameCityVbal 12341234 Sears Kmart Esso Toronto Ottawa Montreal Waterloo 200.00 671.05 0.00 2.25
4
CS 338Relational Algebra4-4 Selection Selection: choosing some rows from a relation Notation: C (R) where R is a relation and C is a condition on individual rows of R Value: those rows of R for which condition C is true E.g. “The vendors in Waterloo.” City = 'Waterloo' (Vendor) Result: VnoVnameCityVbal 4EssoWaterloo2.25
5
CS 338Relational Algebra4-5 E.g. “Vendors that are in Toronto or have a balance exceeding 100.” (City = 'Toronto' OR Vbal > 100) (Vendor) Result:...continued VnoVnameCityVbal 1212 Sears Kmart Toronto Ottawa 200.00 671.05 Selection condition may: test for equality (=) between attributes and values, and invoke function calls on underlying domains To ease writing queries, may: build more complicated conditions using logical connectives AND, OR and NOT
6
CS 338Relational Algebra4-6 Projection Projection: drop attributes from the result Notation: A1,..., An (R) where A i are attributes in the relational schema of R Value: R restricted to attributes in A i E.g. “Vendor names.” Vname (Vendor) Result: Vname Sears Kmart Esso Because relations are sets, any resulting duplicate rows are removed
7
CS 338Relational Algebra4-7 Cross product Cross-product: pairing all possible combinations of tuples from two relations Notation: R 1 R 2 where R 1 and R 2 are relations with disjoint relational schema Value: every tuple in R 1 unioned ("matched") with every tuple in R 2 May generate a very large relation: Number of tuples in result = (number of tuples in R 1 ) X (number of tuples in R 2 ) Number of values in result tuple = (number of values in an R 1 tuple) + (number of values in an R 2 tuple)
8
CS 338Relational Algebra4-8 E.g. R1R1 AB 1212 xyxy R2R2 CD abcabc stustu R 1 R 2 ABCD 111222111222 xxxyyyxxxyyy abcabcabcabc stustustustu...continued
9
CS 338Relational Algebra4-9 Set union Union: merging two relations Notation: R 1 R 2 where R 1 and R 2 are relations with equivalent relational schema Value: all tuples in R 1 or in R 2 (or in both) E.g. “Vendors that are in Toronto or have a balance exceeding 100.” City = 'Toronto' (Vendor) Vbal > 100 (Vendor) Result: VnoVnameCityVbal 1212 Sears Kmart Toronto Ottawa 200.00 671.05
10
CS 338Relational Algebra4-10 Set difference Difference: excluding tuples of one relation Notation: R 1 R 2 where R 1 and R 2 are relations with equivalent relational schema Value: Tuples in R 1 that are not in R 2. E.g. “Vendor numbers for vendors with no transactions.” Vno (Vendor) Vno (Transactions) Result: Vno 1
11
CS 338Relational Algebra4-11 Another e.g. “Vendor names for vendors with no transactions.” Vname (Vendor) Vname (???) Result: Vname Sears...continued Discussion: Why is the previous example easy, but the above more complex? Vno attribute is common to both relations, but Vname is not Use a cross-product
12
CS 338Relational Algebra4-12 Use a cross-product to form a table with the desired attributes added to the rows How many rows in the C.P.? Too many! Use a selection to choose the right rows Attribute name problem: need to rename one of the VNo attributes temporarily E.g. “Vendor names for vendors with no transactions.” Vname (Vendor) Vname ( Vno = V.Vno ( Vno as V.Vno (Vendor) Transaction)) Result: Cross-product and select Vname Sears
13
CS 338Relational Algebra4-13 Step-by-step Vname (Vendor) Vname Sears Kmart Esso Vno as V.Vno (Vendor) 12341234 Sears Kmart Esso Toronto Ottawa Montreal Waterloo 200.00 671.05 0.00 2.25 V.Vno Vname City VBal Transaction TnoVnoAccNumTdateAmount 1001 1002 1003 1004 1005 2234422344 101 103 101 102 103 960115 960116 960115 960120 960125 13.25 19.00 25.00 16.13 33.12
14
CS 338Relational Algebra4-14...continued Vno as V.Vno (Vendor) Transaction
15
CS 338Relational Algebra4-15... continued Vno = V.Vno (...etc... ) Vname (...etc... ) Vname Kmart Esso Vname (Vendor) (...etc... ) Vname Sears a cross-product followed by a select is called a join (also equijoin, nautral join)
16
CS 338Relational Algebra4-16 Renaming: temporarily changing names of attributes Notation: A1 as B1,..., An as Bn (R) where R is a relation and A n are attributes of R Value: same as R, with attribute A i replaced by attribute B i (attribute name A i replace by B i ) Attribute renaming
17
CS 338Relational Algebra4-17 Additional operators Do not increase expressive power, but make life easier: set intersection: join (cross-product & select): division: assignment: Assignment Notation: NewR R Value: creates a relation named NewR identical to R E.g. “Vendor names for vendors with no transactions.” T1 Vno as V.Vno (Vendor) T2 T1 Transaction T3 Vname ( Vno=V.Vno (T2)) T4 Vname (Vendor) T3
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.