Presentation is loading. Please wait.

Presentation is loading. Please wait.

SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections 6.1-6.2. And then u Subqueries,

Similar presentations


Presentation on theme: "SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections 6.1-6.2. And then u Subqueries,"— Presentation transcript:

1 SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections 6.1-6.2. And then u Subqueries, Grouping and Aggregation. u Read Sections 6.3-6.4.

2 SCUHolliday - coen 1785–2 Relational Algebra The relational algebra is a precise mathematical notation and set of rules for manipulating “relations”. SQL is basically a more human readable form of the relational algebra.

3 SCUHolliday - coen 1785–3 “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.

4 SCUHolliday - coen 1785–4 Relational Algebra  SELECT π PROJECT X CARTESIAN PRODUCT NATURAL JOIN

5 SCUHolliday - coen 1785–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 )

6 SCUHolliday - coen 1785–6 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.

7 SCUHolliday - coen 1785–7 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

8 SCUHolliday - coen 1785–8 Natural-Join R = R 1 R 2 is equivalent to R =  C (R 1  R 2 ) where c is the condition that the the values of the attributes that R1 and R2 have in common must match.

9 SCUHolliday - coen 1785–9 Example Sells = Bars = BarInfo = Sells Bars

10 SCUHolliday - coen 1785–10 Combining Operations Algebra = 1.Basis arguments or domain + 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.

11 SCUHolliday - coen 1785–11 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.

12 SCUHolliday - coen 1785–12 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}.

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

14 SCUHolliday - coen 1785–14 Bank Database Schema Branch = (branch-name, branch-city, assets) Customer = (customer-name, customer-street, customer-city) Account = (branch-name, account#, balance) Depositor = (customer-name, account#) Loan = (branch-name, loan#, amount) Borrower = (customer-name, loan#)

15 SCUHolliday - coen 1785–15 The Customer Table Customer-nameC-StreetC-city Bob123 Third StSan Jose Carol456 Main StSanta Clara Ted89 Blossom AveLos Gatos Alice64 Longwalk DrOakland

16 SCUHolliday - coen 1785–16 The Account Table Branch-nameAccount#Balance Oakland1012000 SJ-Main2057500 SJ-Main2074500 Santa Clara3113100 SJ-West251850

17 SCUHolliday - coen 1785–17 The Loan Table Branch-nameloan#Amount Oakland23015000 SJ-Main5155700 SJ-Main57099000 Santa Clara15411800 SJ-West4321250

18 SCUHolliday - coen 1785–18 The Depositor Table Customer-nameAccount# Bob207 Carol311 Ted205 Alice101 Bob251

19 SCUHolliday - coen 1785–19 Queries on the Loan Table Loan = (branch-name, loan#, amount) Find the names of all the branches in the Loan relation select branch-name from Loan  (branch-name) (Loan) Branch-name Oakland SJ-Main Santa Clara SJ-West

20 SCUHolliday - coen 1785–20 More Queries on the Loan Table select * from Loan where amount > 3000  (amount>3000) (Loan)

21 SCUHolliday - coen 1785–21 Find the loan numbers for all loans made at the Oakland branch with loan amounts greater than 1200. select loan# from Loan where branch-name="Oakland" and amount>1200  (loan#)  (branch-name=“Oakland”) (Loan)

22 SCUHolliday - coen 1785–22 Cross Product Select A1, A2 from R1, R2 Result 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.  (A1, A2) (R1  R2)

23 SCUHolliday - coen 1785–23 Natural Join Find the name of customers with an account at the Oakland branch. select customer-name from Depositor, Account where Depositor.account# = Account.account# and branch-name = "Oakland"  (cust-name)  (b-name=“Oakland”) (Depositor Account)


Download ppt "SCUHolliday - coen 1785–1 Schedule Today: u Relational Algebra. u Read Chapter 5 to page 199. Next u SQL Queries. u Read Sections 6.1-6.2. And then u Subqueries,"

Similar presentations


Ads by Google