Docsity

Relational Algebra - Database Systems - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Relational Algebra - Database Systems - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Relational Algebra

Basic Operations

Algebra of Bags

What is an “Algebra”

• Mathematical system consisting of:

  • Operands --- variables or values from which new

values can be constructed.

  • Operators --- symbols denoting procedures that

construct new values from given values.

Core Relational Algebra

• Union, intersection, and difference.

  • Usual set operations, but both operands must

have the same relation schema.

• Selection: picking certain rows.

• Projection: picking certain columns.

• Products and joins: compositions of relations.

• Renaming of relations and attributes.

Selection

• R1 := σ C (R2)

  • C is a condition (as in “if” statements) that refers

to attributes of R2.

  • R1 is all those tuples of R2 that satisfy C.

Projection

• R1 := π L (R2)

  • L is a list of attributes from the schema of R2.
  • R1 is constructed by looking at each tuple of R2,

extracting the attributes on list L , in the order

specified, and creating from those components a

tuple for R1.

  • Eliminate duplicate tuples, if any.

Example: Projection

8

Relation Sells: bar beer price Joe’s Bud 2. Joe’s Miller 2. Sue’s Bud 2. Sue’s Miller 3.

Prices := πbeer,price (Sells):

beer price Bud 2. Miller 2. Miller 3.

Example: Extended Projection

10

R = ( A B )

π A+B->C,A,A (R) = C A1 A 3 1 1 7 3 3

Product

• R3 := R1 Χ R

  • Pair each tuple t1 of R1 with each tuple t2 of R2.
  • Concatenation t1t2 is a tuple of R3.
  • Schema of R3 is the attributes of R1 and then R2,

in order.

  • But beware attribute A of the same name in R

and R2: use R1. A and R2. A.

Theta-Join

• R3 := R1 ⋈ C R

  • Take the product R1 Χ R2.

– Then apply σ C to the result.

• As for σ, C can be any boolean-valued

condition.

  • Historic versions of this operator allowed only A θ

B, where θ is =, <, etc.; hence the name “theta-

join.”

Example: Theta Join

14

Sells( bar, beer, price ) Bars( name, addr ) Joe’s Bud 2.50 Joe’s Maple St. Joe’s Miller 2.75 Sue’s River Rd. Sue’s Bud 2. Sue’s Coors 3.

BarInfo := Sells ⋈Sells.bar = Bars.name Bars

BarInfo( bar, beer, price, name, addr ) Joe’s Bud 2.50 Joe’s Maple St. Joe’s Miller 2.75 Joe’s Maple St. Sue’s Bud 2.50 Sue’s River Rd. Sue’s Coors 3.00 Sue’s River Rd.

Example: Natural Join

16

Sells( bar, beer, price ) Bars( bar, addr ) Joe’s Bud 2.50 Joe’s Maple St. Joe’s Miller 2.75 Sue’s River Rd. Sue’s Bud 2. Sue’s Coors 3.

BarInfo := Sells ⋈ Bars

Note: Bars.name has become Bars.bar to make the natural join “work.” BarInfo( bar, beer, price, addr ) Joe’s Bud 2.50 Maple St. Joe’s Milller 2.75 Maple St. Sue’s Bud 2.50 River Rd. Sue’s Coors 3.00 River Rd. Docsity.com

Renaming

• The ρ operator gives a new schema to a

relation.

• R1 := ρR1(A1,…,A n ) (R2) makes R1 be a relation

with attributes A1,…,A n and the same tuples

as R2.

• Simplified notation: R1(A1,…,A n ) := R2.

Building Complex Expressions

• Combine operators with parentheses and

precedence rules.

• Three notations, just as in arithmetic:

1. Sequences of assignment statements.

2. Expressions with several operators.

3. Expression trees.

Sequences of Assignments

• Create temporary relation names.

• Renaming can be implied by giving relations a

list of attributes.

• Example: R3 := R1 ⋈ C R2 can be written:

R4 := R1 Χ R

R3 := σ C (R4)