Database Systems - Lec 5: Relational Algebra - Nguyen Thanh Tung

§Relational Algebra

•Unary Relational Operations

•Relational Algebra Operations from Set Theory

•Binary Relational Operations

•Additional Relational Operations

§Brief Introduction to Relational Calculus

§Reading:

•[1]: Chapter 6

ppt 60 trang xuanthi 02/01/2023 260
Bạn đang xem 20 trang mẫu của tài liệu "Database Systems - Lec 5: Relational Algebra - Nguyen Thanh Tung", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pptdatabase_systems_lec05_relational_algebra_nguyen_thanh_tung.ppt

Nội dung text: Database Systems - Lec 5: Relational Algebra - Nguyen Thanh Tung

  1. Outline ▪ Relational Algebra • Unary Relational Operations • Relational Algebra Operations from Set Theory • Binary Relational Operations • Additional Relational Operations ▪ Brief Introduction to Relational Calculus ▪ Reading: • [1]: Chapter 6 2
  2. 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 →OUTER JOINS, OUTER UNION →AGGREGATE FUNCTIONS (SUM, COUNT, AVG, MIN, MAX) 4
  3. The following query results refer to this database state 6
  4. Unary Relational Operations: SELECT • In general, the select operation is denoted by  (R) where →the symbol  (sigma) is used to denote the select operator →the selection condition is a Boolean (conditional) expression specified on the attributes of relation R →tuples that make the condition true appear in the result of the operation, and tuples that make the condition false are discarded from the result of the operation 8
  5. Unary Relational Operations: PROJECT ▪ PROJECT Operation is denoted by (pi) ▪ This operation keeps certain columns (attributes) from a relation and discards the other columns • PROJECT creates a vertical partitioning: the list of specified columns (attributes) is kept in each tuple, the other attributes in each tuple are discarded ▪ Example: To list each employee’s first and last name and salary, the following is used: LNAME, FNAME,SALARY(EMPLOYEE) 10
  6. Unary Relational Operations: PROJECT ▪ PROJECT Operation Properties • The number of tuples in the result of projection (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 • ( (R) ) = (R) as long as contains the attributes in →If does not contain the attributes in ?? 12
  7. Relational Algebra Expressions ▪ We may want to apply several relational algebra operations one after the other • Either we can write the operations as a single relational algebra expression by nesting the operations, or • We can apply one operation at a time and create intermediate result relations. ▪ In the latter case, we must give names to the relations that hold the intermediate results. 14
  8. 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) 16
  9. Outline ▪ Relational Algebra • Unary Relational Operations • Relational Algebra Operations from Set Theory • Binary Relational Operations • Additional Relational Operations ▪ Brief Introduction to Relational Calculus ▪ Reading: • [1]: Chapter 6 18
  10. Example of the result of a UNION operation 20
  11. Relational Algebra Operations from Set Theory: INTERSECTION ▪ INTERSECTION is denoted by  ▪ The result of the operation R  S, is a relation that includes all tuples that are in both R and S • The attribute names in the result will be the same as the attribute names in R ▪ The two operand relations R and S must be “type compatible” 22
  12. Example to illustrate the result of UNION, INTERSECT, and DIFFERENCE 24
  13. Relational Algebra Operations from Set Theory: CARTESIAN PRODUCT ▪ CARTESIAN (or CROSS) PRODUCT Operation • 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. • 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
  14. Binary Relational Operations: JOIN ▪ 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 28
  15. The following query results refer to this database state 30
  16. 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 nR tuples, and S has nS tuples, then the join result will generally have less than nR * nS tuples. • Only related tuples (based on the join condition) will appear in the result 32
  17. Binary Relational Operations: EQUIJOIN ▪ 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 34
  18. Example of NATURAL JOIN operation 36
  19. Binary Relational Operations: DIVISION ▪ DIVISION Operation • The division operation is applied to two relations R(Z)S(X), where Z = X  Y (Y is 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 tR appear in R with tR [Y] = t, and with tR [X] = ts for every tuple ts in S, i.e., 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 38
  20. Recap of Relational Algebra Operations 40
  21. 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 • Common functions applied to collections of numeric values include SUM, AVERAGE, MAXIMUM, and MINIMUM. The COUNT function is used for counting tuples or values 42
  22. Additional Relational Operations ▪ Use of the Functional operator ℱ • ℱ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 • DNO ℱCOUNT SSN, AVERAGE Salary (Employee) groups employees by DNO (department number) and computes the count of employees and average salary per department • Note: count just counts the number of rows, without removing duplicates 44
  23. Additional Relational Operations (cont.) ▪ 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. • A set of operations, called 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. ▪ Outer Union operations: homework !! 46
  24. The following query results refer to this database state 48
  25. Exercise ▪ Using relational algebra: 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) RESULT  FNAME, LNAME, ADDRESS (RESEARCH_EMPS) 50
  26. Brief Introduction to Relational Calculus ▪ A relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus) ▪ In a calculus expression, there is no order of operations to specify how to retrieve the query result—a calculus expression specifies only what information the result should contain. This is the main distinguishing feature between relational algebra and relational calculus ▪ Relational calculus is considered to be a nonprocedural language. This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; hence relational algebra can be considered as a procedural way of stating a query 52
  27. Brief Introduction to Relational Calculus ▪ Two special symbols called quantifiers can appear in formulas; these are the universal quantifier () and the existential quantifier () ▪ Informally, a tuple variable t is bound if it is quantified, meaning that it appears in an ( t) or ( t) clause; otherwise, it is free 54
  28. Brief Introduction to Relational Calculus ▪ Example 2: find the names of employees who work on all the projects controlled by department number 5 {e.LNAME, e.FNAME | EMPLOYEE(e) and (( x) (not(PROJECT(x)) or not(x.DNUM=5) OR (( w)(WORKS_ON(w) and w.ESSN=e.SSN and x.PNUMBER=w.PNO))))} ▪ Details: [1] Chapter 6 56
  29. Brief Introduction to Relational Calculus ▪ Example: Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. {uv | ( q) ( r) ( s) ( t) ( w) ( x) ( y) ( z) (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)} 58
  30. Q&A 60