The Relational Algebra and Relational 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)
The Relational Calculus
The Relational Algebra
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.
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.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
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.
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.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
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.
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 © 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.
Database Systems 主講人 : 陳建源 日期 :99/11/30 研究室 : 法 Chapter 6 The Relational Algebra.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
Chapter 4 The Relational Algebra and Calculus
CSE202 Database Management Systems
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
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
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
Presentation transcript:

The Relational Algebra and Relational Calculus Chapter 6 The Relational Algebra and Relational Calculus

Chapter 6 Outline Unary Relational Operations: SELECT and PROJECT Relational Algebra Operations from Set Theory Binary Relational Operations: JOIN and DIVISION Additional Relational Operations

Chapter 6 Outline (cont’d.) Examples of Queries in Relational Algebra The Tuple Relational Calculus The Domain Relational Calculus

The Relational Algebra and Relational Calculus Basic set of operations for the relational model Relational algebra expression Sequence of relational algebra operations Relational calculus Higher-level declarative language for specifying relational queries

Unary Relational Operations: SELECT and PROJECT The SELECT Operation Subset of the tuples from a relation that satisfies a selection condition: Boolean expression contains clauses of the form <attribute name> <comparison op> <constant value> or <attribute name> <comparison op> <attribute name>

The SELECT operation can also be visualized as a horizontal partition of the relation into two sets of tuples—those tuples that satisfy the condition and are selected, and those tuples that do not satisfy the condition and are discarded. For example, to select the EMPLOYEE tuples whose department is 4, or those whose salary is greater than $30,000, we can individually specify each of these two conditions with a SELECT operation as follows: σ Dno=4(EMPLOYEE) σSalary>30000(EMPLOYEE)

Unary Relational Operations: SELECT and PROJECT (cont’d.) Example: <selection condition> applied independently to each individual tuple t in R If condition evaluates to TRUE, tuple selected Boolean conditions AND, OR, and NOT Unary Applied to a single relation

In SQL, the SELECT condition is typically specified in the WHERE clause of a query. For example, the following operation: σDno=4 AND Salary>25000 (EMPLOYEE) would correspond to the following SQL : SELECT * FROM EMPLOYEE WHERE Dno=4 AND Salary>25000; The degree of the relation resulting from a SELECT operation?

Unary Relational Operations: SELECT and PROJECT (cont’d.) Selectivity Fraction of tuples selected by a selection condition SELECT operation commutative (can be applied in any order) σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R)) Cascade SELECT operations into a single operation with AND condition σ<cond1>(σ<cond2>(...(σ<condn>(R)) ...)) = σ<cond1> AND<cond2> AND...AND <condn>(R)

The PROJECT Operation Selects columns from table and discards the other columns: Degree Number of attributes in <attribute list> Duplicate elimination Result of PROJECT operation is a set of distinct tuples

πLname, Fname, Salary(EMPLOYEE) Therefore, the result of the PROJECT operation can be visualized as a vertical partition of the relation into two relations: one has the needed columns (attributes) and contains the result of the operation, and the other contains the discarded columns. For example, to list each employee’s first and last name and salary, we can use the PROJECT operation as follows: πLname, Fname, Salary(EMPLOYEE) In SQL, the PROJECT attribute list is specified in the SELECT clause of a query. For example, the following operation: πLname, Fname, Salary(EMPLOYEE) would correspond to the following SQL query: SELECT DISTINCT Lname, Fname, Salary FROM EMPLOYEE

Sequences of Operations and the RENAME Operation In-line expression: Figure 6.2(a) shows the result of this in-line relational algebra expression. Sequence of operations: Rename attributes in intermediate results RENAME operation

It is sometimes simpler to break down a complex sequence of operations by specifying intermediate result relations than to write a single relational algebra expression. We can also use this technique to rename the attributes in the intermediate and result relations. This can be useful in connection with more complex operations such as UNION and JOIN, as we shall see. To rename the attributes in a relation, we simply list the new attribute names in parentheses, as in the following example: TEMP ← σDno=5(EMPLOYEE) R(First_name, Last_name, Salary) ← πFname, Lname, Salary(TEMP) These two operations are illustrated in Figure 6.2(b).

ρS(B1, B2, ..., Bn)(R) or ρS(R) or ρ(B1, B2, ..., Bn)(R) For a PROJECT operation with no renaming, the resulting relation has the same attribute names as those in the projection list and in the same order in which they appear in the list. We can also define a formal RENAME operation—which can rename either the relation name or the attribute names, or both—as a unary operator. The general RENAME operation when applied to a relation R of degree n is denoted by any of the following three forms: ρS(B1, B2, ..., Bn)(R) or ρS(R) or ρ(B1, B2, ..., Bn)(R) In SQL, a single query typically represents a complex relational algebra expression. Renaming in SQL is accomplished by aliasing using AS, as in the following example: SELECT E.Fname AS First_name, E.Lname AS Last_name, E.Salary AS Salary FROM EMPLOYEE AS R WHERE E.Dno=5,

Relational Algebra Operations from Set Theory UNION, INTERSECTION, and MINUS Merge the elements of two sets in various ways Binary operations Relations must have the same type of tuples UNION R ∪ S Includes all tuples that are either in R or in S or in both R and S Duplicate tuples eliminated

we can use the UNION operation as follows: DEP5_EMPS ← σDno=5(EMPLOYEE) RESULT1 ← πSsn(DEP5_EMPS) RESULT2(Ssn) ← πSuper_ssn(DEP5_EMPS) RESULT ← RESULT1 ∪ RESULT2 The relation RESULT1 has the Ssn of all employees who work in department 5, whereas RESULT2 has the Ssn of all employees who directly supervise an employee who works in department 5. The UNION operation produces the tuples that are in either RESULT1 or RESULT2 or both (see Figure 6.3), while eliminating any duplicates. Thus, the Ssn value ‘333445555’ appears only once in the result.

Relational Algebra Operations from Set Theory (cont’d.) INTERSECTION R ∩ S Includes all tuples that are in both R and S SET DIFFERENCE (or MINUS) R – S Includes all tuples that are in R but not in S

We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE on two union-compatible relations R and S as follows: ■ UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. ■ INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S. ■ SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that includes all tuples that are in R but not in S.

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation CROSS PRODUCT or CROSS JOIN Denoted by × Binary set operation Relations do not have to be union compatible Useful when followed by a selection that matches values of attributes

In general, the result of R(A1, A2,. , An) × S(B1, B2, In general, the result of R(A1, A2, ..., An) × S(B1, B2, ..., Bm) is a relation Q with degree n + m attributes Q(A1, A2, ..., An, B1, B2, ..., Bm), in that order. The resulting relation Q 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 × S will have nR * nS tuples.

In general, the CARTESIAN PRODUCT operation applied by itself is generally meaningless. It is mostly useful when followed by a selection that matches values of attributes coming from the component relations. For example, suppose that we want to retrieve a list of names of each female employee’s dependents. We can do this as follows: 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)

every tuple from EMPNAMES is combined with every tuple from DEPENDENT, giving a result that is not very meaningful (every dependent is combined with every female employee) EMP_DEPENDENTS We want to combine a female employee tuple only with her particular dependents—namely, the DEPENDENT tuples whose Essn value match the Ssn value of the EMPLOYEE tuple. The ACTUAL_DEPENDENTS

Binary Relational Operations: JOIN and DIVISION The JOIN Operation Denoted by Combine related tuples from two relations into single “longer” tuples General join condition of the form <condition> AND <condition> AND...AND <condition> Example:

To illustrate JOIN, 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 Mgr_ssn value in the department tuple.

The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, The result of the JOIN is a relation Q with n + m attributes Q(A1, A2, ..., An, B1, B2, ... , Bm) in that order; Q has one tuple for each combination of tuples—one from R and one from S whenever the combination satisfies the join condition. This is the main difference between CARTESIAN PRODUCT and JOIN. In JOIN, only combinations of tuples satisfying the join condition appear in the result, whereas in the CARTESIAN PRODUCT all combinations of tuples are included in the result. EMP_DEPENDENTS ← EMPNAMES × DEPENDENT ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS) ACTUAL_DEPENDENTS ← EMPNAMES Ssn=EssnDEPENDENT

Binary Relational Operations: JOIN and DIVISION (cont’d.) THETA JOIN Each <condition> of the form Ai θ Bj Ai is an attribute of R Bj is an attribute of S Ai and Bj have the same domain θ (theta) is one of the comparison operators: {=, <, ≤, >, ≥, ≠}

Variations of JOIN: The EQUIJOIN and NATURAL JOIN Only = comparison operator used Always have one or more pairs of attributes that have identical values in every tuple NATURAL JOIN Denoted by * Removes second (superfluous) attribute in an EQUIJOIN condition

A Complete Set of Relational Algebra Operations Set of relational algebra operations {σ, π, ∪, ρ, –, ×} is a complete set Any relational algebra operation can be expressed as a sequence of operations from this set

The DIVISION Operation Denoted by ÷ Example: retrieve the names of employees who work on all the projects that ‘John Smith’ works on Apply to relations R(Z) ÷ S(X) Attributes of R are a subset of the attributes of S

Operations of Relational Algebra

Operations of Relational Algebra (cont’d.)

Notation for Query Trees Represents the input relations of query as leaf nodes of the tree Represents the relational algebra operations as internal nodes

Examples of Queries in Relational Algebra

Examples of Queries in Relational Algebra (cont’d.)

Examples of Queries in Relational Algebra (cont’d.)

The Tuple 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

Tuple Variables and Range Relations 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)

Expressions and Formulas in Tuple 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

Existential and Universal Quantifiers Existential quantifier (∃) Define a tuple variable in a formula as free or bound

Sample Queries in Tuple Relational Calculus

Notation for Query Graphs

Transforming the Universal and Existential Quantifiers 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

Using the Universal Quantifier in Queries

Safe Expressions 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

The Domain 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

The Domain Relational Calculus (cont’d.) QBE language Based on domain relational calculus

Summary Formal languages for relational model of data: Relational algebra: operations, unary and binary operators Some queries cannot be stated with basic relational algebra operations But are important for practical use Relational calculus Based predicate calculus