Download presentation
Presentation is loading. Please wait.
1
cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1
2
cs3431 Relational Languages Query Language : 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.
3
cs3431 Basics Relational Algebra is defined on bags --- versions for relations (sets) exist too. Bag or multi-set : allow duplicate values; but order is not significant.
4
cs3431 Basics Query 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
5
cs3431 Relational Algebra Basics Relational algebra includes : set operators, and other operators specific to relational model.
6
cs3431 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 Example : Union compatible or not? Student (sNumber, sName) Course (cNumber, cName)
7
cs3431 Union: Consider two bags R 1 and R 2 that are union- compatible. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 12 12 34 34 56 R 1 R 2 Suppose a tuple t appears in R1 m times, and in R2 n times. Then in the union, t appears m + n times.
8
cs3431 Intersection: ∩ Consider two bags R 1 and R 2 that are union-compatible. Suppose tuple t appears in R 1 m times, and in R 2 n times. Then in intersection, t appears min (m, n) times. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 34 R 1 ∩ R 2
9
cs3431 Difference: - Consider two bags R 1 and R 2 that are union-compatible. Suppose tuple t appears in R 1 m times & in R 2 n times. Then in R 1 – R 2, t appears max (0, m - n) times. AB 12 34 12 R1R1 AB 12 34 56 R2R2 AB 12 R 1 – R 2
10
cs3431 Idempotent property Idempotent property : Operation applied twice gives same result as when applied once Example : Filter-BLUE ( Filter-BLUE ( images ))
11
cs3431 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
12
cs3431 Bag vs Set Semantics R 1 R 2 = R 1 – (R 1 – R 2 ). For sets ? For bags ? Yes. True for both.
13
cs3431 Cross Product (Cartesian Product): X Consider two bags R 1 and R 2. Suppose a tuple t 1 appears in R 1 m times, and a tuple t 2 appears in R 2 n times. Then in R 1 X R 2, t 1 t 2 appears m*n times. AB 12 12 R1R1 BC 23 45 45 R2R2 AR 1.BR 2.BC 1223 1223 1245 1245 1245 1245 R 1 X R 2
14
cs3431 Basic Relational Operations Select, Project
15
cs3431 Basic Relational Operations Select: σ C (R): selects subset of tuples of R that satisfies selection condition C. ABC 125 346 127 127 R σ (C ≥ 6) (R) ABC 346 127 127
16
cs3431 Select Select is commutative: σ 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)
17
cs3431 Project: π A1, A2, …, An (R) π A1, A2, …, An (R), with A1, A2, …, An attributes A R returns tuples in R, but only columns A1, A2, …, An. ABC 125 346 127 128 R π A, B (R) AB 12 34 12 12
18
cs3431 Project: Bag vs Set Semantics For bags, cardinality of R = cardinality of π A1, A2, …, An (R). For sets, cardinality of R ≥ cardinality of π A1,A2, …, An (R). For sets and bags Is project commutative ? π A π B ( Relation ) NO ! Is project idempotent ? π A π A ( Relation ) YES !
19
cs3431 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 ))
20
cs3431 Summary So Far Key Property: 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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.