Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Chapter 6 Relational Algebra and The Relational Calculus

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Chapter 6 Relational Algebra and the Relational Calculus

Chapter Slides (ppt) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Figures (ppt) 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Reference: Ramez Elmasri and Shamkant Navathe, Fundamentals of Database Systems, 5th edition, Addison-Wesley, 2007. See www.aw-bc.com/elmasri Contents: 1. 2. 3. 4. 5. 6. 7. Unary relational operations: SELECT and PROJECT Relational algebra operations from set theory Binary relational operations: JOIN and DIVISION Additional relational operations Examples of queues in relational algebra Tuple relational calculus Domain relational calculus Summary Review questions Exercises Selected bibliography

Importance:
y y y

Foundation for operations on relational models Basis for implementing and optimizing queries in RDBMS Understand how implementation is supposed to work

Chapter 6 is intended to be formal/theoretical SQL is derived from relational algebra and calculus NOT identical Class (or abstract data type) is
y

Attributes

Operations

Chapter 5 Relational Model was about attributes/objects (relations/tables, attributes, domains, constraints) Chapter 6 is about operations that apply to those Chapters 8 & 9 (SQL) is about an implementation

See Table 6.1, p. 191, for summary Exercise: Give an example of each

6.1 Unary operations


Operate on one relation (think, table)

6.1.1 SELECT
NOT identical to SQL SELECT statement Select a subset of tuples from a relation that satisfy a selection condition Notation: <sigma><selection condition>(R) R - Relational algebra expression whose result is a relation Examples:
y y

Relation Results of another SELECT operation

<selection condition> - Number of clauses: <attribute name> <comparison op> <constant value> or <attribute name> <comparison op> <attribute name> <attribute name> - Attribute from R <comparison op> - One of {=, <, <=, >, >=, !=}

<constant value> - Constant value from the attribute domain Selection condition clauses connected by AND, OR, NOT Action of SELECT:
y y y

Apply <selection condition> independently to each tuple t in R substituting attributes If TRUE, tuple t is selected No provision for choosing attributes

SELECT is unary - applies to one relation Operation is applied to each tuple individually

6.1.2 PROJECT
SELECT selects some rows from a relation while discarding others PROJECT selects some columns while discarding others Notation: <pi><attribute list>(R) R - Relational algebra expression whose result is a relation <attribute list> - Desired attributes from the relation R Action of PROJECT:
y y y y

Relation containing attributes from <attribute list> From each tuple t in R In the order listed in <attribute list> Removes duplicate tuples

C.f., mathematical projection

6.1.3 Sequences of operations


<pi>(<sigma>(R)) or T1 <- <sigma>(R)

<pi>(T1)

6.1.3 RENAME
Notation: <rho>S(B1, B2, ..., Bn)(R) - Renames relation R to S and attributes Ai to Bi <rho>S(R) - Renames relation R to S <rho>(B1, B2, ..., Bn)(R) - Renames attributes Ai to Bi

6.2 Operations from set theory


Relations R and S must be union compatible:
y y

Same number of attributes Each corresponding pair of attributes has same domain

See Figure 6.4

6.2.1 UNION R <cup> S


Includes all tuples that are in either R or S Duplicate tuples are removed

6.2.1 INTERSECTION R <cap> S


Includes all tuples that are in both R and S

6.2.1 SET DIFFERENCE (MINUS) R - S


Includes all tuples that are R but not in S

UNION and INTERSECTION are commutative and associative SET DIFFERENCE is not commutative

6.2.2 CARTESIAN PRODUCT (CROSS PRODUCT) R x S


Action:

y y y y

New relation Combine every tuple from R with every tuple from S Number of tuples = Number of tuples in R x number of tuples in S (Attributes) = ( Attributes of R, Attributes of S )

See Figure 6.5 Often preceded by SELECT and/or PROJECT

6.3 Binary relational operations


6.3.1 JOIN
Combine related tuples from two relations into a single relation Denoted by <tie> Notation Q <-- R <tie><join condition> S Relations R(A1, A2, ..., An) and S(B1, B2, ..., Bm) Result Q(A1, A2, ..., An, B1, B2, ..., Bm) is one tuple for each combination of tuples, one from R and one from S, whenever the combination satisfies <join condition> <join condition> - <condition> AND <condition> AND ... AND <condition> where <condition> is Ai <THETA> Bj, attributes of R and S, resp., with same domains Often, one attribute is a primary key and the other is a foreign key corresponding to it JOIN is similar to CROSS PRODUCT - combinations of tuples from R and S JOIN is different to CROSS PRODUCT - combinations satisfies <join condition> JOIN is equivalent to SELECT ( CROSS PRODUCT )

6.3.2 EQUIJOIN
<join condition> involves only = Pairs of attributes are equal

6.3.2 NATURAL JOIN

<join condition> involves only = Result has only one of the equal pairs

6.3.3 Complete set of relational algebra operators


Theorem: {<sigma>, <pi>, <cup>, -, x} is a complete set of relational algebra operations. SELECT, PROJECT, UNION, MINUS, and CROSS PRODUCT can generate ANY other relational algebra operation R <operation> S INTERTSECTION: R <cap>S = (R <cup> S) - ((R - S) <cup> (S - R)) JOIN: R <tie><condition> S = <sigma><condition>(R x S)

6.3.5 Notation for query trees


Example: Figure 6.9 Graphical notation to represent composition of relational operations Similar to computational graphs Tree data structure corresponding to relational algebra expression Leaf nodes are input relations to the query Operations as internal nodes Root is result Useful to consider optimization/rearrangements 6.4 Other operations 6.5 Examples

Go to Chapter 7 Relational Database Design by ER- and EER-Relational Mapping

Chapter

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Slides (ppt)

1 2 3 4 5 6 7 8 9 10 11 12 13 14

Figures (ppt) 1 2 3 4 5 6 7 8 9 10 11 12 13 14

You might also like