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 4pRelational Query Languages•Query languages: Allow manipulation and retrieval ofdata 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 the querylanguage 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 ( s ) Selects a subset of rows from relation(horizontal).•Projection ( p ) Retains only wanted columns from relation(vertical).•Cross-product ( ¥ ) Allows us to combine two relations.•Set-difference ( — ) Tuples in r1, but not in r2.•Union ( » ) Tuples in r1 and/or in r2.Since each operation returns a relation, operations can becomposed! (Algebra is “closed”.)Example Instancessidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0sidbidday2210110/10/965810311/12/96R1S1S2bidbnamecolor101Interlakeblue102Interlakered103Clippergreen104MarineredBoats2ProjectionpageS( )2• Examples: ;• Retains only attributes that are in the “projectionlist”.•Schema of result:– exactly the fields in the projection list, with thesame names that they had in the input relation.• Projection operator has to eliminate duplicates(How do they arise? Why remove them?)– Note: real systems typically don’t do duplicateelimination unless the user explicitly asks for it.(Why not?)psname ratingS,( )2Projectionsnameratingyuppy9lubber8guppy5rusty10)2(,Sratingsnamepage35.055.5sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0pageS( )2S2Selection (s)sratingS>82( )snameratingyuppy9rusty10p ssname ratingratingS,( ( ))>82• Selects rows that satisfy selection condition.• Result is a relation.Schema of result is same as that of the inputrelation.• 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• All of these operations take two input relations,which must be union-compatible:– Same number of fields.– `Corresponding’ fields have the same type.• For which, if any, is duplicate eliminationrequired?Unionsidsnameratingage22dustin745.031lubber855.558rusty1035.044guppy535.028yuppy935.0S S1 2»sidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S1S2Set Differencesidsnameratingage22dustin745.031lubber855.558rusty1035.0sidsnameratingage28yuppy935.031lubber855.544guppy535.058rusty1035.0S1S2sidsnameratingage22dustin745.0S S1 2-S2 – S1sidsnameratingage28yuppy935.044guppy535.03Cross-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 havea field with the same name.– In this case, can use the renaming operator:r( ( , ), )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 areseveral 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, whichmust 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.0S S1 2«Compound 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.• Useful for putting “normalized” relations backtogether.Natural Join Examplesidsnameratingage22dustin745.031lubber855.558rusty1035.0sidbidday2210110/10/965810311/12/96R1S1R1 S1 =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 containsonly conjunction of equalities.RcScR S>< = ¥s( )(sid)snameratingage(sid)bidday22dustin745.05810311/12/9631lubber855.55810311/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 are subset of attrs of


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?