Query Languages Asst. Prof. Lipyeow Lim InformaDon & Computer Science Department University of Hawaii at Manoa 1 Lipyeow Lim -‐-‐ University of Hawaii at Manoa
– no duplicates allowed • Basic operaDons: – Selec%on (σ) Selects a subset of rows from relaDon. – Projec%on (π) Deletes unwanted columns from relaDon. – Cross-‐product (×) Allows us to combine two relaDons. – Set-‐difference (−) Tuples in reln. 1, but not in reln. 2. – Union (U) Tuples in reln. 1 and in reln. 2. • AddiDonal operaDons: – IntersecDon, join, division, renaming: Not essenDal, but (very!) useful. • Each operaDon returns a relaDon, operaDons can be composed! (Algebra is “closed”.) Lipyeow Lim -‐-‐ University of Hawaii at Manoa 2
bags • Avoid duplicate eliminaDon • Support aggregaDons Lipyeow Lim -‐-‐ University of Hawaii at Manoa 3 A B 1 2 3 4 1 2 1 2 A B 1 2 3 4 Bag Set Can relaDonal algebra work with bags ?
C 1 2 2 3 1 2 2 3 1 2 4 5 1 2 4 5 1 2 4 5 1 2 4 5 Lipyeow Lim -‐-‐ University of Hawaii at Manoa 5 A B 1 2 1 2 B C 2 3 4 5 4 5 R S R × S Cross Product A B C 1 2 3 1 2 3 Join Treat duplicates like non-‐ duplicates
of Hawaii at Manoa 6 A B 1 2 3 4 1 2 1 2 A B 1 2 3 4 3 4 5 6 R S A B 1 2 3 4 1 2 1 2 1 2 3 4 3 4 5 6 R ∪ S A B 1 2 3 4 R ∩ S A B 1 2 1 2 R - S A B 3 4 5 6 S - R Bag Union Bag Intersection Bag Difference Treat duplicates like non-‐ duplicates Tuple t appears min(n,m) Dmes Tuple t appears max(0, n-‐m) Dmes Tuple t appears (n +m) Dmes
a bag into a set • AggregaDon – calculates an aggregate (sum, average etc) over the values in a column • Grouping γ – parDDons tuples in a relaDon into groups based on values in some columns • Extended projecDon π – allow computaDon on column values to produce new values • SorDng τ – sorts a relaDon according to the values in some column(s) • Outer join – preserves dangling pointers in the results of joins Lipyeow Lim -‐-‐ University of Hawaii at Manoa 7
some columns – Apply aggregaDon funcDon to each group – Generate a result tuple per group Lipyeow Lim -‐-‐ University of Hawaii at Manoa 9 Title Year Length Genre StudioName producer Movies For each studio, find the total lengths of movies produced
at Manoa 10 Title Year Length Genre StudioName producer Movies γstudioName, SUM(length) →sumOfLengths Grouping aqributes AggregaDon funcDons on aggregated aqributes with opDonal renaming StudioName SumOfLengths Disney 12345 MGM 54321 ... ...
Manoa 11 πA→X, B+C→Y (R) A B C 0 1 2 0 1 2 3 4 5 X Y 0 3 0 3 3 9 Renaming of aqributes Expressions that compute new values from aqributes and naming the new values
Manoa 12 A B C 1 2 3 4 5 6 7 8 9 B C D 2 3 10 2 3 11 6 7 12 R S A B C D 1 2 3 10 1 2 3 11 A B C D 1 2 3 10 1 2 3 11 4 5 6 ⊥ 7 8 9 ⊥ ⊥ 6 7 12 R S A B C D 1 2 3 10 1 2 3 11 ⊥ 6 7 12 R R S A B C D 1 2 3 10 1 2 3 11 4 5 6 ⊥ 7 8 9 ⊥ R L S Keep right dangling pointers Keep les dangling pointers Discard right & les dangling pointers