Download presentation
Presentation is loading. Please wait.
Published byBathsheba Price Modified over 8 years ago
1
ASET Relational Algebra continues…
2
ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name. Example: x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2, …., A n.
3
ASET Composition of Operations Can build expressions using multiple operations Example: A=C (r x s) r x s A=C (r x s) AB 1111222211112222 CD 10 20 10 20 10 E aabbaabbaabbaabb ABCDE 122122 20 aabaab
4
ASET Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number)
5
ASET Example Queries Find all loans of over $1200 n Find the loan number for each loan of an amount greater than $1200 amount > 1200 (loan) loan_number ( amount > 1200 (loan)) n Find the names of all customers who have a loan, an account, or both, from the bank customer_name (borrower) customer_name (depositor)
6
ASET Example Queries Find the names of all customers who have a loan at the Perryridge branch. n Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank. customer_name ( branch_name = “Perryridge” ( borrower.loan_number = loan.loan_number (borrower x loan))) – customer_name (depositor) customer_name ( branch_name=“Perryridge ” ( borrower.loan_number = loan.loan_number (borrower x loan)))
7
ASET Additional Operations – Set intersection – Natural join – Aggregation – Outer Join – Division All above, other than aggregation, can be expressed using basic operations we have seen earlier
8
ASET Set-Intersection Operation – Example Relation r, s: r s A B 121121 2323 rs 2
9
ASET Natural Join Operation – Example Relations r, s: AB 1241212412 CD aababaabab B 1312313123 D aaabbaaabb E r AB 1111211112 CD aaaabaaaab E s n r s
10
ASET n Notation: r s Natural-Join Operation Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: – Consider each pair of tuples t r from r and t s from s. – If t r and t s have the same value on each of the attributes in R S, add a tuple t to the result, where t has the same value as t r on r t has the same value as t s on s Example: R = (A, B, C, D) S = (E, B, D) – Result schema = (A, B, C, D, E) – r s is defined as: r.A, r.B, r.C, r.D, s.E ( r.B = s.B r.D = s.D (r x s))
11
ASET Aggregate Functions and Operations Aggregation 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 E is any relational-algebra expression – G 1, G 2 …, G n is a list of attributes on which to group – Each F i is an aggregate function – Each A i is an attribute name
12
ASET Aggregate Operation – Example Relation account grouped by branch-name: branch_name g sum( balance ) ( account ) branch_nameaccount_numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch_namesum(balance) Perryridge Brighton Redwood 1300 1500 700
13
ASET 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 )
14
ASET Q1. Consider the following relational database, where the primary keys are underlined. Give an expression in the relational algebra to express each of the following queries: employee( person_name, street, city) Works(person_name, company_name, salary) company( company_name, city) manages(person_name, manager_name)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.