❖ Relational Algebra |
Relational algebra (RA) can be viewed as ...
❖ Relational Algebra (cont) |
Core relational algebra operations:
❖ Relational Algebra (cont) |
Select, project, join provide a powerful set of operations for building relations and extracting interesting data from them.
Adding set operations and renaming makes RA complete.
❖ Notation |
Standard treatments of relational algebra use Greek symbols.
We use the following notation (because it is easier to reproduce):
Standard Notation |
Our Notation |
|||
σexpr(Rel) | Sel[expr](Rel) | |||
πA,B,C(Rel) | Proj[A,B,C](Rel) | |||
Rel1 ⨝expr Rel2 | Rel1 Join[expr] Rel2 | |||
ρschemaRel | Rename[schema](Rel) |
For other operations (e.g. set operations) we adopt the standard notation.
Except when typing in a text file, where *
+
❖ Describing RA Operations |
We define the semantics of RA operations using
❖ Describing RA Operations (cont) |
All RA operators return a result of type relation.
For convenience, we can name a result and use it later.
E.g.
Temp = R op1 S op2 T
Res = Temp op3 Z
-- which is equivalent to
Res = (R op1 S op2 T) op3 Z
Each "intermediate result" has a well-defined schema.
❖ Rename |
Rename provides "schema mapping".
If expression E returns a relation R(A1, A2, ... An), then
gives a relation called S
The only thing that Rename changes is the schema.
❖ Rename (cont) |
Rename can be viewed as a "technical" apparatus of RA.
We can also use implicit rename/project in sequences of RA operations, e.g.
-- R(a,b,c), S(c,d) Res = Rename[Res(b,c,d)](Project[b,c](Sel[a>5](R)) Join S) -- vs Tmp1 = Select[a>5](R) Tmp2 = Project[b,c](Tmp1) Tmp3 = Rename[Tmp3(cc,d)](S) Tmp4 = Tmp2 Join[c=cc] Tmp3 Res = Rename[Res(b,c,d)](Tmp4) -- vs Tmp1(b,c) = Select[a>5](R) Tmp2(cc,d) = S Res(b,c,d) = Tmp1 Join[c=cc] Tmp2
In SQL, we achieve a similar effect by defining a set of views
❖ Selection |
Selection returns a subset of the tuples in a relation r(R) that satisfy a specified condition C.
C is a boolean expression on attributes in R.
Result size: |σC(r)| ≤ |r|
Result schema: same as the schema of r (i.e. R)
Algorithmic view:
result = {} for each tuple t in relation r if (C(t)) { result = result ∪ {t} }
❖ Selection (cont) |
Querying with relational algebra (selection) ...
Result = Sel[addr=The Rocks](Bars)
SNBeers = Sel[manf=Sierra Nevada](Beers) Result = Rename[beer](Proj[name](SNBeers))
❖ Projection |
Projection returns a set of tuples containing a subset of the attributes in the original relation.
X specifies a subset of the attributes of R.
Note that removing key attributes can produce duplicates.
In RA, duplicates are removed from the result set.
(In RDBMS's, duplicates are retained (i.e. they use bags, not sets))
Result size: |πX(r)| ≤ |r| Result schema: R'(X)
Algorithmic view:
result = {} for each tuple t in relation r result = result ∪ {t[X]}
❖ Projection (cont) |
Querying with relational algebra (projection)...
Result = Proj[name](Beers)
Result = Proj[name](Sel[addr=Newtown](Drinkers))
Result(brewer) = Proj[manf](Beers)
Produced: 6 Nov 2020