Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra.

Similar presentations


Presentation on theme: "Relational Algebra."— Presentation transcript:

1 Relational Algebra

2 Relational Query Languages
Recall: Query = “Retrieval Program” Language Examples: Theoretical: Relational Algebra Relational Calculus Tuple Relational Calculus (TRC) Domain Relational Calculus (DRC) Practical: SQL (originally: SEQUEL from System R) Quel (used in Ingres) Datalog (Prolog-like – used in research lab systems) Theoretical QLs give semantics to Practical QLs CSCI1270, Lecture 2

3 Relational Algebra Basic Operators select ( σ ) project ( p )
union (  ) set difference ( – ) cartesian product (  ) rename ( ρ ) Closure Property Relation Relational Operator Relation Relational Operator Relation CSCI1270, Lecture 2

4 Select ( σ ) Notation: σpredicate (Relation)
Relation: Can be name of table or result of another query Predicate: Simple attribute1 = attribute2 attribute = constant value (also: ≠, <, >, ≤, ≥) 2. Complex predicate AND predicate predicate OR predicate NOT predicate Idea: Select rows from a relation based on a predicate CSCI1270, Lecture 2

5 Bank Database Account bname acct_no balance Branch bname bcity assets
Downtown Mianus Perry R.H. Brighton Redwood A-101 A-215 A-102 A-305 A-201 A-222 A-217 500 700 400 350 900 750 Branch bname bcity assets Downtown Redwood Perry Mianus R.H. Pownel N. Town Brighton Brooklyn Palo Alto Horseneck Bennington Rye 9M 2.1M 1.7M 0.4M 8M 0.3M 3.7M 7.1M Depositor cname acct_no Johnson Smith Hayes Turner Jones Lindsay A-101 A-215 A-102 A-305 A-201 A-217 A-222 Borrower cname lno Jones Smith Hayes Jackson Curry Williams Adams L-17 L-23 L-15 L-14 L-93 L-11 L-16 Customer cname cstreet ccity Jones Smith Hayes Curry Lindsay Turner Williams Adams Johnson Glenn Brooks Green Main North Park Putnam Nassau Spring Alma Sand Hill Senator Walnut Harrison Rye Pittsfield Stanford Princeton Palo Alto Woodside Brooklyn Loan bname lno amt Downtown Redwood Perry Mianus R.H. L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300 CSCI1270, Lecture 2

6 Select ( σ ) Notation: σpredicate (Relation)
bname bcity assets Downtown Brighton Brooklyn 9M 7.1M σ bcity = “Brooklyn” (branch) = σ assets > $8M (σ bcity = “Brooklyn” (branch)) = bname bcity assets Downtown Brooklyn 9M CSCI1270, Lecture 2

7 Project ( p ) Notation: pA1, …, An (Relation)
Relation: name of a table or result of another query Each Ai is an attribute Idea: p selects columns (vs. σ which selects rows) p cstreet, ccity (customer) = cstreet ccity Main North Park Putnam Nassau Spring Alma Sand Hill Senator Walnut Harrison Rye Pittsfield Stanford Princeton Palo Alto Woodside Brooklyn CSCI1270, Lecture 2

8 Project ( p ) p bcity (σassets > 5M (branch)) = bcity Brooklyn
Horseneck Question: Does the result of Project always have the same number of tuples as its input? CSCI1270, Lecture 2

9 Union (  ) Notation: Relation1  Relation2 Example:
R  S valid only if: R, S have same number of columns (arity) R, S corresponding columns have same name and domain (compatibility) Example: cname Johnson Smith Hayes Turner Jones Lindsay Jackson Curry Williams Adams (p cname (depositor))  (p cname (borrower)) = Schema: Depositor cname acct_no Borrower cname lno CSCI1270, Lecture 2

10 Set Difference ( – ) Notation: Relation1 - Relation2 Example:
R - S valid only if: R, S have same number of columns (arity) R, S corresponding columns have same domain (compatibility) Example: (p bname (σamount ≥ 1000 (loan))) – (p bname (σ balance < 800 (account))) = bname lno amount Downtown Redwood Perry L-17 L-23 L-15 L-14 L-16 1000 2000 1500 500 300 bname acct_no balance Mianus Brighton Redwood A-215 A-201 A-222 A-217 700 900 850 bname Downtown Perry CSCI1270, Lecture 2

11 What About Intersection?
Remember: R ⋂ S = R – (R – S) R – S R S

12 Cartesian Product (  ) Notation: Relation1  Relation2 Example:
R  S like cross product for mathematical relations: every tuple of R appended to every tuple of S flattened!!! Example: depositor  borrower = depositor. cname acct_no borrower. lno Johnson Smith A-101 A-215 Jones Hayes Jackson Curry Williams Adams L-17 L-23 L-15 L-14 L-93 L-11 L-16 How many tuples in the result? A: 56 CSCI1270, Lecture 2

13 Rename ( ρ ) Notation: r identifier (Relation)
renames a relation, or Notation: r identifier0 (identifier1, …, identifiern) (Relation) renames relation and columns of n-column relation Use: massage relations to make , – valid, or  more readable CSCI1270, Lecture 2

14 Rename ( ρ ) Notation: r identifier0 (identifier1, …, identifiern) (Relation) Example: r result (dcname, acctno, bcname, lno) (depositor  borrower) = result dccname acctno bcname lno Johnson Smith A-101 A-215 Jones Hayes Jackson Curry Williams Adams L-17 L-23 L-15 L-14 L-93 L-11 L-16 CSCI1270, Lecture 2

15 Example Query in RA Determine lno for loans that are for an amount that is larger than the amount of some other loan. (i.e. lno for all non-minimal loans) Can do in steps: Temp1  … Temp2  … Temp1 … CSCI1270, Fall 2008, Lecture 2

16 Example Query in RA 1. Find the base data we need
lno amt L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300 Temp1  p lno,amt (loan) 2. Make a copy of (1) Temp2  ρ Temp2 (lno2,amt2) (Temp1) lno2 amt2 L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300 CSCI1270, Lecture 2

17 Example Query in RA 3. Take the cartesian product of 1 and 2
Temp3  Temp1  Temp2 lno amt lno2 amt2 L-17 L-23 1000 2000 L-16 1300 CSCI1270, Lecture 2

18 Example Query in RA p lno (σamt > amt2 (p lno,amt (loan)  (
4. Select non-minimal loans Temp4  σamt > amt2 (Temp3) 5. Project on lno Result  p lno (Temp4) … or, if you prefer… p lno (σamt > amt2 (p lno,amt (loan)  ( ρTemp2 (lno2,amt2) (p lno,amt (loan))))) CSCI1270, Fall 2008, Lecture 2

19 Review Theoretical Query Languages
Relational Algebra SELECT ( σ ) PROJECT ( π ) UNION (  ) SET DIFFERENCE ( – ) CARTESIAN PRODUCT (  ) RENAME ( ρ ) Relational algebra gives semantics to practical query languages Above set: minimal relational algebra  will now look at some redundant (but useful!) operators CSCI1270, Fall 2008, Lecture 2

20 Review Express the following query in the RA:
Find the names of customers who have both accounts and loans T1  ρT1 (cname2, lno) (borrower) T2  depositor  T1 T3  σcname = cname2 (T2) Result  π cname (T3) Above sequence of operators (ρ, , σ) very common. Motivates additional (redundant) RA operators. CSCI1270, Lecture 2

21 Relational Algebra Additional Operators
Natural Join ( ) 2. Division ( ) Generalized Projection (π) Aggregation 5. Outer Joins ( ) 6. Update (  ) (we’ve already been using this) 1&2 Redundant: Can be expressed in terms of minimal RA e.g. depositor borrower = π …(σ…(depositor  ρ…(borrower))) 3 – 6 Added for extra power CSCI1270, Lecture 2

22 Natural Join Relation1 Notation: Relation2 Idea: combines ρ, , σ r s
3 α Β β + - 10 20 ‘a’ ‘b’ ‘c’ A B C D 1 2 3 α β + - 10 20 E B D ‘a’ ‘b’ ‘c’ α β 10 20 = r s πcname,acct_no,lno (σcname=cname2 (depositor  ρt(cname2,lno) (borrower))) depositor borrower CSCI1270, Lecture 2

23 Division Notation: Relation1 Relation2
Idea: expresses “for all” queries r A B α β γ δ 1 23 3 4 6 2 B 1 2 s A α δ = Query: Find values for A in r which have corresponding B values for all B values in s CSCI1270, Lecture 2

24 Division ¸ ´ Another way to look at it: and Relational Division
17 ¸ 3 = 5 The largest value of i such that: i ´ 3 ≤ 17 Relational Division r A B α β γ δ 1 23 3 4 6 2 s B 1 2 t A α δ = The largest value of t such that: ( t  s Í r ) CSCI1270, Lecture 2

25 Division A More Complex Example r t s ? A B C D E α β γ a b 1 3 A B C
11 = ? CSCI1270, Lecture 2

26 Division Adds No Power Definition in terms of the basic algebra operation Let r(R) and s(S) be relations, and let S  R r  s = R-S (r) – R-S ( (R-S (r) x s) – R-S,S(r)) To see why R-S,S(r) simply reorders attributes of r R-S(R-S (r) x s) – R-S,S(r)) gives those tuples t in R-S (r) such that for some tuple u  s, tu  r.

27 Generalized Projection
Notation: p e1,…,en (Relation) e1,…,en can include arithmetic expressions – not just attributes Example cname limit balance Jones Turner 5000 3000 2000 2500 credit = Then… cname limit-balance Jones Turner 3000 500 π cname, limit - balance (credit) = CSCI1270, Lecture 2

28 Aggregate Functions and Operations
An aggregate function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E ) E is any relational-algebra expression G1, G2 …, Gn is a list of attributes on which to group (can be empty) Each Fi is an aggregate function Each Ai is an attribute name CSCI1270, Lecture 2

29 Aggregate Operation – Example
B C Relation r: 7 3 10 sum-C g sum(c) (r) No grouping 27 CSCI1270, Lecture 2

30 Aggregate Operation – Example
Relation account grouped by branch-name: branch-name account-number balance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-name g sum(balance) (account) branch-name sum(balance) Perryridge Brighton Redwood 1300 1500 700 CSCI1270, Lecture 2

31 Aggregate Functions (Cont.)
Result of aggregation does not have a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account) CSCI1270, Lecture 2


Download ppt "Relational Algebra."

Similar presentations


Ads by Google