1 Session 3 Welcome: To session 3-the fifth learning sequence “ Relational algebra “ Recap : In the previous learning sequences, we discussed the eight operators of relational algebra. Present learning: We shall explore the following topic: - Another operators.
2 Relational Algebra
3
4
uTwo basic operators wDivision wNatural-Join Operation uThe operators take two or more relations as inputs and give a new relation as a result.
Division Operation uSuited to queries that include the phrase “for all”. uLet r and s be relations on schemas R and S respectively where wR = (A 1, …, A m, B 1, …, B n ) wS = (B 1, …, B n ) The result of r s is a relation on schema R – S = (A 1, …, A m ) r s = { t | t R-S (r) u s ( tu r ) } r s
Division Operation – Example Relations r, s: r s:r s: A B 1212 AB r s
Another Division Example AB aaaaaaaaaaaaaaaa CD aabababbaabababb E Relations r, s: r s:r s: D abab E 1111 AB aaaa C r s
n Notation: r s Natural-Join Operation uLet r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: wConsider each pair of tuples t r from r and t s from s. wIf 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
n Notation: r s Natural-Join Operation uExample: R = (A, B, C, D) S = (E, B, D) wResult schema = (A, B, C, D, E) wr 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))
Natural Join Operation – Example uRelations r, s: AB CD aababaabab B D aaabbaaabb E r AB CD aaaabaaaab E s r s
Outer Join uAn extension of the join operation that avoids loss of information. uComputes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join. uUses null values: wnull signifies that the value is unknown or does not exist wAll comparisons involving null are (roughly speaking) false by definition. Will study precise meaning of comparisons with nulls later
Outer Join – Example uRelation loan n Relation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L loan-numberamount L-170 L-230 L-260 branch-name Downtown Redwood Perryridge
Outer Join – Example uInner Join loan Borrower loan-numberamount L-170 L customer-name Jones Smith branch-name Downtown Redwood Jones Smith null loan-numberamount L-170 L-230 L customer-namebranch-name Downtown Redwood Perryridge n Left Outer Join loan Borrower
Outer Join – Example uRight Outer Join loan borrower Loan borrower n Full Outer Join loan-numberamount L-170 L-230 L null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-numberamount L-170 L-230 L-260 L null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null
Example Queries (1) uFind all customers who have an account from at least the “Downtown” and the Uptown” branches. where CN denotes customer-name and BN denotes branch-name. Query 1 CN ( BN=“Downtown ” (depositor account)) CN ( BN=“Uptown ” (depositor account))
Example Queries (2) uFind all customers who have an account from at least the “Downtown” and the Uptown” branches. Query 2 customer-name, branch-name (depositor account) temp(branch-name ) ({(“Downtown”), (“Uptown”)})
uFind all customers who have an account at all branches located in Brooklyn city. Example Queries (3) customer-name, branch-name (depositor account) branch-name ( branch-city = “Brooklyn” (branch))
19 Relational Algebra Summary: In this learning sequence, we discussed another basic operators of the topic relational algebra.
20 END