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

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Lecture 1 Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
CM036: Advanced Database Lecture 3 Relational Algebra and SQL.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
The Relational Data Model (Based on Chapter 5)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 4 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
The Relational Data Model 1.Relational Model Concepts 2.Characteristics of Relations 3.Relational Integrity Constraints 3.1Key Constraints 3.2Entity Integrity.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
The Relational Algebra and Calculus
October 9, Announcements Exam Tuesday in class –Closed book Program 2 due Friday –Turn in using D2L dropbox –Only turn in the 4 files specified.
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Lecture 1: Relational Data Models
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 11 A First Course in Database Systems.
1 CSBP430 – Database Systems Chapter 7 - The Relational Data Model Elarbi Badidi College of Information Technology United Arab Emirates University
7-1 Chapter 7 The Relational Data Model, Relational Constraints, and the Relational Algebra.
CG096 Advanced Database Technologies Lecture 1 Relational Algebra and SQL.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Database Systems 主講人 : 陳建源 日期 :99/11/30 研究室 : 法 Chapter 6 The Relational Algebra.
The Relational Algebra and Calculus
Database Systems Chapter 6
The Relational Algebra
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
The Relational Algebra and Calculus
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
6/22/2018.
The Relational Algebra and Relational Calculus
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
The Relational Algebra and Calculus
Session - 6 Sequence - 2 SQL: The Structured Query Language:
The Relational Algebra
Introduction to Database Systems
Lecture 3 Relational Algebra and SQL
The Relational Algebra and The Relational Calculus
Presentation transcript:

DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra

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)

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

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)

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

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

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

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

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))

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)

DatabaseIM ISU11 Unary Relational Operations (cont.)

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

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

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)

DatabaseIM ISU15 The Relational Algebra (cont.)

DatabaseIM ISU16 Set Operations (cont.)

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

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

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)

DatabaseIM ISU20 The Relational Algebra (cont.)

DatabaseIM ISU21 Set Operations (cont.)

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 {=,, ,  }

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)

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

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)

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)

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

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)

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

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)

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)

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

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

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)

DatabaseIM ISU35 Binary Operations (cont.)

DatabaseIM ISU36 Binary Operations (cont.)

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

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

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

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

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

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

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)

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

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

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

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)

DatabaseIM ISU48 Additional Relational Operations (cont.)

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

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)

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)

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)

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