Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes
Lecture 8COP Outline of Lecture Relational Algebra –Set Operations –Projection –Selection –Cartesian Product –Join –Combining Operations Completeness of Relational Algebra Operations Sec. 3.2
Lecture 8COP Database Query Languages Given a database, ask questions, get data as answers –Ex: Get all students with GPA > 3.7 who applied to Berkeley and Stanford and nowhere else –Ex: Get all humanities departments at campuses in Florida with < 1000 applicants –Ex: Get the campus with highest average accept rate over the last five years Some questions are easy to pose, some are not Some questions are easy for DBMS to answer, some are not. "Query language" also used to update the database
Lecture 8COP Relational Query Languages Formal(pure): relational algebra, relational calculus Actual: SQL In all languages, a query is executed over a set of relations, get a relation as the result
Lecture 8COP Relational Algebra Construct new relations from old ones –Set of operators –Relations are operands Build progressively more complex expressions by applying operators to relations or to rel. algebra expressions (which are relations as well) Query is a relational algebra expression –First concrete example of a query language Four broad classes of operations –Set operations, selection operations, operations that combine data from two relations, rename operation
Lecture 8COP Sample Relational Schema Movies(title,year,length,filmType) MovieStars(name,address,gender,birthdate)
Lecture 8COP Relational Operators: Select Select or Restrict (unary) (r) = {t | t r and predicate(t)} is a conditional expression of the type that we are familiar with from conventional programming languages – –attribute in R –op {=, ,, , …, AND, OR} Ex: length 100 (Movies)
Lecture 8COP Pictorially A 1 A 2 A 3 … A n... i A 1 A 2 A 3 … A n... j, i j titleyearlengthfilmType Star Wars Mighty Ducks Wayne’s World color Movies result set Comparison with null values return a false condition
Lecture 8COP Relational Operators: Project Project (unary) (r) is a list of attributes (columns) from R only Ex: title, year, length (Movies) A 1 A 2 A 3 … A n... i A 1 A 2 … A k... i n k, k<=n
Lecture 8COP Relational Operators: Rename Rename (unary) New (Old) New is the name and schema of the relation to be renamed (Old) Ex: MyMovies (Movies) Ex: MyMovies(title,year,length,type) (Movies)
Lecture 8COP Set Operations Union (binary, commutative, associative) R S Intersection (binary, commutative, associative) R S Set Difference (binary) R - S –Set of elements in R but not in S –R-S S-R !! R(A 1,A 2,…,A n ), S(B 1,B 2,…,B n ) must be union compatible –R and S are of the same degree –for each i, dom(A i ) = dom(B i )
Lecture 8COP Example nameaddressgenderbirthdate Carrie Fisher Mark Hamil 123 Maple St., Hollywood 456 Oak Rd., Brentwood F M 9/9/99 8/8/88 R nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 S
Lecture 8COP Sample Operations nameaddressgenderbirthdate Carrie Fisher123 Maple St., HollywoodF 9/9/99 R S nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 R S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88 nameaddressgenderbirthdate R - S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88
Lecture 8COP Cartesian Product Cartesian Product (binary, commutative, associative) R x S Sets of all pairs that can be formed by choosing the first element of the pair to be any element of R, the second any element of S Resulting schema may be ambiguous –Use R.A or S.A to remove ambiguity for an attribute that occurs in both schemas –Alternatively, use Rename operator and rename
Lecture 8COP Example AB BC D x AR.BS.BCD RS
Lecture 8COP Join Operations Natural Join (binary) R join S R S Match only those tuples from R and S that agree in whatever attributes are common to the schemas of R and S –If r and s from r(R) and s(S) are successfully paired, result is called a joined tuple
Lecture 8COP Example AB BC D join Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and S Tuples that fail to pair with any tuple of the other relation are called dangling tuples ABCD RS
Lecture 8COP Join Operations Theta Join (binary) R join C S, where C is an arbitrary join condition –R S is an arbitrary join condition Step 1: take the product of R and S Step 2: Select from the product only those tuples that satisfy condition C As with the product operation, the schema for the result is the union of the schemas of R and S natural join - join based on common attributes R S = R x S
Lecture 8COP Example BC D join A<D AND U.B V.B AB C UV V.BV.CDAU.BU.C
Lecture 8COP Combining Operations to Form Queries Form expressions of arbitrary complexity by applying operators either to –given relations or –to relations that are the result of applying one or more relational operators to relations Single relational expression, using parenthesis as necessary Apply only one operator at a time and create intermediate result relations
Lecture 8COP Complete Set of Relational Algebra Expressions select project union set difference cartesian product Any of the other operators can be expressed as a sequence of operations from this set
Lecture 8COP Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)
Lecture 8COP Sample Queries PROJECT _{date,decision} (Apply) Campus X Apply Find Names and addresses of all students with GPA > 3.7 who applied to CS major and were rejected List name and address of all students with GPA > 3.7