Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.

Similar presentations


Presentation on theme: "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus."— Presentation transcript:

1 Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus

2 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The Relational Algebra and Relational Calculus  Relational algebra  Basic set of operations for the relational model  Relational algebra expression  Sequence of relational algebra operations  whose result will also be a relation that represents the result of a database query (or retrieval request).  Relational calculus  Higher-level declarative language for specifying relational queries  A relational calculus expression creates a new relation

3 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unary Relational Operations: SELECT and PROJECT  Unary  Applied to a single relation  The SELECT Operation  Subset of the tuples from a relation that satisfies a selection condition: the symbol σ (sigma) is used to denote the SELECT operator the selection condition is a Boolean expression (condition) specified on the attributes of relation R. Boolean expression contains clauses of the form or R is generally a relational algebra expression whose result is a relation— the simplest such expression is just the name of a database relation. The relation resulting from the SELECT operation has the same attributes as R.

4 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unary Relational Operations: SELECT and PROJECT (cont’d.)  Example:  All the comparison operators in the set {=,, ≥, ≠} can apply to attributes whose domains are ordered values, such as numeric or date domains.  applied independently to each individual tuple t in R  If condition evaluates to TRUE, tuple selected  Boolean conditions AND, OR, and NOT

5 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unary Relational Operations: SELECT and PROJECT (cont’d.)  Selectivity  Fraction of tuples selected by a selection condition  SELECT operation commutative  A sequence of SELECTs can be applied in any order.  Combine cascade SELECT operations into a single operation with AND condition

6 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The PROJECT Operation  Selects columns from table and discards the other columns:  π (pi) is the symbol used to represent the PROJECT operation,  is the desired sublist of attributes from the attributes of relation R.  R is, in general, a relational algebra expression  Degree  Number of attributes in  Duplicate elimination  Result of PROJECT operation is a set of distinct tuples

7 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Examples

8 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The RENAME Operation  Rename attributes in intermediate results  RENAME operation  where the symbol ρ (rho) is used to denote the RENAM E operator, S is the new relation name, and B1, B2,..., Bn are the new attribute names.

9 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 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 compatibility or type compatibility. means that the two relations have the same number of attributes and each corresponding pair of attributes has the same domain.  UNION  R ∪ S  Includes all tuples that are either in R or in S or in both R and S  Duplicate tuples eliminated

10 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 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  Both UN ION and INTERSECTION are commutative operations; that is, R ∪ S = S ∪ R and R ∩ S = S ∩ R  Both U N ION and INTERSECTION can be treated as n-ary operations applicable to any number of relations because both are also associative operations; that is, R ∪ (S ∪ T) = (R ∪ S) ∪ T and (R ∩ S) ∩ T = R ∩ (S ∩ T )  The MINUS operation is not commutative; that is, in general, R − S ≠ S − R

11 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Relational Algebra Operations from Set Theory (cont’d.)

12 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CARTESIAN PRODUCT (CROSS PRODUCT) Operation  CARTESIAN PRODUCT  CROSS PRODUCT or CROSS JOIN  Denoted by ×  Binary set operation  Relations do not have to be union compatible  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. If R has nR tuples (denoted as |R| = nR), and S has nS tuples, then R × S will have nR * nS tuples.  Useful when followed by a selection that matches values of attributes

13 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Binary Relational Operations: JOIN  The JOIN Operation  Denoted by  Combine related tuples from two relations into single “longer” tuples  The general form of a JOIN operation on two relations R(A1, A2,..., An) and S(B1, B2,..., Bm) is R S The result of the JOIN is a relation Q with n + m attributes Q(A1, A2,..., An, B1, B2,..., Bm) in that order;  Example:

14 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Binary Relational Operations: JOIN (cont’d.)  General join condition of the form AND AND...AND  THETA JOIN  Each of the form A i θ B j  A i is an attribute of R  B j is an attribute of S  A i and B j have the same domain  θ (theta) is one of the comparison operators: {=,, ≥, ≠} U ⋈ A<D V

15 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Variations of JOIN: The EQUIJOIN and NATURAL JOIN  EQUIJOIN  Only = comparison operator used  Always have one or more pairs of attributes that have identical values in every tuple  NATURAL JOIN  Denoted by * Q ← R *( ),( )S  Removes second (superfluous) attribute in an EQUIJOIN condition  The standard definition of NATURAL JOIN requires that the two join attributes (or each pair of join attributes) have the same name in both relations. If this is not the case, a renaming operation is applied first.

16 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Variations of JOIN: The EQUIJOIN and NATURAL JOIN  Join selectivity  Expected size of join result divided by the maximum size n R * n S  Inner joins  Combine data from two relations so that related information can be presented in a single table.  Type of match and combine operation  Defined formally as a combination of CARTESIAN PRODUCT and SELECTION

17 Copyright © 2011 Ramez Elmasri and Shamkant Navathe 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  For example, R ∩ S = ((R ∪ S) − (R − S)) − (S − R) R  S = R – (R – S) = S – (S – R) R ⋈ C S =  C (R  S) [C is a condition, ⋈ C is the theta-join] R ⋈ S =  L (  C (R  S)) [L is the list of attributes in the schema of R followed by those attributes in the schema of S that are not also in the schema of R.

18 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Semijoin  The semijoin is similar to the natural join and written as R ⋉ S where R and S are relations, but the output of the semijoin is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names. Employee Dept Employee ⋉ Dept NameEmpIdDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Production DeptNameManager SalesHarriet ProductionCharles NameEmpIdDeptName Sally2241Sales Harriet2202Production Note that if X is the set of all attributes of R, then R ⋉ S =  X (R ⋈ S)

19 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Antijoin  The antijoin, written as R ▷ S, where R and S are relations, is in a sense opposite of semijoin; the result of an antijoin is only those tuples in R for which there is NOT a tuple in S that is equal on their common attribute names. Employee Dept Employee ▷ Dept NameEmpIdDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Production DeptNameManager SalesHarriet ProductionCharles NameEmpIdDeptName Harry3415Finance George3401Finance Note: if X is the set of all attributes of R, then R ▷ S = R – R ⋉ S = R –  X (R ⋈ S)

20 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The DIVISION Operation  Denoted by ÷  In general, the DIVISION operation is applied to two relations R(Z) ÷ S(X), where the attributes of S are a subset of the attributes of R; that is, X ⊆ Z.  Let Y be the set of attributes of R that are not attributes of S; that is, Y= Z – X (and hence Z = X ∪ Y).  The result of DIVISION is a relation T(Y) that includes a tuple t if tuples t R appear in R with t R [Y] = t, and with t R [X] = t S for every tuple t S in S. This means that, for a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combination with every tuple in S.

21 Copyright © 2011 Ramez Elmasri and Shamkant Navathe The DIVISION Operation  The DIVISION operation can be expressed as a sequence of π, ×, and – operations as follows:  T1 ← π Y (R)  T2 ← π Y ((S × T1) – R)  T ← T1 – T2

22 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Notation for Query Trees  Query tree  A tree data structure that corresponds to a relational algebra expression. Represents the input relations of query as leaf nodes of the tree Represents the relational algebra operations as internal nodes  An execution of the query tree consists of executing an internal node operation whenever its operands (represented by its child nodes) are available, and then replacing that internal node by the relation that results from executing the operation. The execution terminates when the root node is executed and produces the result relation for the query.

23 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

24 Combining Operations to Form Queries Relational algebra, like all algebras, allows us to form expressions of arbitrary complexity by applying operations to the result of other operations. An example to illustrate the point; consider the Movie relation: Query: “What are the titles and years of movies made by Fox that are at least 100 minutes long?” – see the adjoining figure how to this – similar to expression trees in compiler design. Another way: Yet another way:

25 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Alternative Ways for Expressions An alternative is to invent names for the temporary relations that correspond to the interior nodes of the tree and write a sequence of assignments that create a value for each. The last query can be written as:

26 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Operations of Relational Algebra

27 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Operations of Relational Algebra (cont’d.)

28 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Relational Operations  Generalized projection  Allows functions of attributes to be included in the projection list

29 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Relational Operations  Aggregate functions and grouping  Common functions applied to collections of numeric values  Include SUM, AVERAGE, MAXIMUM, and MINIMUM  Group tuples by the value of some of their attributes  Apply aggregate function independently to each group  where is a list of attributes of the relation specified in R, and is a list of ( ) pairs.  The resulting relation has the grouping attributes plus one attribute for each element in the function list.

30 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

31 OUTER JOIN Operations  Outer joins  Keep all tuples in R, or all those in S, or all those in both relations regardless of whether or not they have matching tuples in the other relation  Types LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN

32 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Left Outer Join  The left outer join is written as R S, where R and S are relations. The result of the left outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition (loosely speaking) to tuples in R that have no matching tuples in S. Employee Dept NameEmpIdDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales Tim1123Executive DeptNameManager SalesHarriet ProductionCharles NameEmpIdDeptNameManager Harry3415FinanceNULL Sally2241SalesHarriet George3401FinanceNULL Harriet2202SalesHarriet Tim1123ExecutiveNULL Let Y be the set of attributes unique to R and let {(NULL, …, NULL)} be the singleton relation on attributes unique to S. Then we can express R S as follows using basic operators R S = R ⋈ S  ((R –  Y (R ⋈ S))  {(NULL, …, NULL)})

33 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Right outer join  The right outer join is written as R S, where R and S are relations. The result of the right outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R. Employee Dept Employee Dept NameEmpIdDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales Tim1123Executive DeptNameManager SalesHarriet ProductionCharles NameEmpIdDeptNameManager Sally2241SalesHarriet 2202SalesHarriet NULL ProductionCharles Let Y be the set of attributes unique to S and let {(NULL, …, NULL)} be the singleton relation on attributes unique to R. Then we can express R S as follows using basic operators R S = (R ⋈ S)  ({(NULL, …, NULL)}  (S –  Y (R ⋈ S)))

34 Copyright © 2011 Ramez Elmasri and Shamkant Navathe Outer join (Full outer join)  The full outer join is written as R S where R and S are relations. The result of the full outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R and tuples in R that have no matching tuples in S in their common attribute names.  Example: Employee Dept NameEmpIdDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales Tim1123Executive DeptNameManager SalesHarriet ProductionCharles NameEmpIdDeptNameManager Harry3415FinanceNULL Sally2241SalesHarriet George3401FinanceNULL Harriet2202SalesHarriet Tim1123ExecutiveNULL ProductionCharles

35 Copyright © 2011 Ramez Elmasri and Shamkant Navathe

36 Examples of Queries in Relational Algebra


Download ppt "Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus."

Similar presentations


Ads by Google