Unformatted text preview:

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1Relational AlgebraChapter 4, Part ADatabase Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2Relational Query Languages Query languages: Allow manipulation and retrieval of 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.Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: Relational Algebra: More operational, very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.)Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input relations for a query are fixed (but query will run regardless of instance!) The schema for the result of a given query is also fixed! Determined by definition of query language constructs. Positional vs. named-field notation:  Positional notation easier for formal definitions, named-field notation more readable.  Both used in SQLDatabase Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5Example Instancessid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0sidbidday2210110/10/9658 103 11/12/96R1S1S2 “Sailors” and “Reserves” relations for our examples. We’ll use positional or named field notation, assume that names of fields in query results are `inherited’ from names of fields in query input relations.Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6Relational Algebra Basic operations: Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in reln. 1, but not in reln. 2. Union ( ) Tuples in reln. 1 and in reln. 2. Additional operations: Intersection, join, division, renaming: Not essential, but (very!) useful. Since each operation returns a relation, operationscan be composed! (Algebra is “closed”.)σπ−×Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7Projectionsnameratingyuppy9lubber8guppy5rusty10πsname ratingS,()2age35.055.5πageS()2 Deletes attributes that are not in projection list. Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Projection operator has to eliminate duplicates! (Why??) Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?)Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8SelectionσratingS>82()sidsnameratingage28yuppy935.058rusty1035.0snameratingyuppy9rusty10πσsname ratingratingS,(())>82 Selects rows that satisfy selection condition. No duplicates in result! (Why?) Schema of result identical to schema of (only) input relation. Result relation can be the input for another relational algebra operation! (Operatorcomposition.)Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9Union, Intersection, 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. What is the schema of result?sidsnameratingage22dustin745.031 lubber 8 55.558rusty1035.044guppy535.028yuppy935.0sidsnameratingage31lubber855.558rusty1035.0SS12∪SS12∩sidsnameratingage22dustin745.0SS12−Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10Cross-Product Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. Conflict: Both S1 and R1 have a field called sid.ρ((,),)CsidsidSR115211→→×(sid) sname rating age (sid) bid day22 dustin 7 45.0 22 101 10/10/9622 dustin 7 45.0 58 103 11/12/9631 lubber 8 55.5 22 101 10/10/9631 lubber 8 55.5 58 103 11/12/9658 rusty 10 35.0 22 101 10/10/9658 rusty 10 35.0 58 103 11/12/96 Renaming operator: Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11Joins Condition Join: Result schema same as that of cross-product. Fewer tuples than cross-product, might be able to compute more efficiently Sometimes called a theta-join. RcScRS =×σ()(sid)snameratingage(sid)bidday22dustin745.05810311/12/9631lubber855.55810311/12/96SRS sid R sid111 1 . .<Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12Joins Equi-Join: A special case of condition join where the condition c contains only equalities. Result schema similar to cross-product, but only one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields.sidsnameratingagebidday22dustin745.010110/10/9658rusty1035.010311/12/96SRsid11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved allboats. Let A have 2 fields, x and y; B have only field y: A/B =  i.e., A/B contains all x tuples (sailors) such that for every ytuple (boat) in B, there is an xy tuple in A. Or: If the set of y values (boats) associated with an x value (sailor) in A contains all y values in B, the x value is in A/B. In general, x and y can be any lists of fields; y is the list of fields in B, and x y is the list of fields of A.{}AyxByx ∈∃∈∀ ,|∪Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14Examples of Division A/Bsnopnos1p1s1p2s1p3s1p4s2p1s2p2s3p2s4p2s4p4pnop2pnop2p4pnop1p2p4snos1s2s3s4snos1s4snos1AB1B2B3A/B1 A/B2 A/B3Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15Expressing A/B Using Basic Operators Division is not essential op; just a useful


View Full Document

CORNELL CS 432 - Relational Algebra

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?