The Relational Algebra and Calculus

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)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
The Relational Algebra
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Slide Chapter 6 The Relational Algebra and Calculus.
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.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
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.
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
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
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.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1 CIS 611.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
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.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
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.
Announcements Today –Finish RDM (Chapter 5), begin relational algebra Reading –Sections Program 2 –Due Friday Exam –Tuesday Oct 16, in class –Closed.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
The Relational Algebra and The Relational Calculus
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
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.
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
The Relational Algebra and The Relational Calculus
Database Systems Chapter 6
The Relational Algebra
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
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
The Relational Algebra and Calculus
11/22/2018.
The Relational Algebra and Relational Calculus
12/5/2018.
The Relational Algebra
12/31/2018.
The Relational Algebra and The Relational Calculus
The Relational Algebra and The Relational Calculus
The Relational Algebra and The Relational Calculus
Presentation transcript:

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

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)

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

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)

The COMPANY Database

The following query results refer to this database state

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)

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

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)

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

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>

Examples of applying SELECT and PROJECT operations

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)

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

Dno ( SSN = 123456789 (EMPLOYEE)) results in {(5)}. Important Note The Select and Project operations produce relations. Dno ( SSN = 123456789 (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.

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)

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

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)

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

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

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)

The COMPANY Database

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

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

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”

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)

Example of applying CARTESIAN PRODUCT

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.

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.

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)

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

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.

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.

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)

Results of Natural JOINs

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

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)

The COMPANY Database

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

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

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

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