Docsity

Relational Algebra: Manipulating Relations with Set-Based Operators, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
\Core" Relational Algebra
A small set of set-based operators that allowusto
manipulate relations in limited but useful ways.
The operators are:
1. Union, intersection, and dierence: the usual
set operators.
✦
But the relation schemas must be the
same.
2.
Selection
: Picking certain rows from a
relation.
3.
Projection
: Picking certain columns.
4.
Products and joins
: Composing relations in
useful ways.
5.
Renaming
of relations and their attributes.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Relational Algebra: Manipulating Relations with Set-Based Operators and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

\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:

  1. Union, intersection, and di erence: the usual set op erators.

F But the relation schemas must b e the same.

  1. Selection : Picking certain rows from a relation.
  2. Projection : Picking certain columns.
  3. Products and joins : Comp osing relations in useful ways.
  4. Renaming of relations and their attributes.

Selection

R 1 = C (R 2 )

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 =

  1. Basis arguments +
  2. Ways of constructing expressions.

For relational algebra:

  1. Arguments = variables standing for relations
    • nite, constant relations.
  2. Expressions constructed by applying one of the op erators + parentheses.

 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.

  1. Duplicate-eli minati on op erator .
  2. Extended pro jection.
  3. Sorting op erator .
  4. Grouping-and-aggregation op erator.
  5. Outerjoin op erator (^) ./.

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