Presentation is loading. Please wait.

Presentation is loading. Please wait.

DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.

Similar presentations


Presentation on theme: "DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra."— Presentation transcript:

1 DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra

2 DatabaseIM ISU2 Chapter Outline  Relational Algebra  Unary Relational Operations  Relational Algebra Operations From Set Theory  Binary Relational Operations  Additional Relational Operations  Examples of Queries in Relational Algebra  Example Database Application (COMPANY)

3 DatabaseIM ISU3 Relational Algebra Overview  Basic concepts  The relational algebra is a set of operations to manipulate relations  Used to specify retrieval requests (queries)  Query result is in the form of a relation

4 DatabaseIM ISU4 The Relational Algebra  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 (  )  Binary Relational Operations »JOIN (several variations of JOIN exist) »DIVISION  Additional Relational Operations »OUTER JOINS, OUTER UNION »AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT, AVG, MIN, MAX)

5 DatabaseIM ISU5 Unary Relational Operations  SELECT operation   Selects the tuples (rows) from a relation R that satisfy a certain selection condition c  Form of the operation:  c (R) »The condition c is an arbitrary Boolean expression (AND, OR, NOT) on the attributes of R  Resulting relation has the same attributes as R

6 DatabaseIM ISU6  (DNO=4 AND SALARY>25000) OR (DNO=5 AND SALARY > 30000) (EMPLOYEE)

7 DatabaseIM ISU7 Unary Relational Operations (cont.)  PROJECT operation   Keeps only certain attributes (columns) from a relation R specified in an attribute list L  Form of operation:  L (R)  Resulting relation has only those attributes of R specified in L  The PROJECT operation eliminates duplicate tuples in the resulting relation

8 DatabaseIM ISU8 Unary Relational Operations (cont.)  FNAME,LNAME,SALARY (EMPLOYEE)  SEX,SALARY (EMPLOYEE)

9 DatabaseIM ISU9 Unary Relational Operations (cont.)  Sequences of operations  Several operations can be combined to form a relational algebra expression (query) »e.g., retrieve the names and salaries of employees who work in department 5:  FNAME,LNAME,SALARY (  DNO=5 (EMPLOYEE))

10 DatabaseIM ISU10 Unary Relational Operations (cont.)  Alternatively, we specify explicit intermediate relations for each step DEPT5_EMPS   DNO=5 (EMPLOYEE) RESULT   FNAME,LNAME,SALARY (DEPT5_EMPS)  Attributes can optionally be renamed in the resulting left-hand-side relation TEMP   DNO=5 (EMPLOYEE) R(FIRSTNAME,LASTNAME,SALARY)   FNAME,LNAME,SALARY (TEMP)

11 DatabaseIM ISU11 Unary Relational Operations (cont.)

12 DatabaseIM ISU12 Unary Relational Operations (cont.)  RENAME operation  (rho) General form:  S (B 1, B 2, …, B n ) (R) changes both: »the relation name to S, and »the column (attribute) names to B 1, B 2, …..B n  S (R) changes: »the relation name only to S  (B 1, B 2, …, B n ) (R) changes: »the column (attribute) names only to B 1, B 2, …..B n

13 DatabaseIM ISU13 Set Operations  Operations from set theory  Binary operations from mathematical set theory  UNION: R 1  R 2  INTERSECTION: R 1  R 2  SET DIFFERENCE: R 1  R 2  CARTESIAN PRODUCT: R 1  R 2

14 DatabaseIM ISU14 Set Operations (cont.)  Union compatibility  For , , , the operand relations R 1 (A 1, A 2,..., A n ) and R 2 (B 1, B 2,..., B n ) must have the same number of attributes, and the domains of corresponding attributes must be compatible  The resulting relation for , , , has the same attribute names as the first operand relation R 1 (by convention)

15 DatabaseIM ISU15 The Relational Algebra (cont.)

16 DatabaseIM ISU16 Set Operations (cont.)

17 DatabaseIM ISU17 Set Operations (cont.)  Cartesian product (cross product, cross join)  R(A 1, A 2,..., A m, B 1, B 2,..., B n )  R 1 (A 1, A 2,..., A m )  R 2 (B 1, B 2,..., B n )  A tuple t exists in R for each combination of tuples t 1 from R 1 and t 2 from R 2 such that t[A 1, A 2,..., A m ]=t 1 and t[B 1, B 2,..., B n ]=t 2  If R 1 has n 1 tuples and R 2 has n 2 tuples, then R will have n 1 *n 2 tuples

18 DatabaseIM ISU18 Set Operations (cont.)  CARTESIAN PRODUCT is a meaningless operation on its own  It is useful when followed by a SELECT operation that matches values of attributes coming from the component relations

19 DatabaseIM ISU19 Set Operations (cont.) Example: FEMALE_EMPS   SEX=’F’ (EMPLOYEE) EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS) EMP_DEPENDENTS  EMPNAMES  DEPENDENT ACTUAL_DEPENDENTS   SSN=ESSN (EMP_DEPENDENTS) RESULT   FNAME, LNAME,DEPENDENT_NAME (ACTUAL_DEPENDENTS)

20 DatabaseIM ISU20 The Relational Algebra (cont.)

21 DatabaseIM ISU21 Set Operations (cont.)

22 DatabaseIM ISU22 Binary Operations  JOIN Operations  THETA JOIN »R(A 1, A 2,..., A m, B 1, B 2,..., B n )  R 1 (A 1, A 2,..., A m )  c R 2 (B 1, B 2,..., B n ) »The condition c is called a join condition of the form AND AND... AND –Each condition is of the form A i  B j, A i and B j have the same domain –  is one of the comparison operators {=,, ,  }

23 DatabaseIM ISU23 Binary Operations (cont.) »Example DEPT_MGR  DEPARTMENT  MGRSSN=SSN EMPLOYEE »THETA JOIN is similar to a CARTESIAN PRODUCT followed by a SELECT, e.g., DEP_EMP  DEPARTMENT  EMPLOYEE DEPT_MGR   MGRSSN=SSN (DEP_EMP)

24 DatabaseIM ISU24 Binary Operations (cont.)  EQUIJOIN »The join condition c involves only equality comparisons (A i =B j ) AND... AND (A h =B k ); 1<i,h<m, 1<j,k<n »A i,..., A h are called the join attributes of R 1 »B j,..., B k are called the join attributes of R 2 »Notice that in the result of an EQUIJOIN one or more pairs of attributes have identical values in every tuple e.g., MGRSSN and SSN in Figure 6.6

25 DatabaseIM ISU25 Binary Operations (cont.)  NATURAL JOIN (*) R  R 1 * (join attributes of R1),(join attributes of R2) R 2 »In a NATURAL JOIN, the redundant join attributes of R 2 are eliminated from R »The equality condition is implied and need not be specified »Example Retrieve each EMPLOYEE's name and the name of the DEPARTMENT he/she works for: T  EMPLOYEE * (DNO),(DNUMBER) DEPARTMENT RESULT   FNAME,LNAME,DNAME (T)

26 DatabaseIM ISU26 Binary Operations (cont.) »If the join attributes have the same names in both relations, they need not be specified and we can write R  R 1 * R 2 »Example Retrieve each EMPLOYEE's name and the name of his/her SUPERVISOR: SUPERVISOR(SUPERSSN,SFN,SLN)   SSN,FNAME,LNAME (EMPLOYEE) T  EMPLOYEE * SUPERVISOR RESULT   FNAME,LNAME,SFN,SLN (T)

27 DatabaseIM ISU27 Binary Operations (cont.) »Note: In the original definition of NATURAL JOIN, the join attributes were required to have the same names in both relations

28 DatabaseIM ISU28 Binary Operations (cont.)  The natural join or equijoin operation can also be specified among multiple tables, leading to an n-way join »For example, consider the following three-way join: ((PROJECT  DNUM=DNUMBER DEPARTMENT)  MGRSSN=SSN EMPLOYEE)

29 DatabaseIM ISU29 Binary Operations (cont.)  A relation can have a set of join attributes to join it with itself, e.g., JOIN ATTRIBUTES RELATIONSHIP EMPLOYEE(1).SUPERSSN= EMPLOYEE(2) supervises EMPLOYEE(2).SSN EMPLOYEE(1) »This type of operation (called recursive closure algebra) is applied to a recursive relationship »One can think of this as joining two distinct copies of the relation, although only one relation actually exists »In this case, renaming can be useful

30 DatabaseIM ISU30 Binary Operations (cont.) »Example 1 Retrieve each EMPLOYEE's name and the name of his/her SUPERVISOR: SUPERVISOR(SSSN,SFN,SLN)   SSN,FNAME,LNAME (EMPLOYEE) T  EMPLOYEE  SUPERSSN=SSSN SUPERVISOR RESULT   FNAME,LNAME,SFN,SLN (T)

31 DatabaseIM ISU31 Binary Operations (cont.) Example 2 Retrieve all employees supervised by ‘James Borg’ at level 1: BORG_SSN   SSN (  FNAME=’James’ AND LNAME=’Borg’ (EMPLOYEE)) SUPERVISION(SSN1, SSN2)   SSN, SUPERSSN (EMPLOYEE) RESULT1(SSN)   SSN1 (SUPERVISION  SSN2=SSN BORG_SSN)

32 DatabaseIM ISU32 Binary Operations (cont.) Example 2 (cont.) Retrieve all employees supervised by ‘James Borg’ at level 2: RESULT2(SSN)   SSN1 (SUPERVISION  SSN2=SSN RESULT1) All employees supervised at levels 1 and 2 by ‘James Borg:’ RESULT  RESULT2  RESULT1

33 DatabaseIM ISU33 Binary Operations (cont.)  DIVISION Operation  The DIVISION operation is applied to two relations R(Z) ÷ S(X), where X  Z  That is, the result of DIVISION is a relation T(Y) = R(Z) ÷ S(X), Y = Z  X  Example »Retrieve the names of employees who work on all the projects that ‘John Smith’ works on

34 DatabaseIM ISU34 Binary Operations (cont.) SMITH   FNAME=’John’ AND LNAME=’Smith’ (EMPLOYEE) SMITH_PNOS   PNO (WORKS_ON  ESSN=SSN SMITH) SSN_PNOS   ESSN,PNO (WORKS_ON) SSNS(SSN)  SSN_PNOS ÷ SMITH_PNOS RESULT   FNAME, LNAME (SSNS * EMPLOYEE)

35 DatabaseIM ISU35 Binary Operations (cont.)

36 DatabaseIM ISU36 Binary Operations (cont.)

37 DatabaseIM ISU37 Binary Operations (cont.)  The DIVISION operator can be expressed as a sequence of , , and  operations as follows: T 1   Y (R) T 2   Y ((S  T 1 )  R) T  T 1  T 2

38 DatabaseIM ISU38 Complete Set of Relational Algebra  Complete Set of Relational Algebra Operations  All the operations discussed so far can be described as a sequence of only the operations SELECT, PROJECT, UNION, SET DIFFERENCE, and CARTESIAN PRODUCT  Hence, the set { ,  } is called a complete set of relational algebra operations

39 DatabaseIM ISU39 Complete Set of Relational Algebra (cont.)  Any query language equivalent to these operations is called relationally complete  For database applications, additional operations are needed that were not part of the original relational algebra. These include: 1. Aggregate functions and grouping 2. OUTER JOIN and OUTER UNION

40 DatabaseIM ISU40 Additional Relational Operations  Generalized projection  A useful operation for developing reports with computed values output as columns  Form of operation  F 1, F 2, …, F n (R)  F 1, F 2, …, F n are functions over attributes in R

41 DatabaseIM ISU41 Additional Relational Operations (cont.)  Example »Relation EMPLOYEE(SSN, SALARY, DEDUCTION, YEARS_SERVICE) »Relation expression REPORT   (Ssn, Net_salary, Bonus, Tax) (  ssn, salary – deduction, 2000*years_service, 0.25*salary (EMPLOYEE)) SSNNet_salaryBonusTax

42 DatabaseIM ISU42 Additional Relational Operations (cont.)  Aggregate functions  Functions such as SUM, COUNT, AVERAGE, MIN, MAX are often applied to sets of values or sets of tuples in database applications  (R)  The grouping attributes are optional  is a list of ( ) pairs

43 DatabaseIM ISU43 Additional Relational Operations (cont.)  Example 1 For each department, retrieve the department number, the number of employees, and the average salary (in the department): »Attributes renaming R(DNO,NUMEMPS,AVGSAL)  DNO  COUNT SSN, AVERAGE SALARY (EMPLOYEE) –DNO is called the grouping attribute »No attributes renaming DNO  COUNT SSN, AVERAGE SALARY (EMPLOYEE)

44 DatabaseIM ISU44 Additional Relational Operations (cont.) The resulting attributes nam are in the form _

45 DatabaseIM ISU45 Additional Relational Operations (cont.)  Example 2 -- no grouping attributes are specified Retrieve the average salary of all employees (no grouping needed) R(AVGSAL)   AVERAGE SALARY (EMPLOYEE) –The functions are applied to the attribute values of all the tuples in the relation, so the resulting relation has a single tuple only

46 DatabaseIM ISU46 Additional Relational Operations (cont.)  OUTER JOIN  In a regular EQUIJOIN or NATURAL JOIN operation, tuples in R 1 or R 2 that do not have matching tuples in the other relation do not appear in the result  Some queries require all tuples in R 1 (or R 2 or both) to appear in the result  When no matching tuples are found, nulls are placed for the missing attributes

47 DatabaseIM ISU47 Additional Relational Operations (cont.)  LEFT OUTER JOIN R 1  R 2 »Lets every tuple in R 1 appear in the result »Example List all employee names and also the name of the departments they manage if they happen to manage a department: TEMP  (EMPLOYEE  SSN=MGRSSN DEPARTMENT) RESULT   FNAME, MINIT, LNAME, DNAME (TEMP)

48 DatabaseIM ISU48 Additional Relational Operations (cont.)

49 DatabaseIM ISU49 Additional Relational Operations (cont.)  RIGHT OUTER JOIN R 1  R 2 »Lets every tuple in R 2 appear in the result  FULL OUTER JOIN R 1  R 2 »Lets every tuple in R 1 or R 2 appear in the result

50 DatabaseIM ISU50 Additional Relational Operations (cont.)  OUTER UNION »It was developed to take the union of tuples from two relations if the relations are not union compatible »This operation will take the UNION of tuples in two relations that are partially compatible »Example –STUDENT(Name, SSN, Department, Advisor) and FACULTY(Name, SSN, Department, Rank). –The resulting relation R(Name, SSN, Department, Advisor, Rank)

51 DatabaseIM ISU51 Example Queries  Query 1  Retrieve the name and address of all employees who work for the ‘Research’ department: RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT) RESEARCH_EMPS  (RESEARCH_DEPT  DNUMBER=DNO EMPLOYEE) RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

52 DatabaseIM ISU52 Example Queries (cont.)  Query 2  For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, address, and birthdate: STAFFORD_PROJS   PLOCATION=’Stafford’ (PROJECT) CONTR_DEPT  (STAFFORD_PROJS  DNUM=DNUMBER DEPARTMENT) PROJ_DEPT_MGR  (CONTR_DEPT  MGRSSN=SSN EMPLOYEE) RESULT   PNUMBER, DNUM, LNAME, ADDRESS, BDATE (PROJ_DEPT_MGR)

53 DatabaseIM ISU53 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


Download ppt "DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra."

Similar presentations


Ads by Google