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.

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.
SQL Queries Principal form: SELECT desired attributes FROM tuple variables –– range over relations WHERE condition about tuple variables; Running example.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
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 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.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
1 Relational Algebra Basic operators Relational algebra expression tree.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
1 Relational Algebra Operators Expression Trees Bag Model of Data.
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.
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.
Relational Algebra (Chapter 7)
1 Relational Algebra Basic Operations Algebra of Bags.
“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,
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.
Database Management Systems Chapter 5 The Relational Algebra Instructor: Li Ma Department of Computer Science Texas Southern University, Houston October,
1 Lecture 2 Relational Algebra Based on
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.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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:
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
Basic Operations Algebra of Bags
CPSC-310 Database Systems
Schedule Today: Jan. 28 (Mon) Jan. 30 (Wed) Next Week Assignments !!
Slides are reused by the approval of Jeffrey Ullman’s
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Schedule Today: Jan. 23 (wed) Week of Jan 28
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.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

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 I. Next Week u SQL Queries, Subqueries, Grouping and Aggregation. u Read Sections u Assignment 2 due Tuesday. u Assignment 3 (due Monday 2/4) u PDA 3 due Thursday 5pm. u Modifications, Schemas, Views. u Read Sections Assignment 3 due.

Winter 2002Judy Cushing5–2 “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.

Winter 2002Judy Cushing5–3 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

Winter 2002Judy Cushing5–4 Extra Example Relations DEPOSIT(branch-name, acct-no,cust-name,balance) CUSTOMER(cust-name,street,cust-city) BORROW(branch-name,loan-no,cust-name,amount) BRANCH(branch-name,assets, branch-city) CLIENT(cust-name,empl-name) Borrow B-N L-# C-N AMT T1 Midtown 123 Fred 600 T2 Midtown 234 Sally 1200 T3 Midtown 235 Sally 1500 T4 Downtown 612 Tom 2000

Winter 2002Judy Cushing5–5 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 )

Winter 2002Judy Cushing5–6 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   branch-name = "Midtown"  amount > 1000 (Borrow)  cust-name = emp-name (client)

Winter 2002Judy Cushing5–7 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.

Winter 2002Judy Cushing5–8 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 π branch-name, cust-name  (Borrow) Midtown Fred Midtown Sally Downtown Tom

Winter 2002Judy Cushing5–9 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.

Winter 2002Judy Cushing5–10 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

Winter 2002Judy Cushing5–11 Theta-Join R = R 1 C R 2 is equivalent to R =  C (R 1  R 2 ).

Winter 2002Judy Cushing5–12 Example Sells = Bars = BarInfo = Sells Sells.Bar=Bars.Name Bars

Winter 2002Judy Cushing5–13 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)

Winter 2002Judy Cushing5–14 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

Winter 2002Judy Cushing5–15 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.

Winter 2002Judy Cushing5–16 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) )

Winter 2002Judy Cushing5–17 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 π Cust-Name (  Branch-Name = "Perryridge" ( DEPOSIT – BORROW) ) Deposit Borrow π Cust-Name (  Branch-Name = "Perryridge" ( DEPOSIT) ) — π Cust-Name (  Branch-Name = "Perryridge" ( BORROW) ) Does  (π (D)  π (B) ) work?

Winter 2002Judy Cushing5–18 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.

Winter 2002Judy Cushing5–19 π Cust-Name,Cust-City (  CLIENT.Banker-Name = "Johnson" (CLIENT  CUSTOMER) ) = π Cust-Name,Cust-City (CUSTOMER) Is this always true? π CLIENT.Cust-Name, CUSTOMER.Cust-City (  CLIENT.Banker-Name = "Johnson"  CLIENT.Cust-Name = CUSTOMER.Cust-Name (CLIENT  CUSTOMER) ) π CLIENT.Cust-Name, CUSTOMER  Cust-City (  CLIENT.Cust-Name =CUSTOMER.Cust-Name (CUSTOMER  π Cust-Name  CLIENT.Banker-Name="Johnson" (CLIENT) ) ) )

Winter 2002Judy Cushing5–20 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

Winter 2002Judy Cushing5–21 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 ).

Winter 2002Judy Cushing5–22 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.

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

Winter 2002Judy Cushing5–24 Example Find the bars that sell two different beers at the same price. Sells(bar, beer, price)

Winter 2002Judy Cushing5–25 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

Winter 2002Judy Cushing5–26 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.

Winter 2002Judy Cushing5–27 Example R = Recall we decomposed this relation as:

Winter 2002Judy Cushing5–28 Project onto Drinkers1 ( name, addr, favoriteBeer ): Project onto Drinkers3(beersLiked, manf): Project onto Drinkers4(name, beersLiked):

Winter 2002Judy Cushing5–29 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.

Winter 2002Judy Cushing5–30 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.

Winter 2002Judy Cushing5–31 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.

Winter 2002Judy Cushing5–32 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}.

Winter 2002Judy Cushing5–33 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}.

Winter 2002Judy Cushing5–34 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.

Winter 2002Judy Cushing5–35 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

Winter 2002Judy Cushing5–36 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)].

Winter 2002Judy Cushing5–37 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

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

Winter 2002Judy Cushing5–39 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.

Winter 2002Judy Cushing5–40 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

Winter 2002Judy Cushing5–41 2.Compute average of price within groups: beerAVG(price) Bud2.25 Miller3.00 Coors3.00

Winter 2002Judy Cushing5–42 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).

Winter 2002Judy Cushing5–43 Example R =AB S =BC R o S =ABC 345part of natural join 12  part of right-outerjoin  67part of left-outerjoin