Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.

Similar presentations


Presentation on theme: "Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015."— Presentation transcript:

1 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015

2 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus

3 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Chapter 6 Outline  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary Relational Operations: JOIN and DIVISION  Additional Relational Operations  Examples of Queries in Relational Algebra

4 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 4 Relational Algebra Overview  Relational Algebra consists of several groups of operations  Unary Relational Operations SELECT (symbol:  (sigma)) PROJECT (symbol:  (pi)) RENAME (symbol:  (rho))  Relational Algebra Operations From Set Theory UNION (  ), INTERSECTION (  ), DIFFERENCE (or MINUS, – ) CARTESIAN PRODUCT ( x )  Binary Relational Operations JOIN (several variations of JOIN exist) DIVISION  Additional Relational Operations AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT, AVG, MIN, MAX)

5 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 5 Selection Condition  Operators:, =,   Simple selection condition:  operator  AND  OR  NOT  NOTE: Each attribute in condition must be one of the attributes of the relation name

6 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The Relational Algebra  Relational algebra  Basic set of operations for the relational model  Relational algebra expression  Sequence of relational algebra operations

7 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unary Relational Operations: SELECT and PROJECT  The SELECT Operation The Select operator selects tuples that satisfies a predicate  Example: retrieve the employees whose salary is 30,000 б Salary = 30,000 (Employee)

8 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Equivalence from SQL to RA SELECT * FROM R WHERE Equals

9 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

10 Unary Relational Operations: SELECT and PROJECT (cont’d.)  Example:  applied independently to each individual tuple t in R  If condition evaluates to TRUE, tuple selected  Boolean conditions AND, OR, and NOT  Unary  Applied to a single relation

11 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unary Relational Operations: SELECT and PROJECT (cont’d.)  Selectivity  Fraction of tuples selected by a selection condition  SELECT operation commutative  Cascade SELECT operations into a single operation with AND condition

12 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 12 Select Operator Produce table containing subset of rows of argument table satisfying condition Example: 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Person Id Name Address Hobby

13 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 13 Select Operator  Id>3000 OR Hobby=‘hiking’ (Person)  Id>3000 AND Id <3999 (Person)  NOT(Hobby=‘hiking’) (Person)  Hobby  ‘hiking’ (Person)  (Id > 3000 AND Id < 3999) OR Hobby  ‘hiking’ (Person) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Person Id Name Address Hobby

14 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 14 Find all employees with salary more than $40,000.  Salary > 40000 (Employee)

15 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

16 Select Operator Example NameAgeWeight Harry3480 Sally2864 George2970 Helena54 Peter3480 NameAgeWeight Harry3480 Helena54 Peter3480 Person б Age≥34 (Person) NameAgeWeight Helena54 б Age=Weight (Person)

17 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The PROJECT Operation  Selects columns from table and discards the other columns:  e.g., name of employees: ∏ name (Employee) e.g., name of employees earning more than 80,000: ∏ name (б Salary>80,000 (Employee))

18 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Harry Sally George Helena Peter Employee ∏ name (Employee)

19 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Sally Employee б Salary>80,000 (Employee) NameAgeSalary Sally2890,000 ∏ name (б Salary>80,000 (Employee))

20 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Sequences of Operations and the RENAME Operation  In-line expression:  Sequence of operations:  Rename attributes in intermediate results  RENAME operation

21 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Rename Operator  In relational algebra, a rename is a unary operation written as ρ a / b (R) where: a and b are attribute names R is a relation  The result is identical to R except that the b field in all tuples is renamed to an a field.  Example, rename operator changes the name of its input table to its subscript,  ρ employee (Emp)  Changes the name of Emp table to employee

22 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Rename Operator Example NameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000 Employee ρ EmployeeName / Name (Employee) EmployeeNameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000

23 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Relational Algebra Operations from Set Theory  UNION, INTERSECTION, and MINUS  Merge the elements of two sets in various ways  Binary operations  Relations must have the same type of tuples

24 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Union Operator  The union operation is denoted U as in set theory. It returns the union (set union) of two compatible relations.  For a union operation r U s to be legal, we require that, r and s must have the same number of attributes. The domains of the corresponding attributes must be the same.  As in all set operations, duplicates are eliminated.

25 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Union Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah Student Professor FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang JohnSmith RicardoBrown FrancisJohnson Student U Professor

26 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Intersection Operator  Denoted as . For relations R and S, intersection is R  S.  Defines a relation consisting of the set of all tuples that are in both R and S.  R and S must be union-compatible.  Expressed using basic operations: R  S = R – (R – S)

27 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Intersection Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah Student Professor FNLN SusanYao RameshShah Student  Professor

28 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Set Difference Operator  For relations R and S, Set difference R - S, defines a relation consisting of the tuples that are in relation R, but not in S. Set difference S – R, defines a relation consisting of the tuples that are in relation S, but not in R.

29 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Set Difference Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah Student Professor FNLN BarbaraJones AmyFord JimmyWang Student - Professor FNLN JohnSmith RicardoBrown FrancisJohnson Professor - Student

30 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Division Operator  The division operator takes as input two relations, called the dividend relation (r on scheme R) and the divisor relation (s on scheme S) such that all the attributes in S also appear in R and S is not empty.  The output of the division operation is a relation on scheme R with all the attributes common with S.  Denoted by ÷  Example: retrieve the names of employees who work on all the projects that ‘John Smith’ works on  Apply to relations R(Z) ÷ S(X)  Attributes of R are a subset of the attributes of S

31 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Division Operator Example StudentTask FredDatabase1 FredDatabase2 FredCompiler1 EugeneDatabase1 SaraDatabase1 SaraDatabase2 EugeneCompiler1 Task Database1 Database2 Completed DBProject Student Fred Sara Completed / DBProject

32 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Examples of Division A/B A B1 B2 B3 A/B1A/B2A/B3

33 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CARTESIAN PRODUCT (CROSS PRODUCT) Operation  CARTESIAN PRODUCT  CROSS PRODUCT or CROSS JOIN  Denoted by ×  Binary set operation  Relations do not have to be union compatible  Useful when followed by a selection that matches values of attributes

34 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Binary Relational Operations: JOIN and DIVISION  The JOIN Operation  Denoted by  Combine related tuples from two relations into single “longer” tuples  General join condition of the form AND AND...AND  Example:

35 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 35 Binary Relational Operations: JOIN  JOIN Operation (denoted by )  The sequence of CARTESIAN PRODECT followed by SELECT is used quite commonly to identify and select related tuples from two relations  A special operation, called JOIN combines this sequence into a single operation  This operation is very important for any relational database with more than a single relation, because it allows us combine related tuples from various relations  The general form of a join operation on two relations R(A1, A2,..., An) and S(B1, B2,..., Bm) is: R S  where R and S can be any relations that result from general relational algebra expressions.

36 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 36 Binary Relational Operations: JOIN (cont.)  Example: Suppose that we want to retrieve the name of the manager of each department.  To get the manager’s name, we need to combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple.  We do this by using the join operation.  DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE  MGRSSN=SSN is the join condition  Combines each department record with the employee who manages the department  The join condition can also be specified as DEPARTMENT.MGRSSN= EMPLOYEE.SSN

37 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 37 Example of applying the JOIN operation DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE

38 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 38 Some properties of JOIN  Consider the following JOIN operation:  R(A1, A2,..., An) S(B1, B2,..., Bm) R.Ai=S.Bj  Result is a relation Q with degree n + m attributes: Q(A1, A2,..., An, B1, B2,..., Bm), in that order.  The resulting relation state has one tuple for each combination of tuples—r from R and s from S, but only if they satisfy the join condition r[Ai]=s[Bj]  Hence, if R has n R tuples, and S has n S tuples, then the join result will generally have less than n R * n S tuples.  Only related tuples (based on the join condition) will appear in the result

39 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 39 Binary Relational Operations: EQUIJOIN  EQUIJOIN Operation  The most common use of join involves join conditions with equality comparisons only  Such a join, where the only comparison operator used is =, is called an EQUIJOIN.  In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple.  The JOIN seen in the previous example was an EQUIJOIN.

40 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

41

42 EQUIJOIN & Natural JOIN  EQUIJOIN  Only = comparison operator used  Always have one or more pairs of attributes that have identical values in every tuple  NATURAL JOIN  Denoted by *  Removes second (superfluous) attribute in an EQUIJOIN condition

43 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

44 Natural Join Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr FinanceGeorge SalesHarriet ProductionCharles Employee Dept NameEmpIDDeptNameMgr Harry3415FinanceGeorge Sally2241SalesHarriet George3401FinanceGeorge Harriet2202SalesHarriet Employee lXl Dept For an example, consider the tables Employee and Dept and their natural join:

45 Copyright © 2011 Ramez Elmasri and Shamkant Navathe A Complete Set of Relational Algebra Operations  Set of relational algebra operations {σ, π, ∪, ρ, –, ×} is a complete set  Any relational algebra operation can be expressed as a sequence of operations from this set

46 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 46 Binary Relational Operations: DIVISION  DIVISION Operation  The division operation is applied to two relations  R(Z)  S(X), where X subset Z. Let Y = Z - X (and hence Z = X  Y); that is, let Y be the set of attributes of R that are not attributes of S.  The result of DIVISION is a relation T(Y) that includes a tuple t if tuples t R appear in R with t R [Y] = t, and with t R [X] = t s for every tuple t s in S.  For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combination with every tuple in S.

47 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 47 Example of DIVISION

48 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 48 Additional Relational Operations: Aggregate Functions and Grouping  A type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.  Examples of such functions include retrieving the average or total salary of all employees or the total number of employee tuples.  These functions are used in simple statistical queries that summarize information from the database tuples.  Common functions applied to collections of numeric values include  SUM, AVERAGE, MAXIMUM, and MINIMUM.  The COUNT function is used for counting tuples or values.

49 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 49 Aggregate Function Operation  Use of the Aggregate Functional operation ℱ  ℱ MAX Salary (EMPLOYEE) retrieves the maximum salary value from the EMPLOYEE relation  ℱ MIN Salary (EMPLOYEE) retrieves the minimum Salary value from the EMPLOYEE relation  ℱ SUM Salary (EMPLOYEE) retrieves the sum of the Salary from the EMPLOYEE relation  ℱ COUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count (number) of employees and their average salary Note: count just counts the number of rows, without removing duplicates

50 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 50 Using Grouping with Aggregation  The previous examples all summarized one or more attributes for a set of tuples  Maximum Salary or Count (number of) Ssn  Grouping can be combined with Aggregate Functions  Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY  A variation of aggregate operation ℱ allows this:  Grouping attribute placed to left of symbol  Aggregate functions to right of symbol  DNO ℱ COUNT SSN, AVERAGE Salary (EMPLOYEE)  Above operation groups employees by DNO (department number) and computes the count of employees and average salary per department

51 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 51 Examples of applying aggregate functions and grouping

52 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 52 Additional Relational Operations (cont.)  Recursive Closure Operations  Another type of operation that, in general, cannot be specified in the basic original relational algebra is recursive closure. This operation is applied to a recursive relationship.  An example of a recursive operation is to retrieve all SUPERVISEES of an EMPLOYEE e at all levels — that is, all EMPLOYEE e’ directly supervised by e; all employees e’’ directly supervised by each employee e’; all employees e’’’ directly supervised by each employee e’’; and so on.

53 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Recursive Closure Operations  Operation applied to a recursive relationship between tuples of same type

54 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 54 Additional Relational Operations (cont.)

55 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 55 Database State for COMPANY  All examples discussed below refer to the COMPANY database shown here.

56 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Notation for Query Trees  Query tree  Represents the input relations of query as leaf nodes of the tree  Represents the relational algebra operations as internal nodes

57 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

58 Slide 6- 58 Examples of Queries in Relational Algebra : Procedural Form Q1: Retrieve the name and address of all employees who work for the ‘Research’ department. RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT) RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

59 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 59 Q2: Retrieve the names of employees who have no dependents. ALL_EMPS   SSN (EMPLOYEE) EMPS_WITH_DEPS ( SSN )   ESSN ( DEPENDENT ) EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS) RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

60 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 60

61 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Slide 6- 61

62 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

63

64

65

66

67

68

69 Operations of Relational Algebra

70 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Operations of Relational Algebra (cont’d.)

71 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Summary  The relational model has rigorously defined query languages that are simple and powerful.  Relational algebra is more operational; useful as internal representation for query evaluation plans.  Several ways of expressing a given query; a query optimizer should choose the most efficient version.

72 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Summary  Formal languages for relational model of data:  Relational algebra: operations, unary and binary operators  Some queries cannot be stated with basic relational algebra operations But are important for practical use


Download ppt "Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015."

Similar presentations


Ads by Google