

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















\Core" Relational Algebra
A small set of set-based op erators that allow us to manipulate relations in limited but useful ways. The op erators are:
F But the relation schemas must b e the same.
Selection
where C is a condition involving the attributes of relation R 2.
Example
Relation Sells:
bar b eer price Jo e's Bud 2. Jo e's Miller 2. Sue's Bud 2. Sue's Co ors 3.
JoeMenu = bar =J oe^0 s (Sells)
bar b eer price Jo e's Bud 2. Jo e's Miller 2.
Pro duct
R = R 1 R 2
pairs each tuple t 1 of R 1 with each tuple t 2 of R 2 and puts in R a tuple t 1 t 2.
Theta-Join
R = R 1. C/ R 2
is equivalent to R = C (R 1 R 2 ).
Example
Sells =
bar b eer price
Jo e's Bud 2. Jo e's Miller 2. Sue's Bud 2. Sue's Co ors 3.
Bars =
name addr
Jo e's Maple St. Sue's River Rd.
BarInfo = Sells (^) S ells:bar =.B/ ar s:name Bars
bar b eer price name addr Jo e's Bud 2.50 Jo e's Maple St. Jo e's Miller 2.75 Jo e's Maple St. Sue's Bud 2.50 Sue's River Rd. Sue's Co ors 3.00 Sue's River Rd.
Renaming
S (A 1 ;::: ;An ) (R ) pro duces a relation identical to R but named S and with attributes, in order, named A 1 ; : : : ; An.
Example
Bars =
name addr Jo e's Maple St. Sue's River Rd.
R(bar;addr ) (Bars ) =
bar addr
Jo e's Maple St. Sue's River Rd.
The name of the ab ove relation is R.
Combining Op erations
Algebra =
For relational algebra:
Query = expression of relational algebra.
Each Expression Needs a Schema
If [ , \ , applied, schemas are the same, so use this schema.
Pro jection: use the attributes listed in the pro jection.
Selection: no change in schema.
Pro duct R S : use attributes of R and S.
F But if they share an attribute A, pre x it with the relation name, as R :A, S:A.
Theta-join: same as pro duct.
Natural join: use attributes from each relation; common attributes are merged anyway.
Renaming: whatever it says.
Example
Find the bars that are either on Maple Street or sell Bud for less than $3.
Sells(bar, beer, price) Bars(name, addr)
name
addr =M apleS t:
Bars
R(name)
bar
pr ice<$3 AN D beer =B ud
Sells
Linear Notation for Expressions
Invent new names for intermediate relations, and assign them values that are algebraic expressions.
Renaming of attributes implicit in schema of new relation.
Example
Find the bars that are either on Maple Street or sell Bud for less than $3.
Sells(bar, beer, price) Bars(name, addr)
R1(name) := name (addr =M aple S t: (Bars)) R2(name) := bar (beer =B ud AN D pr ice<$3 (Sells)) Answer(name) := R1 [ R
Bag Semantics
A relation (in SQL, at least) is really a bag or multiset.
It may contain the same tuple more than once, although there is no sp eci ed order (unlike a list).
Example: f 1 ; 2 ; 1 ; 3 g is a bag and not a set.
Select, pro ject, and join work for bags as well as sets. F Just work on a tuple-by-tuple basis, and don't eliminate duplicates.
Laws for Bags Di er From Laws for Sets
Some familiar laws continue to hold for bags.
F Examples: union and intersection are still commutative and asso ciative.
But other laws that hold for sets do not hold for bags.
Example
R \ (S [ T ) (R \ S ) [ (R \ T ) holds for sets.
Let R , S , and T each b e the bag f 1 g.
Left side: S [ T = f 1 ; 1 g; R \ (S [ T ) = f 1 g.
Right side: R \ S = R \ T = f 1 g; (R \ S ) [ (R \ T ) = f 1 g [ f 1 g = f 1 ; 1 g 6 = f 1 g.
Extended (\Nonclassical ") Relational Algebra
Adds features needed for SQL, bags.
Sorting
L (R ) = list of tuples of R , ordered according to attributes on list L.
Note that result typ e is outside the normal typ es (set or bag) for relational algebra. F Consequence: cannot b e followed by other relational op erators.
example
R =
A B 1 3 3 4 5 2
B (R ) = [(5; 2); (1; 3); (3; 4)].
Extended Pro jection
Allow the columns in the pro jection to b e functions of one or more columns in the argument relation.
Example
R =
A B 1 2 3 4
A+B ;A;A (R ) =
A + B A 1 A 2 3 1 1 7 3 3