Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra and Calculus

Similar presentations


Presentation on theme: "The Relational Algebra and Calculus"— Presentation transcript:

1 The Relational Algebra and Calculus
Chapter 6 The Relational Algebra and Calculus Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe

2 Chapter Outline Relational Algebra Relational Calculus
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 Example Database Application (COMPANY) Overview of the QBE language (appendix D)

3 Relational Algebra Overview
Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) The result of an operation is a new relation, which may have been formed from one or more input relations

4 Relational Algebra Overview (continued)
The algebra operations thus produce new relations These can be further manipulated using operations of the same algebra A sequence of relational algebra operations forms a relational algebra expression The result of a relational algebra expression is also a relation that represents the result of a database query (or retrieval request)

5 The COMPANY Database

6 The following query results refer to this database state

7 Unary Relational Operations: SELECT
The SELECT operation (denoted by  (sigma)) is used to select a subset of the tuples from a relation based on a selection condition. Keeps only those tuples that satisfy the qualifying condition Examples: Select the EMPLOYEEs whose department # is 4:  DNO = 4 (EMPLOYEE) Select the EMPLOYEEs whose salary > $30,000:  SALARY > 30,000 (EMPLOYEE)

8 SELECT Operation Properties
The SELECT operation  <selection condition>(R) produces a relation S that has the same schema (same attributes) as R SELECT  is commutative:  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R)) Because of commutativity, a sequence of SELECT operations may be applied in any order: <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R))) A cascade of SELECT operations may be replaced by a single selection with a conjunction of all the conditions: <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))

9 Unary Relational Operations: PROJECT
PROJECT Operation is denoted by  (pi) This operation keeps certain columns (attributes) from a relation and discards the other columns. Example: To list each employee’s first and last name and salary, the following is used: LNAME, FNAME,SALARY(EMPLOYEE)

10 <attribute list>(R)
PROJECT (cont.) The general form of the project operation is: <attribute list>(R) <attribute list> is the desired list of attributes from relation R. The project operation removes any duplicate tuples This is because the result of the project operation is still a relation, that is, a set of tuples

11 PROJECT Operation Properties
The number of tuples in the result of projection <list>(R) is always less or equal to the number of tuples in R If the list of attributes includes a key of R, then the number of tuples in the result of PROJECT is equal to the number of tuples in R PROJECT is not commutative  <list1> ( <list2> (R) ) =  <list1> (R) as long as <list2> contains the attributes in <list1>

12 Examples of applying SELECT and PROJECT operations

13 Single expression versus sequence of relational operations (Example)
Task: To retrieve the first name, last name, and salary of all employees who work in department number 5. Solution 1: A single relational algebra expression: FNAME, LNAME, SALARY( DNO=5(EMPLOYEE)) Solution 2: a sequence of operations, giving a name to each intermediate relation: DEP5_EMPS   DNO=5(EMPLOYEE) RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

14 Example of applying multiple operations and RENAME
LNAME, FNAME,SALARY(Dno=5 (EMPLOYEE)) Temp = Dno=5 (EMPLOYEE) R (First_name, Last_name, Salary) = LNAME, FNAME,SALARY(Temp) * Notice the implied renaming

15 Dno ( SSN = 123456789 (EMPLOYEE)) results in {(5)}.
Important Note The Select and Project operations produce relations. Dno ( SSN = (EMPLOYEE)) results in {(5)}. The schema of the relation is (Dno). The result relation has only one tuple. It is still a relation, not a number.

16 Unary Relational Operations: RENAME
The RENAME operator is denoted by  (rho) In some cases, we may want to rename the attributes of a relation or the relation name or both Useful when a query requires multiple operations Necessary in some cases (see JOIN operation later)

17 Example of applying multiple operations and RENAME
Temp = Dno=5 (EMPLOYEE) R = (First_name, Last_name Salary) (LNAME, FNAME,SALARY(Temp)) * Notice the explicit renaming

18 Relational Algebra Operations from Set Theory
Type Compatibility of operands is required for the binary set operation UNION , (also for INTERSECTION , and SET DIFFERENCE –, see next slides) R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type compatible if: they have the same number of attributes, and the domains of corresponding attributes are type compatible (i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n). The resulting relation for R1R2 (also for R1R2, or R1–R2) has the same attribute names as the first operand relation R1 (by convention)

19 Example Uses of UNION DEP5_EMPS  DNO=5 (EMPLOYEE)
To retrieve the SSNs of all employees who either work in department 5 (RESULT1 below) or directly supervise an employee who works in department 5 (RESULT2 below) We can use the UNION operation as follows: DEP5_EMPS  DNO=5 (EMPLOYEE) RESULT1  SSN(DEP5_EMPS) RESULT2(SSN)  SUPERSSN(DEP5_EMPS) RESULT  RESULT1  RESULT2 The union operation produces the tuples that are in either RESULT1 or RESULT2 or both

20 Examples Union (two type compatible relations) Intersection STU - INS
INS - STU

21 Troubles with Attribute Names
We define the domain Date (mm/dd/yyyy). R1 (Birth_date, SSN), where Birth_date is of domain Date R2 (Death_date, SSN), where Death_date is also of domain Date. R3 = R1  R2 will have the attribute list (Birth_date, SSN), which sounds weird. R3 is best named as R3(BorD_date, SSN) = R1  R2 Or more formally, R3 =  (BorD_date, SSN) (R1  R2)

22 The COMPANY Database

23 Exercises Give the SSNs of those managers have dependent(s).
Give the SSNs non-manager employees Give the SSNs of employees who are both supervisors and supervisees

24 Properties of Set Operations
Notice that both union and intersection are commutative; that is R  S = S  R, and R  S = S  R Both union and intersection are associative operations; that is R  (S  T) = (R  S)  T (R  S)  T = R  (S  T) The minus operation is not commutative; that is, in general R – S ≠ S – R

25 Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT
CARTESIAN (or CROSS) PRODUCT Operation This operation is used to combine tuples from two relations in a combinatorial fashion. Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) 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—one from R and one from S. Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS tuples, then R x S will have nR * nS tuples. The two operands do NOT have to be "type compatible”

26 CARTESIAN PRODUCT (cont.)
Example: To find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’(EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES x DEPENDENT ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

27 Example of applying CARTESIAN PRODUCT

28 More Exercises List the names of those departments that have at least one female employee whose salary  $100,000. List the Names and SSNs of the managers of the above departments.

29 R <join condition>S
JOIN Operator ( ) 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 The general form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: R <join condition>S where R and S can be any relations that result from general relational algebra expressions.

30 Exercise Use the JOIN operator to find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’(EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES x DEPENDENT ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

31 Exercises List the names of department managers.
Use Cartesian Product and Select Use Join

32 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.

33 NATURAL JOIN Operations
Another variation of JOIN called NATURAL JOIN — denoted by * — was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. because one of each pair of attributes with identical values is superfluous The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations If this is not the case, a renaming operation is applied first.

34 Exercise Use the Natural JOIN operator to find the dependents’ names of all female employees FEMALE_EMPS   SEX=’F’(EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

35 Results of Natural JOINs

36 NATURAL JOIN: Example 2 Because of the same attribute name Dnumber, Natural Join can be applied directly to DEPARTMENT and DEP_LOCATION DEPT_LOCS  DEPARTMENT * DEPT_LOCATIONS

37 NATURAL JOIN (contd.) Notice that all pairs of identical attribute names are compared Example: Q  R(A,B,C,D) * S(C,D,E) The implicit join condition includes each pair of attributes with the same name, “AND”ed together: R.C=S.C AND R.D=S.D Result keeps only one attribute of each such pair: Q(A,B,C,D,E)

38

39 The COMPANY Database

40 Exercise List names and SSNs of those EMPLOYEE who work on some project(s) located in Houston.

41 Exercise List the names and birth dates of all female dependents born after 1980.

42 Exercise List the names of all employees who earns salary  $10,000 but does not have a supervisor.

43 Exercises List the names of all employees who earns salary  $10,000 but does not supervise anyone.


Download ppt "The Relational Algebra and Calculus"

Similar presentations


Ads by Google