“Core” Relational Algebra A small set of operators that allow us to manipulate relations in limited but useful ways. The operators are: 1.Union, intersection,

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Winter 2002Arthur Keller – CS 1806–1 Schedule Today: Jan. 22 (T) u SQL Queries. u Read Sections Assignment 2 due. Jan. 24 (TH) u Subqueries, Grouping.
SQL CSET 3300.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
1 Lecture 12: Further relational algebra, further SQL
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Relational Operations on Bags Extended Operators of Relational Algebra.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
Winter 2002Arthur Keller – CS 1805–1 Schedule Today: Jan. 17 (TH) u Relational Algebra. u Read Chapter 5. Project Part 1 due. Jan. 22 (T) u SQL Queries.
Fall 2001Arthur Keller – CS 1806–1 Schedule Today (TH) Bags and SQL Queries. u Read Sections Project Part 2 due. Oct. 16 (T) Duplicates, Aggregation,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Winter 2002Arthur Keller – CS 1804–1 Schedule Today: Jan. 15 (T) u Normal Forms, Multivalued Dependencies. u Read Sections Assignment 1 due. Jan.
Relational Operations on Bags Extended Operators of Relational Algebra.
Fall 2001Arthur Keller – CS 1805–1 Schedule Today Oct. 9 (T) Multivalued Dependencies, Relational Algebra u Read Sections 3.7, Assignment 2 due.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Fall 2001Arthur Keller – CS 1804–1 Schedule Today Oct. 4 (TH) Functional Dependencies and Normalization. u Read Sections Project Part 1 due. Oct.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Databases 1 Seventh lecture. Topics of the lecture Extended relational algebra Normalization Normal forms 2.
Normalization Goal = BCNF = Boyce-Codd Normal Form = all FD’s follow from the fact “key  everything.” Formally, R is in BCNF if for every nontrivial FD.
Databases 1 First lecture. Informations Lecture: Monday 12:15-13:45 (3.716) Practice: Thursday 10:15-11:45 (2-519) Website of the course:
SCUHolliday6–1 Schedule Today: u SQL Queries. u Read Sections Next time u Subqueries, Grouping and Aggregation. u Read Sections And then.
SCUJ. Holliday - coen 1784–1 Schedule Today: u Normal Forms. u Section 3.6. Next u Relational Algebra. Read chapter 5 to page 199 After that u SQL Queries.
Relational Algebra (Chapter 7)
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra Basic Operations Algebra of Bags.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
From Professor Ullman, Relational Algebra.
1 Lecture 2 Relational Algebra Based on
Winter 2002Judy Cushing5–1 Schedule Today: Jan. 23 (Wed) u Relational Algebra. u Read Chapter 5. u Project Part 2 due Friday 5pm, Judy’s mailbox in Lab.
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections And then u Subqueries,
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
1 Introduction to SQL Database Systems. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation.
Relational Algebra BASIC OPERATIONS 1 DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA.
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 Introduction to Database Systems, CS420 Relational Algebra.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Basic Operations Algebra of Bags
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
COP4710 Database Systems Relational Algebra.
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Schedule Today: Jan. 23 (wed) Week of Jan 28
CPSC-608 Database Systems
IST 210: Organization of Data
Operators Expression Trees Bag Model of Data
CPSC-310 Database Systems
Instructor: Mohamed Eltabakh
Basic Operations Algebra of Bags
Schedule Today: Next And then Relational Algebra.
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

“Core” Relational Algebra A small set of operators that allow us to manipulate relations in limited but useful ways. The operators are: 1.Union, intersection, and difference: the usual set operators. u But the relation schemas must be the same. 2.Selection: Picking certain rows from a relation. 3.Projection: Picking certain columns. 4.Products and joins: Composing relations in useful ways. 5.Renaming of relations and their attributes.

Relational Algebra limited expressive power (subset of possible queries) good optimizer possible rich enough language to express enough useful things Finiteness  SELECT π PROJECT X CARTESIAN PRODUCT FUNDAMENTAL U UNION BINARY – SET-DIFFERENCE  SET-INTERSECTION  THETA-JOIN CAN BE DEFINED NATURAL JOIN IN TERMS OF ÷ DIVISION or QUOTIENT FUNDAMENTAL OPS UNARY

Extra Example Relations DEPOSIT(branchName, acctNo,custName,balance) CUSTOMER(custName,street,custCity) BORROW(branchName,loan-no,custName,amount) BRANCH(branchName,assets, branchCity) CLIENT(custName,emplName) Borrow BN L# CN AMT T1 Midtown 123 Fred 600 T2 Midtown 234 Sally 1200 T3 Midtown 235 Sally 1500 T4 Downtown 612 Tom 2000

Selection R 1 =  C (R 2 ) where C is a condition involving the attributes of relation R 2. Example Relation Sells : JoeMenu =  bar=Joe's ( Sells )

SELECT (  ) arity(  (R)) = arity(R) 0  card(  (R))  card(R)  c (R)  c (R)  (R) c is selection condition: terms of form: attr op value attr op attr op is one of  ≠ ≥ example of term: branch-name = ‘Midtown’ terms are connected by   branchName = ‘Midtown’  amount > 1000 (Borrow)  custName = empName (client)

Projection R 1 =  L (R 2 ) where L is a list of attributes from the schema of R 2. Example  beer,price ( Sells ) Notice elimination of duplicate tuples.

Projection (π) 0  card (π A (R))  card (R) arity (π A (R)) = m  arity(R) = k π i 1,...,i m (R) 1  i j  k distinct  produces set of m-tuples  a 1,...,a m  such that  k-tuple  b 1,...,b k  in R where a j = b i j for j = 1,...,m π branchName, custName  (Borrow) Midtown Fred Midtown Sally Downtown Tom

Product R = R 1  R 2 pairs each tuple t 1 of R 1 with each tuple t 2 of R 2 and puts in R a tuple t 1 t 2.

Cartesian Product (  ) arity(R) = k1 arity(R  S) = k1 + k2 arity(S) = k2 card(R  S) = card(R)  card(S) R  S is the set all possible (k1 + k2)-tuples whose first k1 attributes are a tuple in R last k2 attributes are a tuple in S R S R  S A B C D D E F A B C D D' E F

Theta-Join R = R 1 C R 2 is equivalent to R =  C (R 1  R 2 ).

Example Sells = Bars = BarInfo = Sells Sells.Bar=Bars.Name Bars

Theta-Join R arity(R) = r arity(S) = s arity (R S) = r + s 0  card(R S)  card(R)  card(S) S i  j  $i  $  r  j)  R  S) R S 1... r 1... s   can be = ≠  If equal (=), then it is an  EQUIJOIN RS =  (R  S) c c R(A B C) S(C D E) result has schema T(A B C C' D E) R.A<S.D ij R(ABC) S(CDE) T(ABCC’DE)

Natural Join R = R 1 R 2 calls for the theta-join of R 1 and R 2 with the condition that all attributes of the same name be equated. Then, one column for each pair of equated attributes is projected out. Example Suppose the attribute name in relation Bars was changed to bar, to match the bar name in Sells. BarInfo = Sells Bars

Renaming  S(A 1,…,A n ) (R) produces a relation identical to R but named S and with attributes, in order, named A 1,…,A n. Example Bars =  R( bar,addr ) ( Bars ) = The name of the second relation is R.

Union (R  S) arity(R) = arity(S) = arity(R  S) max(card(R),card(S))  card(R  S)  card(R) + card(S) set of tuples in R or S or bothR  R  S S  R  S Find customers of Perryridge Branch π Cust-Name (  Branch-Name = "Perryridge" (BORROW  DEPOSIT) )

Difference(R  S) arity(R) = arity(S) = arity(R  –  S) 0  card(R –  S)  card(R)  R – S  R is the tuples in R not in S Depositors of Perryridge who aren't borrowers of Perryridge π custName (  branchName = ‘Perryridge’ ( DEPOSIT – BORROW) ) Deposit Borrow π custName (  branchName = ‘Perryridge’ ( DEPOSIT) ) — π custName (  branchName = ‘Perryridge’ ( BORROW) ) Does  (π (D)  π (B) ) work?

Combining Operations Algebra = 1.Basis arguments + 2.Ways of constructing expressions. For relational algebra: 1.Arguments = variables standing for relations + finite, constant relations. 2.Expressions constructed by applying one of the operators + parentheses. Query = expression of relational algebra.

π custName,custCity (  Client.Banker-Name = ‘Johnson’ (Client  Customer) ) = π cust-Name,custCity (Customer) Is this always true? Is this what we wanted? π Client.custName, Customer.custCity (  Client.bankerName = ‘Johnson’  Client.custName = Customer.custName (Client  Customer) ) π Client.custName, Customer  custCity (  Client.custName = Customer.custName (Customer  π custName  Client.bankerName=‘Johnson’ (Client) ) ) )

SET INTERSECTION arity(R) = arity(S) = arity (R  S) (R  S) 0  card (R  S)  min (card(R), card(S)) tuples both in R and in S R  (R  S) = R  S SR  R  S  R  R  S  S

Operator Precedence The normal way to group operators is: 1.Unary operators , , and  have highest precedence. 2.Next highest are the “multiplicative” operators,, C, and . 3.Lowest are the “additive” operators, , , and —. But there is no universal agreement, so we always put parentheses around the argument of a unary operator, and it is a good idea to group all binary operators with parentheses enclosing their arguments. Example Group R   S T as R  (  (S ) T ).

Each Expression Needs a Schema If , , — applied, schemas are the same, so use this schema. Projection: use the attributes listed in the projection. Selection: no change in schema. Product R  S: use attributes of R and S. u But if they share an attribute A, prefix it with the relation name, as R.A, S.A. Theta-join: same as product. Natural join: use attributes from each relation; common attributes are merged anyway. Renaming: whatever it says.

Example Find the bars that are either on Maple Street or sell Bud for less than $3. Sells(bar, beer, price) Bars(name, addr)

Example Find the bars that sell two different beers at the same price. Sells(bar, beer, price)

Linear Notation for Expressions Invent new names for intermediate relations, and assign them values that are algebraic expressions. Renaming of attributes implicit in schema of new relation. Example Find the bars that are either on Maple Street or sell Bud for less than $3. Sells(bar, beer, price) Bars(name, addr) R1(name) :=  name (  addr = Maple St. (Bars)) R2(name) :=  bar (  beer=Bud AND price<$3 (Sells)) R3(name) := R1  R2

Why Decomposition “Works”? What does it mean to “work”? Why can’t we just tear sets of attributes apart as we like? Answer: the decomposed relations need to represent the same information as the original. u We must be able to reconstruct the original from the decomposed relations. Projection and Join Connect the Original and Decomposed Relations Suppose R is decomposed into S and T. We project R onto S and onto T.

Example R = Recall we decomposed this relation as:

Project onto Drinkers1 ( name, addr, favoriteBeer ): Project onto Drinkers3(beersLiked, manf): Project onto Drinkers4(name, beersLiked):

Reconstruction of Original Can we figure out the original relation from the decomposed relations? Sometimes, if we natural join the relations. Example Drinkers3 Drinkers4 = Join of above with Drinkers1 = original R.

Theorem Suppose we decompose a relation with schema XYZ into XY and XZ and project the relation for XYZ onto XY and XZ. Then XY XZ is guaranteed to reconstruct XYZ if and only if X  Y (or equivalently, X  Z). Usually, the MVD is really a FD, X  Y or X  Z. BCNF: When we decompose XYZ into XY and XZ, it is because there is a FD X  Y or X  Z that violates BCNF. u Thus, we can always reconstruct XYZ from its projections onto XY and XZ. 4NF: when we decompose XYZ into XY and XZ, it is because there is an MVD X  Y or X  Z that violates 4NF. u Again, we can reconstruct XYZ from its projections onto XY and XZ.

Lossless-Join Decomposition (Section 3.6.5) If R is a relation scheme decomposed into schemes R 1 and R 2 and D is a set of dependencies, we say the decomposition has a lossless join (with respect to D), or is a lossless-join decomposition (with respect to D) if for every relation r of R satisfying D: r = π R1 (r) natural join π R2 (r)

Testing Lossless Joins Algorithm: INPUT: A relation scheme R = A 1.. A n, a set of FD’s F, and a set of decomposed relations (R 1,..,R k ). OUTPUT: A decision whether the decomposition is lossless

Algorithm (Contd.) METHOD: Construct a table with n columns and k rows; column j corresponds to attribute A j, and row I corresponds to relation scheme R i. In row I and column j put the symbol a j if A j is in R i. If not, put the symbol b ij there. Repeatedly consider each dependency X -> Y, look for rows that agree in all columns for the attributes of X. If we find two such rows, equate the symbols of those rows for the attributes of Y. When we equate two symbols, if one of them is a j, make the other be a j. If they are b ij and b lj, make them both b ij or both b lj. If after modifying the rows of the table, we discover that some row has become a 1 …a n, then the join is lossless. If not, the join is lossy.

Theorem If ρ = (R 1, R 2 ) is a decomposition of R, and F is a set of functional dependencies, then ρ has a lossless join with respect to F if and only if (R 1 intersect R 2 ) -> (R 1 - R 2 ) or (R 1 intersect R 2 ) -> (R 2 -R 1 ). Note that these dependencies need not be in the given set F; it is sufficient that they be in F +.

Dependency Preserving Decomposition Decomposition (R 1,..,R k ) preserves a set of dependencies F if the union of all the dependencies in the projection of F onto the R i ’s for i = 1,2,..,k logically implies all the dependencies in F.

Bag Semantics A relation (in SQL, at least) is really a bag or multiset. It may contain the same tuple more than once, although there is no specified order (unlike a list). Example: {1,2,1,3} is a bag and not a set. Select, project, and join work for bags as well as sets. u Just work on a tuple-by-tuple basis, and don't eliminate duplicates.

Bag Union Sum the times an element appears in the two bags. Example: {1,2,1}  {1,2,3,3} = {1,1,1,2,2,3,3}. Bag Intersection Take the minimum of the number of occurrences in each bag. Example: {1,2,1}  {1,2,3,3} = {1,2}. Bag Difference Proper-subtract the number of occurrences in the two bags. Example: {1,2,1} – {1,2,3,3} = {1}.

Laws for Bags Differ From Laws for Sets Some familiar laws continue to hold for bags. u Examples: union and intersection are still commutative and associative. But other laws that hold for sets do not hold for bags. Example R  (S  T)  (R  S)  (R  T) holds for sets. Let R, S, and T each be the bag {1}. Left side: S  T = {1,1}; R  (S  T) = {1}. Right side: R  S = R  T = {1}; (R  S)  (R  T) = {1}  {1} = {1,1}  {1}.

Extended (“Nonclassical”) Relational Algebra Adds features needed for SQL, bags. 1.Duplicate-elimination operator . 2.Extended projection. 3.Sorting operator . 4.Grouping-and-aggregation operator . 5.Outerjoin operator o.

Duplicate Elimination  (R) = relation with one copy of each tuple that appears one or more times in R. Example R = AB  (R) = AB 12 34

Sorting  L (R) = list of tuples of R, ordered according to attributes on list L. Note that result type is outside the normal types (set or bag) for relational algebra. u Consequence:  cannot be followed by other relational operators. Example R =AB  B (R) = [(5,2), (1,3), (3,4)].

Extended Projection Allow the columns in the projection to be functions of one or more columns in the argument relation. Example R =AB  A+B,A,A (R) = A+BA1A

Aggregation Operators These are not relational operators; rather they summarize a column in some way. Five standard operators: Sum, Average, Count, Min, and Max.

Grouping Operator  L (R), where L is a list of elements that are either a)Individual (grouping) attributes or b)Of the form  (A), where  is an aggregation operator and A the attribute to which it is applied, is computed by: 1.Group R according to all the grouping attributes on list L. 2.Within each group, compute  (A), for each element  (A) on list L. 3.Result is the relation whose columns consist of one tuple for each group. The components of that tuple are the values associated with each element of L for that group.

Example Let R = barbeerprice Joe'sBud2.00 Joe'sMiller2.75 Sue'sBud2.50 Sue'sCoors3.00 Mel'sMiller3.25 Compute  beer,AVG(price) (R). 1.Group by the grouping attribute(s), beer in this case: barbeerprice Joe'sBud2.00 Sue'sBud2.50 Joe'sMiller2.75 Mel'sMiller3.25 Sue'sCoors3.00

2.Compute average of price within groups: beerAVG(price) Bud2.25 Miller3.00 Coors3.00

Outerjoin The normal join can “lose” information, because a tuple that doesn’t join with any from the other relation (dangles) has no vestage in the join result. The null value  can be used to “pad” dangling tuples so they appear in the join. Gives us the outerjoin operator o. Variations: theta-outerjoin, left- and right- outerjoin (pad only dangling tuples from the left (respectively, right).

Example R =AB S =BC R o S =ABC 345part of natural join 12  part of right-outerjoin  67part of left-outerjoin