copyright © 2007 ramez elmasri and shamkant b. navathe chapter 6 (continued) the relational algebra...

22
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Upload: gwendolyn-booth

Post on 16-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Chapter 6 (Continued)

The Relational Algebra and Calculus

Page 2: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 2Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Generalized Projection

Allow functions to be applied to the attributes retrieved

Renaming is required for the results. Example

Report(Ssn, Net_salary, Bonus, Tax)

= Ssn, Salary-Deduction, 2000*years_service, 0.25*salary (EMPLOYEE)

Page 3: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 3Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

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: COUNT, SUM, AVERAGE, MAXIMUM, and

MINIMUM.

Page 4: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 4Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Aggregate Function Operation ℱ Pronounced “script F” ℱMAX Salary (EMPLOYEE) retrieves the maximum salary

value from the EMPLOYEE relation The result is a single attribute, single tuple relation. To make this more explicit, one can use

R1 (max_salary) = ℱMAX Salary (EMPLOYEE) ℱMIN Salary (EMPLOYEE) and ℱSUM Salary (EMPLOYEE) are

similar. ℱ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 This time the result is a 2-attribute, single tuple relation.

Page 5: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 5Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercises

Find the name(s) of the employees with the highest salary in the company.

Page 6: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 6Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercise

Count the number of employees who earns more than twice as much as the average salary.

Page 7: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 7Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Using Grouping with Aggregation

Grouping can be combined with Aggregate Functions A variation of aggregate operation allows this:ℱ

Grouping attribute placed to left of symbol Aggregate functions to right of symbol

Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY

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

Page 8: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 8Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Grouping by Dno

Page 9: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 9Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Examples of applying aggregate functions and grouping

auto-renaming

Renaming by

Page 10: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 10Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercise

List the SSN of employees who involve in exactly 3 projects.

Page 11: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 11Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercises List the department names that have more than

10 employees with salary $100,000

List the names of departments whose best-paid employees earn more than $200,000.

Page 12: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 12Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Recursive Closure Operations Another type of operation that, in general,

cannot be specified in the basic original relational algebra is recursive closure.

Example: 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.

Page 13: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 13Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercises

e = “James Borg”. Get e’

Get e’’ Get e’’’ Termination Condition ?

Page 14: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 14Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Additional Relational Operations (cont.)

Although it is possible to retrieve employees at each level and then take their union, we cannot, in general, specify a query such as “retrieve the supervisees of ‘James Borg’ at all levels” without utilizing a looping mechanism. The SQL3 standard includes syntax for recursive

closure.

Page 15: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 15Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

The OUTER JOIN Operation

In NATURAL JOIN and EQUIJOIN, tuples without a matching (or related) tuple are eliminated from the join result Tuples with null in the join attributes are also

eliminated This amounts to loss of information.

OUTER joins can be used when we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the join, regardless of whether or not they have matching tuples in the other relation.

Page 16: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 16Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Additional Relational Operations (cont.)

The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is found in S, then the attributes of S in the join result are filled or “padded” with null values.

A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R S.

A third operation, full outer join, denoted by keeps all tuples in both the left and the right relations when no matching tuples are found, padding them with null values as needed.

Page 17: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 17Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

An Example of Left Outer Join T = EMPLOYEE ssn=mgr_ssn DEPARTMENT

RESULT = Fname, Minit, Lname, Dname (T)

Page 18: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 18Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercises

Use outer joins to count the number of employees who are not working on any project.

Page 19: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 19Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Exercises Use outer joins to list SSNs of employees who do

not supervise anyone.

Page 20: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 20Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

OUTER UNION Operations

The outer union operation takes the union of two relations R(X, Y) and S(X, Z) that are partially compatible, meaning that only some of their attributes, say X, are type compatible.

The attributes that are type compatible are represented only once in the result, and those attributes that are not type compatible from either relation are also kept in the result relation

T(X, Y, Z).

Page 21: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 21Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Example of OUTER UNIONs Consider two relations

STUDENT (Name, SSN, Dept, Advisor) INSTRUCTOR (Name, SSN, Dept, Rank).

Their OUTER UNION will have the following attributes:

STU_OR_INS (Name, SSN, Dept, Advisor, Rank) Tuples from the two relations are matched based on the

shared attributes — Name, SSN, Department. If a student is also an instructor, both Advisor and Rank will

have a value; otherwise, one of these two attributes will be null. A person being both a student of CS and an instructor of MATH

will have two entries in STU_OR_INS.

Page 22: Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus

Slide 22Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

Chapter Summary

Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra

Relational Calculus Tuple Relational Calculus Domain Relational Calculus

Overview of the QBE language (appendix C)