Database Systems 主講人 : 陳建源 日期 :99/11/30 研究室 : 法 401 Chapter 6 The Relational Algebra.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Advertisements

The Relational Calculus
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
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.
The Relational Algebra and Calculus
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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 and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
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 )
The Relational Algebra and Calculus. Slide 6- 2 Chapter Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
CSE202 Database Management Systems
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
The Relational Algebra and Calculus
Introduction to Database Systems
Lecture 3 Relational Algebra and SQL
The Relational Algebra and The Relational Calculus
Presentation transcript:

Database Systems 主講人 : 陳建源 日期 :99/11/30 研究室 : 法 Chapter 6 The Relational Algebra

1. Relational Algebra Overview 2. Unary Relational Operations 3. Set Operations 4. Binary Relational Operations 5. Complete Set of Relational Algebra 6. Additional Relational Operations 7. Examples Queries 8. Relational calculus 9. Summary Outline

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

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) 1. Relational Algebra Overview

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 2. Unary Relational Operations

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

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 2. Unary Relational Operations

 FNAME,LNAME,SALARY (EMPLOYEE)  SEX,SALARY (EMPLOYEE) 2. Unary Relational Operations

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 2. Unary Relational Operations

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)) 2. Unary Relational Operations

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) 2. Unary Relational 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 3. Set Operations

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) 3. Set Operations

The Relational Algebra (cont.) 

3. Set Operations

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 3. Set Operations

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

3. Set Operations 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)

The Relational Algebra (cont.)

3. Set 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 {=,, ,  } 4. Binary Relational Operations

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) 4. Binary Relational Operations

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 Binary Relational Operations

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) 4. Binary Relational Operations

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) 4. Binary Relational Operations

Note: In the original definition of NATURAL JOIN, the join attributes were required to have the same names in both relations 4. Binary Relational Operations

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) 4. Binary Relational Operations

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 4. Binary Relational Operations

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) 4. Binary Relational Operations

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) 4. Binary Relational Operations

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 4. Binary Relational Operations

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 4. Binary Relational Operations

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) 4. Binary Relational Operations

TRSTRS

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 4. Binary Relational Operations

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 5. Complete Set of Relational Algebra

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 5. Complete Set of Relational Algebra

 Query tree  Represents the input relations of query as leaf nodes of the tree  Represents the relational algebra operations as internal nodes

5. Complete Set of Relational Algebra ((( (PROJECT)) (DEPARTMENT)) (EMPLOYEE))

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 6. Additional Relational Operations

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 6. Additional Relational Operations

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 6. Additional Relational Operations

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) 6. Additional Relational Operations

The resulting attributes nam are in the form _ 6. Additional Relational Operations

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 6. Additional Relational Operations

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 6. Additional Relational Operations

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) 6. Additional Relational Operations

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 6. Additional Relational Operations

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) 6. Additional Relational Operations

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) 7. Example Queries

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) 7. Example Queries

8. Relational Calculus  Declarative expression  Specify a retrieval request nonprocedural language  Any retrieval that can be specified in basic relational algebra  Can also be specified in relational calculus

8. Relational Calculus  Tuple variables  Ranges over a particular database relation  Satisfy COND(t):  Specify:  Range relation R of t  Select particular combinations of tuples  Set of attributes to be retrieved (requested attributes)

8. Relational Calculus  General expression of tuple relational calculus is of the form:  Truth value of an atom  Evaluates to either TRUE or FALSE for a specific combination of tuples  Formula (Boolean condition)  Made up of one or more atoms connected via logical operators AND, OR, and NOT

8. Relational Calculus  Universal quantifier ( ∀ )  Existential quantifier ( ∃ )  Define a tuple variable in a formula as free or bound

8. Relational Calculus

 Transform one type of quantifier into other with negation (preceded by NOT)  AND and OR replace one another  Negated formula becomes unnegated  Unnegated formula becomes negated

8. Relational Calculus

 Guaranteed to yield a finite number of tuples as its result  Otherwise expression is called unsafe  Expression is safe  If all values in its result are from the domain of the expression

8. Relational Calculus  Differs from tuple calculus in type of variables used in formulas  Variables range over single values from domains of attributes  Formula is made up of atoms  Evaluate to either TRUE or FALSE for a specific set of values Called the truth values of the atoms

8. Relational Calculus  QBE language  Based on domain relational calculus

9. 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  Based predicate calculus