Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006
Lecture 14© CIS Spring 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#)
Lecture 14© CIS Spring Relational Operator: Rename The unary rename operator gives a new schema to a relation S(A 1, A 2, …, A n ) (R) Renames relation R to S with attributes A 1, A 2, …, A n Note: S has exactly the same tuples as R Also, note the implicit rename operation in our notation from lecture 15 S(A 1, A 2, …, A n ) := R is shorthand notation for S(A 1, A 2, …, A n ) (R)
Lecture 14© CIS Spring Example MyStarsIn(Title, Year, MoviedStar) (StarsIn) MovieTitle MovieYear StarName Title Year MovieStar Star Wars 1977 H. Ford Star Wars 1977 H. Ford Star Wars 1977 C. Fisher Star Wars 1977 C. Fisher StarsIn MyStarsIn
Lecture 14© CIS Spring 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
Lecture 14© CIS Spring Example “What are the stars of movies that are at least 100 minutes long?”
Lecture 14© CIS Spring Sequence of Assignment Statements Find the address of the studio who produced the Star Wars movie made in 1977.
Lecture 14© CIS Spring 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
Lecture 14© CIS Spring 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
Lecture 14© CIS Spring Exercise Find movie star executive producer pairs who live at the same address?
Lecture 14© CIS Spring 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?