Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra and Relational Calculus

Similar presentations


Presentation on theme: "The Relational Algebra and Relational Calculus"— Presentation transcript:

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

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

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

4 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

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

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

7 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

8 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?

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

10 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

11 π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

12 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

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

14 ρ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,

15

16 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

17 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 ‘ ’ appears only once in the result.

18

19 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

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

21

22 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

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

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

25 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

26 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:

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

28 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

29 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: {=, <, ≤, >, ≥, ≠}

30 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

31 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

32 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

33 Operations of Relational Algebra

34 Operations of Relational Algebra (cont’d.)

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

36

37 Examples of Queries in Relational Algebra

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

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

40 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

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

42 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

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

44 Sample Queries in Tuple Relational Calculus

45 Notation for Query Graphs

46 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

47 Using the Universal Quantifier in Queries

48 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

49 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

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

51 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


Download ppt "The Relational Algebra and Relational Calculus"

Similar presentations


Ads by Google