relational algebra cis 4301 lecture notes lecture 14 - 2/28/2006

11
Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Upload: philippa-tucker

Post on 28-Dec-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Relational Algebra

CIS 4301

Lecture Notes

Lecture 14 - 2/28/2006

Page 2: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 2

Sample Relational Schema

Movie (Title,Year,length,filmType,

studioName,producerC#)

StarsIn (MovieTitle,MovieYear,StarName)

MovieStar(Name,address,gender,birthdate)

MovieExec(name,address,Cert#,netWorth)

Studio (Name,address,presC#)

Page 3: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 3

Relational Operator: Rename

The unary rename operator gives a new schema to a relation

S(A1, A2, …, An) (R)

Renames relation R to S with attributes A1, A2, …, An

Note: S has exactly the same tuples as R Also, note the implicit rename operation in our notation from lecture 15

S(A1, A2, …, An) := R is shorthand notation for S(A1, A2, …, An)

(R)

Page 4: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 4

Example

MyStarsIn(Title, Year, MoviedStar) (StarsIn)

MovieTitleMovieYearStarName Title Year MovieStar

Star Wars 1977 H. Ford Star Wars 1977 H. Ford

Star Wars 1977 C. FisherStar Wars 1977 C. Fisher

StarsIn MyStarsIn

Page 5: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 5

Complex Expressions Form expressions of arbitrary complexity by applying operators either to

given relations or to relations that are the result of applying one or more relational operators to relations

Three notations for expressing sequence of operations (just like in arithmetic) Sequence of assignment statement

Use assignment operator ‘:=’ to assign relation to a relation variable

Expression with several operators Using parentheses as necessary

Expression tree

Page 6: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 6

Example

“What are the stars of movies that are at least 100 minutes long?”

Page 7: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 7

Sequence of Assignment Statements

Find the address of the studio who produced the Star Wars movie made in 1977.

Page 8: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 8

Expression in Single Assignment

Express previous query as single expression:

What about precedence? Option 1: Use parenthesis to make precendence clear

Option 2: Use precedence rules for relational operators

Unary operators have highest precedence, bind first

Next come product and joins Then intersection Finally, union and set difference bind last

Page 9: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 9

Expression Tree

Leaves are operands Either variables standing for relations or for particular constant relations

Interior nodes are operators, applied to their child or children

Example: Find all the producers of movies in which H. Ford played whose networth is greater than $10M

Page 10: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 10

Exercise

Find movie star executive producer pairs who live at the same address?

Page 11: Relational Algebra CIS 4301 Lecture Notes Lecture 14 - 2/28/2006

Lecture 14© CIS 4301 - Spring 2006 11

Dependent and Independent Operations

Union, difference, selection, projection, cartesian product, and renaming form an independent set of operations None of them can be written in terms of the other five

Can you express R S R join=c S R join S

in terms of the 5 independent operations?