Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra : #I

Similar presentations


Presentation on theme: "Relational Algebra : #I"— Presentation transcript:

1 Relational Algebra : #I
Based on Chapter 2.4 & 5.1 cs3431

2 Relational Languages Query Language : Languages
Define data retrieval operations for relational model Express easy access to large data sets in high-level language, not complex application programs Languages Relational Algebra : procedural semantics based on set or bag theory Relational Calculus : logic-based language of denoting what is to be retrieved (but not how) SQL: syntactic sugar for relational calculus. cs3431

3 Basics Query Algebra : Example Query : Closure of Relational Algebra :
nested expression of algebra operators that accept as input relations and outputs a relation Example Query : SELECT [gpa > 3.0] ( UNION (Ugrads,Grads) ) Closure of Relational Algebra : operators work on relations and returns a relation cs3431

4 Set Operators Union, Intersection, Difference
Defined only for union compatible relations. Relations are union compatible if they have same sets of attributes and the same types (domains) of attributes Student (sNumber, sName) Course (cNumber, cName) cs3431

5 Union:  Consider two bags R1 and R2 that are union-compatible.
R1  R2 R1 R2 A B 1 2 3 4 5 6 A B 1 2 3 4 A B 1 2 3 4 5 6 Suppose a tuple t appears in R1 m times, and in R2 n times. Then in the union, t appears m + n times. cs3431

6 Intersection: ∩ Consider two bags R1 and R2 that are union-compatible.
R1 ∩ R2 A B 1 2 3 4 A B 1 2 3 4 5 6 A B 1 2 3 4 Suppose tuple t appears in R1 m times, and in R2 n times. Then in intersection, t appears min (m, n) times. cs3431

7 Difference: - Suppose tuple t appears in R1 m times & in R2 n times.
Then in R1 – R2, t appears max (0, m - n) times. R1 R2 R1 – R2 A B 1 2 3 4 A B 1 2 3 4 5 6 A B 1 2 cs3431

8 Idempotent property Idempotent property : Example :
Operation applied twice gives same result as when applied once Example : Filter-BLUE ( Filter-BLUE ( images )) cs3431

9 Bag vs Set Semantics Union is idempotent for sets:
(R1  R2)  R2 = R1  R2 What about union for bags ? Union is not idempotent for bags. What about intersection ? Intersection is idempotent for sets Intersection is idempotent for bags What about difference ? Difference is idempotent for sets Difference is not idempotent for bags cs3431

10 Cross Product (Cartesian Product): X
Consider two bags R1 and R2. Suppose a tuple t1 appears in R1 m times, and a tuple t2 appears in R2 n times. Then in R1 X R2, t1t2 appears m*n times. R1 X R2 R1 R2 A R1.B R2.B C 1 2 3 4 5 A B 1 2 B C 2 3 4 5 cs3431

11 Basic Relational Operations
Select, Project cs3431

12 Basic Relational Operations
Select: σC (R): selects subset of tuples of R that satisfies selection condition C. σ(C ≥ 6) (R) R A B C 1 2 5 3 4 6 7 A B C 3 4 6 1 2 7 cs3431

13 Select Select is commutative: Select is idempotent:
σC2 (σC1 (R)) = σC1 (σC2 (R)) Select is idempotent: σC (σC (R)) = σC (R) We can combine multiple select conditions into one condition. σC1 (σC2 (… σCn (R)…)) = σC1 AND C2 AND … Cn (R) cs3431

14 Project: πA1, A2, …, An (R) πA, B (R)
πA1, A2, …, An (R), with A1, A2, …, An  attributes AR returns tuples in R, but only columns A1, A2, …, An. πA, B (R) R A B C 1 2 5 3 4 6 7 8 A B 1 2 3 4 cs3431

15 Equivalences with Select/Project
σ [sal>100k) (π sal ( Employee )) = πsal ( σ [sal>100k) ( Employee )) σ [sal>100k) (πsal,name ( Employee )) = πsal,name ( σ [sal>100k) (πsal,name ( Employee )) σ [sal>100k) (π name ( Employee )) = πname ( σ [sal>100k) ( Employee )) cs3431

16 Summary So Far Key Property: Basic Operators: Logical Rewrite Rules:
Closure of Relational Algebra Basic Operators: Set Operators: Union, Intersection, Difference Cartesian Product (simple form of “Join”) Select, Project Logical Rewrite Rules: Idempotent, commutative, associative. cs3431


Download ppt "Relational Algebra : #I"

Similar presentations


Ads by Google