Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.

Similar presentations


Presentation on theme: "Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes."— Presentation transcript:

1 Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes

2 Lecture 8COP 47202 Outline of Lecture Relational Algebra –Set Operations –Projection –Selection –Cartesian Product –Join –Combining Operations Completeness of Relational Algebra Operations Sec. 3.2

3 Lecture 8COP 47203 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

4 Lecture 8COP 47204 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

5 Lecture 8COP 47205 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

6 Lecture 8COP 47206 Sample Relational Schema Movies(title,year,length,filmType) MovieStars(name,address,gender,birthdate)

7 Lecture 8COP 47207 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)

8 Lecture 8COP 47208 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 1977 1991 1992 124 104 95 color Movies result set Comparison with null values return a false condition

9 Lecture 8COP 47209 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

10 Lecture 8COP 472010 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)

11 Lecture 8COP 472011 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 )

12 Lecture 8COP 472012 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

13 Lecture 8COP 472013 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

14 Lecture 8COP 472014 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

15 Lecture 8COP 472015 Example AB 12 34 BC 25 47 D 6 8 91011 x AR.BS.BCD RS 12256 12478 1291011 34 34 34 256 478 91011

16 Lecture 8COP 472016 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

17 Lecture 8COP 472017 Example AB 12 34 BC 25 47 D 6 8 91011 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 1256 3478

18 Lecture 8COP 472018 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

19 Lecture 8COP 472019 Example BC 23 23 D 4 5 7810 join A<D AND U.B  V.B AB 12 67 C 3 8 978 UV V.BV.CDAU.BU.C 1237810

20 Lecture 8COP 472020 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

21 Lecture 8COP 472021 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

22 Lecture 8COP 472022 Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)

23 Lecture 8COP 472023 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


Download ppt "Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes."

Similar presentations


Ads by Google