Docsity

Relational Algebra and Calculus - Database management System | EECS 484, Quizzes of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 1
Relational Algebra and Calculus
Chapter 4
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 2
ā€œFormalā€ Query Languages
•Foundation for commercial query languages like SQL
•Two types
•Declarative: Rela tional Calculus
•Describe what a user wants, rather than how to compute it.
•Procedural : Rela tional Algebra
•Operational, very useful for representing execution plans.
•Query Languages != programming languages!
•QLs not expected to b e ā€œTuring completeā€.
•QLs not intended to b e used for complex calculations.
•QLs support easy, effi cient access to large data sets.
Understanding Algebra & Calc ulus is key to
understanding SQL, query processing!
1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 3
Relational Algebra Preliminaries
•Query:
•Input: Relational instances
•Output: Relational instances!
•Specified using the schemas.
•May produce different results for different instances.
•But schema of the result is fix ed.
Relational Algebra is
ā€œclosedā€
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Relational Algebra and Calculus - Database management System | EECS 484 and more Quizzes Database Management Systems (DBMS) in PDF only on Docsity!

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 1

Relational Algebra and Calculus

Chapter 4

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 2

ā€œFormalā€ Query Languages

  • Foundation for commercial query languages like SQL
  • Two types
    • Declarative: Relational Calculus
      • Describe what a user wants, rather than how to compute it.
    • Procedural : Relational Algebra
      • Operational, very useful for representing execution plans.
  • Query Languages != programming languages!
    • QLs not expected to be ā€œTuring completeā€.
    • QLs not intended to be used for complex calculations.
    • QLs support easy, efficient access to large data sets.

Understanding Algebra & Calculus is key to

understanding SQL, query processing!

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 3

Relational Algebra Preliminaries

  • Query:
    • Input: Relational instances
    • Output: Relational instances!
    • Specified using the schemas.
      • May produce different results for different instances.
      • But schema of the result is fixed.

Relational Algebra is

ā€œclosedā€

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 4

Relational Algebra

  • Basic operations:
    • Selection ( ) Selects a subset of rows from relation.
    • Projection ( ) Deletes unwanted columns from relation.
    • Cross-product ( ) Allows us to combine two relations.
    • Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
    • Union ( ) Tuples in reln. 1 and in reln. 2.
  • Additional operations (constructed from basic ops):
    • Intersection, Join, Division, Renaming
      • Not essential, but (very!) useful.
  • Because algebra is closed, we can compose

operators

U

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 5

Selection

  • Retrieve rows that satisfy

a logical condition

  • Conditions are boolean

combinations (∧, V) of

terms

  • Each term has one of the

following forms:

  • Attr1 Attr
  • Attr value
  • one of

{>, ≄, = ≤, <}

5 Natalie Coughlin swimming USA

6 Paul Hamm gymnastics USA

4 Johann Koss skating Norway

3 Michael Phelps swimming USA

2 Jackie Joyner-Kersee track USA

1 Mary Lou Retton gymnastics USA

aid name sport country

Athlete

6 Paul Hamm gymnastics USA

1 Mary Lou Retton gymnastics USA

aid name sport country

( ) ' ' ' '

Athlete sport = gymnastics! country = USA

"

Schema of result?

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 6

Projection

  • Delete attributes

that are not in

projection list

  • Remove

duplicates!

  • Remember

classical relational

model is a SET

5 Natalie Coughlin swimming USA

6 Paul Hamm gymnastics USA

4 Johann Koss skating Norway

3 Michael Phelps swimming USA

2 Jackie Joyner-Kerseetrack USA

1 Mary Lou Retton gymnastics USA

aidname sport country

Athlete

skating Norway

swimming USA

track USA

gymnastics USA

sport country

( ) ,

Athlete sportcountry

Schema of result?

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 10

Derived Operators: Division

  • Useful for queries like:

Find customers with accounts at all branches in Brooklyn.

  • Let A have 2 fields, x and y ; B have only field y :
    • A/B = { | āˆ€āˆˆ B, ∈A}
    • A/B contains all x tuples (customers) such that for every

y tuple (branches in Brooklyn) in B, there is an < x,y>

tuple in A

  • In general, x and y can be lists of fields; y is the list of

fields in B, and x (^) !y is the list of fields of A.

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 11

Examples of Division A/B

cid bname

c 1 b 1

c 1 b 2

c 1 b 3

c 1 b 4

c 2 b 1

c 2 b 2

c 3 b 2

c 4 b 2

c 4 b 4

A

bname

b 2

B

bname

b 2

b 4

B

bname

b 1

b 2

b 4

B

cid

c 1

c 2

c 3

c 4

A/B

cid

c 1

c 4

A/B

cid

c 1

A/B

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 12

Expressing A/B Using Basic Operators

  • Can be equivalently expressed using basic operators
  • Idea: For A/B, compute all x values that are not

disqualified by some y value in B.

  • x value is disqualified if by attaching y value from B, we

obtain an tuple that is not in A

( ) (( ( A ) B ) A )

x x

A

x

Can you express this # !# # "!

operator using basic

operators?

( A )!

x

A/B:^ "

Disqualified x values

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 13

Examples of Division A/B

A B

x

cid bname

c 1 b 1

c 1 b 2

c 1 b 3

c 1 b 4

c 2 b 1

c 2 b 2

c 3 b 2

c 4 b 2

c 4 b 4

A

bname

b 2

b 4

B

cid bname

c1 b

c1 b

c2 b

c2 b

c3 b

c3 b

c4 b

c4 b

-A

cid

c 1

c 4

A/B

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 14

Relational Calculus

  • Alternative to relational algebra
  • Declarative (non-procedural) – Describes answers without

saying how to compute them

  • Comes in two flavors (see textbook):
    • TRC: Tuple relational calculus and
    • DRC: Domain relational calculus
  • Both are simple subsets of first-order logic
  • Expressions in the calculus are called formulas. An answer

tuple is essentially an assignment of constants to

variables that make the formula evaluate to true.

{ T | T " Loan! T. amount > 1400 }

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 15

Expressive Power

  • Codd’s Theorem: Every RA query can be

expressed as a safe query in relational

calculus; the converse is also true.

  • Relational Completeness: A ā€œrelationally

completeā€ query language (e.g., SQL) can

express every query that is expressible in

relational algebra/calculus.

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 19

Find names of sailors

who’ve reserved boat #

Solution 2:! ( T emp , " Re serves )

bid

= 103

! ( Temp 2 , Temp 1 >< Sailors )

sname

( T emp 2 )

Solution 3:

sname bid

( (Re serves Sailors ))

= 103

><

sname bid

(( Re s erves ) Sailors )

= 103

Solution 1:

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 20

Find names of sailors

who’ve reserved a red boat

! " sname color red

(( Boats serves Sailors

' '

) Re )

=

>< ><

An equivalent solution:

Ļ€ sname

(Ļ€ sid

((Ļ€ bid

σ color=ā€˜red’

Boats)  Res)  Sailors)

Query optimizer chooses from the (equivalent)

expressions and chooses one for efficiency of

evaluation.

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 21

Find the names of sailors

who’ve reserved at least

one boat

(Re serves Sailors )

sname

! ><

Sailor appears in this

intermediate relation

only if there is at least

one Reserves tuple with

same sid.

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 22

Find names of sailors

who’ve reserved a red OR

green boat

  • Identify all red or green boats, then
  • find sailors who’ve reserved one of these boats:

!( , ( "

' ' ' '

Tempboats ))

color red color green

Boats

= # =

sname

( T empboats >< Re serves >< Sailors )

( )))

' '

( )

' '

( , ( Boats

color green

Boats

color red

Tempboats

=

!

=

" "

! sname

( T empboats >< Re serves >< Sailors )

Equivalent:

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 23

Find names of sailors

who’ve reserved a red AND

green boat

  • Identify
    • sailors who’ve reserved red boats
    • sailors who’ve reserved green boats
  • Then find the intersection ( sid is a key for Sailors):

) Re ))

' '

( , ( ( Boats serves

sid color red

Tempred ><

=

sname

(( T empred " Tempgreen ) >< Sailors )

Tempgreen ) Re ))

sid color green

Boats serves

=

1/23/09 EECS 484: Database Management Systems, Kristen LeFevre 24

Find the sids of sailors over

age 20 who have not

reserved a red boat

) Re )

' '

20

Boats serves Sailors

sid color red

Sailors sid age

>