Relational algebra unary and binary operators lecture 2
Download
1 / 37

Relational Algebra Unary and Binary Operators Lecture 2 - PowerPoint PPT Presentation


  • 139 Views
  • Uploaded on

Relational Algebra Unary and Binary Operators Lecture 2. Instructor: Haya sammaneh. What is an “Algebra”. Mathematical system consisting of: Operands --- variables or values Operators --- symbols denoting procedures. Core Relational Algebra. Unary Operations:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Relational Algebra Unary and Binary Operators Lecture 2' - maxima


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Relational algebra unary and binary operators lecture 2 l.jpg

Relational AlgebraUnary and Binary OperatorsLecture 2

Instructor: Haya sammaneh


What is an algebra l.jpg
What is an “Algebra”

  • Mathematical system consisting of:

    • Operands --- variables or values

    • Operators --- symbols denoting procedures


Core relational algebra l.jpg
Core Relational Algebra

Unary Operations:

  • Selection : picking certain rows[tuples].

  • Projection : picking certain columns.

  • Renaming of relations and attributes.

    Binary Operations:

  • Union , intersection , and difference .

    • require both operands have the same relation schema.

  • Products and joins : compositions of relations.


Example l.jpg

R1

Example

  • “Sailors” and “Reserves” relations for our examples.

S1

S2


Selection sigma l.jpg
Selection, sigma

  • R1 := SELECTC (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.

    • Condition may have : =, ≠, >,<,≤,≥,

      ̚ (not), ̭ (and), ̬ (or)


Example6 l.jpg

R2 := SELECTbar=“Joe’s”(R1):

bar beer price

Joe’s Bud 2.50

Joe’s Miller 2.75

Example

R1:

bar beer price

Joe’s Bud 2.50

Joe’s Miller 2.75

Sue’s Bud 2.50

Sue’s Miller 3.00


Selection l.jpg
Selection

S2

  • Selects rows that satisfy selection condition.

  • No duplicates in result!


Projection pi l.jpg
Projection, Pi

  • R1 := PROJL (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.


Example9 l.jpg

Prices := PROJbeer,price(Sells):

beer price

Bud 2.50

Miller 2.75

Miller 3.00

Example

Relation Sells:

bar beer price

Joe’s Bud 2.50

Joe’s Miller 2.75

Sue’s Bud 2.50

Sue’s Miller 3.00




Selection and projection l.jpg
Selection and projection

Find the largest account balance in the bank?

account

Solution: compute a temporary relation consisting of those balances

that are not the large, then take the set difference between the relation

Π balance (account) and the temporary relation

Π account.balance(account) –

(Π account.balance(σ account.balance<new_account.balance(account X ρ new_account (account) )))


Union intersection set difference l.jpg
Union, Intersection, Set-Difference

  • All of these operations take two input relations, which must be union-compatible:

    • Same number of fields.

    • Corresponding fields have the same type.


Union l.jpg
Union,

S1

S2



Set difference l.jpg
Set difference,

S1

Find the relation that are in one relation but not in another

S2


Cross product l.jpg
Cross Product

  • R3 := R1 X R2

    • 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 be ware attribute A of the same name in R1 and R2: use R1.A and R2.A.


Example r3 r1 x r2 l.jpg

R3( A, R1.B, R2.B, C )

1 2 5 6

1 2 7 8

1 2 9 10

3 4 5 6

3 4 7 8

3 4 9 10

Example: R3 := R1 X R2

R1( A, B )

1 2

3 4

R2( B, C )

5 6

7 8

9 10


Renaming operator roh l.jpg
Renaming operator, roh :

Return the result of expression E under the name x

Return the result of expression E under

the name x and with attributes renamed to

A1,A2..An


Renaming l.jpg
Renaming

  • The RENAME operator gives a new schema to a relation.

  • R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2.

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


Example21 l.jpg

R( bar, addr )

Joe’s Maple St.

Sue’s River Rd.

Example

Bars( name, addr )

Joe’s Maple St.

Sue’s River Rd.

R(bar, addr) := Bars


Theta join l.jpg
Theta-Join

  • R3 := R1 JOINC R2

    • Take the product R1 X R2.

    • Then apply SELECTC to the result.


Example23 l.jpg
Example

R1

S1


Theta joins l.jpg
Theta-Joins

  • Condition Join:

  • Result schema same as that of cross-product.

  • Fewer tuples than cross-product


Example25 l.jpg

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

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.50

Sue’s Coors 3.00

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


Natural join l.jpg
Natural Join

  • A frequent type of join connects two relations by:

    • Equating attributes of the same name, and

    • Projecting out one copy of each pair of equated attributes.

  • Called natural join.

  • Denoted R3 := R1 JOIN R2.


Natural joins l.jpg
Natural Joins

  • Equi-Join: A special case of condition join where the condition c contains only equalities.

  • Result schema similar to cross-product, but only one copy of fields for which equality is specified.


Division l.jpg
Division

  • useful for expressing queries like: Find sailors who have reserved allboats.

  • Suited for queries that include the phrase “for all”


Examples of division a b l.jpg
Examples of Division A/B

B1

B2

B3

A/B1

A/B2

A/B3

A


Building complex expressions l.jpg
Building Complex Expressions

  • Combine operators with parentheses and precedence rules.

  • Three notations, just as in arithmetic:

    • Sequences of assignment statements.

    • Expressions with several operators.

    • Expression trees.


Sequences of assignments l.jpg
Sequences of Assignments

  • Create temporary relation names.

  • Renaming can be implied by giving relations a list of attributes.

  • Example: R3 := R1 JOINC R2 can be written:

    R4 := R1 X R2

    R3 := SELECTC (R4)


Expressions in a single assignment l.jpg
Expressions in a Single Assignment

  • Example: the theta-join R3 := R1 JOINC R2 can be written: R3 := SELECTC (R1 X R2)

  • Precedence of relational operators:

    • [SELECT, PROJECT, RENAME] (highest).

    • [PRODUCT, JOIN].

    • INTERSECTION.

    • [UNION, --]


Expression trees l.jpg
Expression Trees

  • Leaves are operands

  • Interior nodes are operators


Example34 l.jpg
Example

  • Using the relations Bars(name, addr) and Sells(bar, beer, price), find the names of all the bars that are either on Maple Street or sell Bud for less than $3.


As a tree l.jpg

UNION

RENAMER(name)

PROJECTname

PROJECTbar

SELECTaddr = “Maple St.”

SELECTprice<3 AND beer=“Bud”

As a Tree:

Bars

Sells


Example36 l.jpg
Example

  • Using Sells(bar, beer, price), find the bars that sell two different beers at the same price.

  • Strategy: by renaming, define a copy of Sells, called S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price.


The tree l.jpg

PROJECTbar

SELECTbeer != beer1

Natural JOIN

RENAMES(bar, beer1, price)

The Tree

Sells

Sells


ad