Chapter 7 Relational Algebra Prof YinFu Huang CSIE
Chapter 7 Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST
7. 1 Introduction n Eight operators of the relational algebra: 1. The traditional set operators union, intersection, difference, and Cartesian product. 2. The special relational operators restrict, project, join, and divide. (See Fig. 7. 1) Advanced Database System 2
Advanced Database System 3
7. 2 Closure Revisited n The output from any given relational operation is another relation. n Nested relational expressions Advanced Database System 4
7. 3 The Original Algebra: Syntax n (See Page 178 -179) <relation exp> : : = Relation {<tuple exp commalist>} | <relvar name> | <relation op inv> | <with exp> | <introduced name> | (<relation exp>) <relation op inv> : : = <project> | <nonproject> <project> : : = <relation exp> { [All But] <attribute name commalist> } <nonproject> : : = <rename> | <union> | <intersect> | <minus> | <times> | <where> | <join> | <divide> Advanced Database System 5
7. 3 The Original Algebra: Syntax (Cont. ) <rename> : : = <relation exp> Rename ( <renaming commalist> ) <union> : : = <relation exp> Union <relation exp> <intersect> : : = <relation exp> Intersect <relation exp> <minus> : : = <relation exp> Minus <relation exp> <times> : : = <relation exp> Times <relation exp> <where> : : = <relation exp> Where <bool exp> <join> : : = <relation exp> Join <relation exp> <divide> : : = <relation exp> Divideby <relation exp> Per <per> : : = <relation exp> | ( <relation exp>, <relation exp> ) <with exp> : : = With <name intro commalist> : <exp> <name intro> : : = <exp> As <introduced name> Advanced Database System 6
7. 4 The Original Algebra: Semantics n Tuple-homogeneous n Union, Intersect, and Difference (See Fig. 7. 2) n Product (See Fig. 7. 3) If we need to construct the Cartesian product of two relations that do have any such common attribute names, we must use the Rename operator first to rename attributes appropriately. n Restrict (See Fig. 7. 4) n Project (See Fig. 7. 5) Advanced Database System 7
Fig. 7. 2 Union, intersection, and difference Advanced Database System 8
Fig. 7. 3 Cartesian product example Advanced Database System 9
Fig. 7. 4 Restriction examples Advanced Database System 10
Fig. 7. 5 Projection examples Advanced Database System 11
7. 4 The Original Algebra: Semantics (Cont. ) n Join • natural join (See Fig. 7. 6) • θ-join (See Fig. 7. 7) ((S Rename City As Scity) Times (P Rename City As Pcity)) Where Scity > Pcity n Divide (See Fig. 7. 8) Advanced Database System 12
Fig. 7. 6 & Fig. 7. 7 Advanced Database System 13
Fig. 7. 8 Division examples Advanced Database System 14
7. 5 Examples n Exam 1: ((Sp Join S) Where P#=P#(‘P 2’)) {Sname} n Exam 2: (((P Where Color=Color(‘Red’)) Join Sp ) {S#} Join S) {Sname} n Exam 3: ((S {S#} Divideby P {P#} Per Sp {S#, P#}) Join S) {Sname} n Exam 4: S {S#} Divideby (Sp Where S#=S#(‘S 2’)) {P#} Per Sp {S#, P#} n Exam 5: (((S Rename S# As Sa) {Sa, City} Join (S Rename S# As Sb) {Sb, City}) Where Sa < Sb) {Sa, Sb} n Exam 6: ((S {S#} Minus (Sp Where P#=P#(‘P 2’)) {S#}) Join S) {Sname} Advanced Database System 15
7. 6 What Is the Algebra For? n The operators join, intersect, and divide can be defined in terms of the other five. n Of the remaining five, however, none can be defined in terms of the other four, so we can regard those five as constituting a primitive or minimum set. n Some possible applications: 1. Defining a scope for retrieval 2. Defining a scope for update 3. Defining integrity constraints 4. Defining derived relvars 5. Defining stability requirements 6. Defining security constraints Advanced Database System 16
7. 6 What Is the Algebra For? (Cont. ) n A high-level, symbolic representation of the user‘s intent Ø Transformation rules ((Sp Join S) Where P#=P#(‘P 2’)) {Sname} ((Sp Where P#=P#(‘P 2’)) Join S) {Sname} n The algebra thus serves as a convenient basis for optimization. n A language is said to be relationally complete if it is at least as powerful as the algebra. Advanced Database System 17
7. 7 Further Points n Associativity and Commutativity • Associative: Union, Intersect, Times, Join e. g. (A Union B) Union C = A Union (B Union C) = A Union B Union C • Commutative: Union, Intersect, Times, Join e. g. A Union B = B Union A n Some Equivalences e. g. r { } = Table_Dum if r=empty, Table_Dee otherwise (a nullary projection) r Join Table_Dee = Table_Dee Join r = r r Times Table_Dee = Table_Dee Times r = r Advanced Database System 18
7. 7 Further Points (Cont. ) n Some Generalizations If s contains no relations at all, then: l. The join of all relations in s is defined to be Table_Dee. l. The union of all relations in s is defined to be the empty relation. l. The intersection of all relations in s is defined to be the “universal” relation. Advanced Database System 19
7. 8 Additional Operators n (See Page 196) <semijoin> : : = <relation exp> Semijoin <relation exp> <semiminus> : : = <relation exp> Semiminus <relation exp> <extend> : : = Extend <relation exp> Add ( <extend add commalist> ) <extend add> : : = <exp> As <attribute name> <summarize> : : = Summarize <relation exp> Per <relation exp> Add ( <summarize add commalist> ) <summarize add> : : = <summary type> [ ( <scalar type> ) ] As <attribute name> <summary type> : : = Count | Sum | Avg | Max | Min | All | Any | Countd | Sumd | Avgd| … <tclose> : : = Tclose <relation exp> Advanced Database System 20
7. 8 Additional Operators (Cont. ) n Semijoin (a Join b) {X, Y} e. g. S Semijoin (Sp Where P#=P#(‘P 2’)) n Semidifference a Minus (a Semijoin b) e. g. S Semiminus (Sp Where P#=P#(‘P 2’)) n Extend e. g. Extend P Add (Weight*454) As Gmwt (See Fig. 7. 9) Advanced Database System 21
7. 8 Additional Operators (Cont. ) Ø Exam 1: Extend S Add ‘Supplier’ As Tag Ø Exam 2: Extend (P Join Sp) Add (Weight*Qty) As Shipwt Ø Exam 3: (Extend S Add City As Scity) {All But City} Rename Ø Exam 4: Extend P Add (Weight*454 As Gmwt, Weight*16 As Ozwt) Ø Exam 5: Extend S Add Count((Sp Rename S# As X) Where X=S#) As Np (See Fig. 7. 10) Advanced Database System 22
7. 8 Additional Operators (Cont. ) n Summarize e. g. Summarize Sp Per P {P#} Add Sum(Qty) As Totqty (See Fig. 7. 11) Ø Exam 1: Summarize Sp Per P {P#} Add (Sum(Qty) As Totqty, Avg(Qty) As Avgqty) Ø Exam 2: Summarize Sp Per S {S#} Add Count As Np • Summarize is not a primitive operator. Extend Ø Exam 3: Summarize S Per S {City}Add Avg(Status) As Avg_Status Ø Exam 4: Summarize Sp Per Sp { }Add Sum(Qty) As Grandtotal n Tclose the transitive closure of a Advanced Database System 23
7. 9 Grouping and Ungrouping n Group e. g. SP Group (P#, Qty) As PQ (See Fig. 7. 12) n Ungroup e. g. SPQ Ungroup PQ n The reversibility of the Group and Ungroup operations (See Fig. 7. 13) Ø Functionally dependency Advanced Database System 24
The End. Advanced Database System 25
- Slides: 25