Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web page (
STEMPNU 2 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.
STEMPNU 3 What is Relational Algebra? An algebra Operands: relations or variables that represent relations. Operators: common operations that we need to do with relations in a database. Relational Algebra: a Theoretical basis of query language.
STEMPNU 4 Core Relational Algebra Set Operators Union, intersection, and difference. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes.
STEMPNU 5 Selection R1 = SELECT C (R) C is a condition (as in “ if ” statements) that refers to attributes of R. R1 is all those tuples of R that satisfy C. Denoted as C (R)
STEMPNU 6 Example barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 Relation Sells: JoeMenu = SELECT bar=“Joe’s” (Sells): barbeerprice Joe’sBud2.50 Joe’sMiller2.75
STEMPNU 7 Projection R1 = PROJ L (R) L : a list of attributes from the schema of R. R1 is constructed by looking at each tuple of R, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. Eliminate duplicate tuples, if any. Denoted L (R)
STEMPNU 8 Example Relation Sells: Prices = PROJ beer,price (Sells): barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 beerprice Bud2.50 Miller2.75 Miller3.00
STEMPNU 9 Product (also Cartesian Product) R3 = R1 x R2 Pair each tuple t1 of R1 with each tuple t2 of R2. Concatenation (t1 t2) is a tuple of R3. Schema of R3: the attributes of R1 and R2, in order. But beware attribute A of the same name in R1 and R2: use R1.A and R2.A.
STEMPNU 10 Example: R1 x R2 AB BC R1(A, B) R3(A,R1.B,R2.B,C) A,R1.B,R2.B,C R2(B, C)
STEMPNU 11 Theta-Join R3 = R1 JOIN C R2 Take the product R1 X R2. Then apply SELECT C to the result. Also denoted R1 C R2 As for SELECT, C can be any boolean-valued condition. Historic versions of this operator allowed only A theta B, where theta was =, <, etc.; hence the name “ theta-join. ”
STEMPNU 12 Example Barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sCoors3.00 nameaddr Joe’sMaple St. Sue’sRiver Rd. BarInfo = Sells JOIN Sells.bar = Bars.name Bars BarInfo (bar, beer, price, name, addr) barbeerpricenameaddr Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Joe’sMaple St. Sue’sBud2.50Sue’sRiver Rd. Sue’sCoors3.00Sue’sRiver Rd Sells (bar, beer,price) Bars (name,addr)
STEMPNU 13 Cartesian Product and Theta Join R1 C R2 R1 X R2 R1 C R2 = C (R1 X R2) R1R2 X
STEMPNU 14 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 or R3 = R1 R2 R1 JOIN R2 = Select R1.A == R2.A (R1 X R2)
STEMPNU 15 Example Barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sCoors3.00 Baraddr Joe’sMaple St. Sue’sRiver Rd. BarInfo = Sells Bars BarInfo (bar, beer, price, name, addr) Barbeerpriceaddr Joe’sBud2.50Maple St. Joe’sMiller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd Sells (bar, beer,price) Bars (name,addr)
STEMPNU 16 Renaming The RENAME operator gives a new schema to a relation. R1 := RENAME R1(A1, …,An) (R2) makes R1 be a relation with attributes A1, …,An and the same tuples as R2. Simplified notation: R1(A1, …,An) := R2 or R1(A1, …,An) (R2)