Unformatted text preview:

1By relieving the brain of all unnecessarywork, a good notation sets it free toconcentrate on more advanced problems,and, in effect, increases the mental power ofthe race.-- Alfred North Whitehead (1861 - 1947)Relational AlgebraR & G, Chapter 4πRelational Query Languages•Query languages: Allow manipulation and retrievalof data from a database.• Relational model supports simple, powerful QLs:– Strong formal foundation based on logic.– Allows for much optimization.• Query Languages != programming languages!– QLs not expected to be “Turing complete”.– QLs not intended to be used for complex calculations.– QLs support easy, efficient access to large data sets.Formal Relational Query LanguagesTwo mathematical Query Languages form thebasis for “real” languages (e.g. SQL), and forimplementation:Relational Algebra: More operational, veryuseful for representing execution plans.Relational Calculus: Lets users describe whatthey want, rather than how to compute it.(Non-procedural, declarative.) Understanding Algebra & Calculus is key to understanding SQL, query processing! Preliminaries• A query is applied to relation instances, and theresult of a query is also a relation instance.–Schemas of input relations for a query are fixed (butquery will run over any legal instance)– The schema for the result of a given query is alsofixed. It is determined by the definitions of thequery language constructs.• Positional vs. named-field notation:– Positional notation easier for formal definitions,named-field notation more readable.– Both used in SQL• Though positional notation is not encouragedRelational Algebra: 5 Basic Operations•Selection ( σ ) Selects a subset of rows from relation(horizontal).•Projection ( π ) Retains only wanted columns fromrelation (vertical).•Cross-product ( × ) Allows us to combine two relations.•Set-difference ( — ) Tuples in r1, but not in r2.•Union ( ∪ ) Tuples in r1 or in r2.Since each operation returns a relation, operations can becomposed! (Algebra is “closed”.)sidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0sidbidday2210110/10/965810311/12/96R1S1S2bidbnamecolor101Interlakeblue102Interlakered103Clippergreen104MarineredBoatsExample Instances2Projection (π)!ageS( )2• Examples: ;• Retains only attributes that are in the “projection list”.•Schema of result:– exactly the fields in the projection list, with the same names that theyhad in the input relation.• Projection operator has to eliminate duplicates (How dothey arise? Why remove them?)– Note: real systems typically don’t do duplicate elimination unless theuser explicitly asks for it. (Why not?)!sname ratingS,( )2Projection (π)age35.055.5sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S2sname ratingyuppy 9lubber 8guppy 5rusty 10)2(,Sratingsname!!ageS( )2Selection (σ)!ratingS>82( )sname ratingyuppy 9rusty 10! "sname ratingratingS,( ( ))>82• Selects rows that satisfy selection condition.• Result is a relation.Schema of result is same as that of the input relation.• Do we need to do duplicate elimination?sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 Union and Set-Difference• Both of these operations take two input relations, which must beunion-compatible:– Same number of fields.– `Corresponding’ fields have the same type.• For which, if any, is duplicate elimination required?Unionsidsnameratingage22dustin745.031lubber855.558rusty1035.044guppy535.028yuppy935.0sidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S1S2S S1 2!Set Differencesidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S1S2sidsnameratingage22dustin745.0S2 – S1sidsnameratingage28yuppy935.044guppy535.0S S1 2!3Cross-Product• S1 × R1: Each row of S1 paired with each row of R1.• Q: How many rows in the result?•Result schema has one field per field of S1 and R1,with field names `inherited’ if possible.–May have a naming conflict: Both S1 and R1 have a fieldwith the same name.– In this case, can use the renaming operator:!( ( , ), )C sid sid S R1 1 5 2 1 1" " #Cross Product Example(sid)snameratingage(sid)bidday22dustin745.02210110/10/9622dustin745.05810311/12/9631lubber855.52210110/10/9631lubber855.55810311/12/9658rusty1035.02210110/10/9658rusty1035.05810311/12/96sidsnameratingage22dustin745.031lubber855.558rusty1035.0sidbidday2210110/10/965810311/12/96R1S1R1 X S1 =Compound Operator: Intersection• In addition to the 5 basic operators, there are several additional“Compound Operators”– These add no computational power to thelanguage, but are useful shorthands.– Can be expressed solely with the basic ops.• Intersection takes two input relations, which must be union-compatible.• Q: How to express it using basic operators?R ∩ S = R − (R − S)Intersectionsidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S1S2sidsnameratingage31lubber855.558rusty1035.0! S1"S2Compound Operator: Join• Joins are compound operators involving crossproduct, selection, and (sometimes) projection.• Most common type of join is a “natural join” (oftenjust called “join”). R S conceptually is:– Compute R × S– Select rows where attributes that appear in both relationshave equal values– Project all unique atttributes and one copy of each of thecommon ones.• Note: Usually done much more efficiently than this.Natural Join Examplesidsnameratingage22dustin745.031lubber855.558rusty1035.0sidbidday2210110/10/965810311/12/96R1S1S1 R1 =sidsnameratingagebidday22dustin745.010110/10/9658rusty1035.010311/12/964Other Types of Joins•Condition Join (or “theta-join”):•Result schema same as that of cross-product.• May have fewer tuples than cross-product.•Equi-Join: Special case: condition c contains onlyconjunction of equalities.RcScR S>< = !"( )(sid) sname rating age (sid) bid day22 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 58 103 11/12/9611.1.1RSsidRsidS <<>Compound Operator: Division• Useful for expressing “for all” queries like:Find sids of sailors who have reserved all boats.• For A/B, attributes of B must be subset of attrs of A.–


View Full Document

Berkeley COMPSCI 186 - Relational Algebra

Documents in this Course
Load more
Download Relational Algebra
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Relational Algebra and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Relational Algebra 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?