280 likes | 422 Views
Relational Algebra. Overview. Relational algebra offers a concise way to express queries. F orm the basis for “real” query languages (SQL) . Much more concise than SQL. It is widely used by database professionals. Basic operations. Additional operations. Example i nstances. R1.
E N D
Overview • Relational algebra offers a concise way to express queries. • Form the basis for “real” query languages (SQL). • Much more concise than SQL. • It is widely used by database professionals. • Basic operations. • Additional operations.
Example instances R1 • “Sailors” and “Reserves” relations for our examples. S1 S2
Basic operations • Selection • Projection • Union • Difference • Cartesian product • Rename
Projection S2 Duplicate rows removed This is true for all the relational algebra operations.
Union S1 S2 • Two input relations must be union-compatible • Same number of attributes. • `Corresponding’ attributes have the same type.
Cartesian Product • Each row of S1 is paired with each row of R1. S1 R1
Renaming S1 My-table(id, name, level, age) (S1) My-table
Example 1 • ACC(acc-id, cust-id, balance) • Find all accounts with balances > 1200. • balance > 1200 (ACC) • Find the acc-id of all accounts with balances > 1200. • acc-id (balance> 1200 (ACC)) • Find the acc-id of all accounts with balances > 1200 and cust-id = 100. • acc-id (balance> 1200 cust-id = 100 (ACC)) • Find the acc-id of all accounts with balances > 1200 or cust-id = 100. • acc-id (balance> 1200 cust-id = 100 (ACC))
Example 2 • ACC(acc-id, cust-id, balance) • LOAN(loan-id, cust-id, amount) • Find the cust-id of customers who have both loans or accounts. • cust-id (ACC) cust-id (LOAN) • LOAN(loan-id, cust-id, amount) • CUST(cust-id, name, address) • Find the names of customers who have loans • name(σLOAN.cust-id = CUST.cust-id(LOAN CUST))
Overview • Relational algebra offers a concise way to express queries. • Form the basis for “real” query languages (SQL). • Much more concise than SQL. • It is widely used by database professionals. • Basic operations. • Additional operations.
Additional operations • Natural join • Division • These operations can be transformed to basic operations. • They do not add any power to relational algebra, but can simplify queries.
Natural join r s • R = (A, B, C, D), S = (E, B, D) • Equal on all common attributes • r s = r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s)) • Result schema = (A, B, C, D, E) B D E A B C D 1 3 1 2 3 a a a b b 1 2 4 1 2 a a b a b r s A B C D E 1 1 1 1 2 a a a a b
Example 2 revisited • LOAN(loan-id, cust-id, amount) • CUST(cust-id, name, address) • Find the names of customers who have loans. • name(σLOAN.cust-id = CUST.cust-id(LOAN CUST)) • = • name (LOAN CUST)
Division (1/3) A B B r s A 1 2 1 2 3 1 1 1 3 4 6 1 2 s r
Division (2/3) • r = (A1, …, Am, B1, …, Bn) • s = (B1, …, Bn) • r s has a schema (A1, …, Am). • If (a1, …, am) is in r s, then • for every tuple (b1, …, bn) in s, (a1, …, am, b1, …, bn) exists in r.
Division (3/3) A B C D E D E a a a a a a a a a a b a b a b b 1 1 1 1 3 1 1 1 a b 1 1 s r A B C r s a a
Example 3 • BRANCH(brh-name, city) • ACC(acc-id, cust-name, brh-name) • Assume that no two customers have the same name. • Find all customers who have accounts at all branches in HK. • A wrong solution • cust-name ((ACC brh-name(city = ‘HK’(BRANCH))) • A correct solution • cust-name, brh-name(ACC) brh-name(city = ‘HK’(BRANCH))
Example Database ACC acc-id cust-name brh-name BRANCH brh-name city A1 A2 A3 A4 A5 A6 A7 A8 Joey Joey Jay Jay Eason Eason Twins Twins B1 B2 B1 B3 B1 B4 B1 B2 B1 B2 B3 B4 HK HK BJ BJ brh-name(city = ‘HK’(BRANCH)) cust-name brh-name cust-name, brh-name(ACC) brh-name Joey Joey Jay Jay Eason Eason Twins Twins B1 B2 B1 B3 B1 B4 B1 B2 B1 B2
Example 3 • BRANCH(brh-name, city) • ACC(acc-id, cust-name, brh-name) • Find all customers who have accounts at all branches in HK. • SQL solution (more complex; to be explained later on): • SELECT DISTINCTcust-nameFROM ACC A1WHERE NOT EXISTS ((SELECT brh-name FROM BRANCH WHERE city = ‘HK’) EXCEPT (SELECT brh-name FROM ACC A2 WHERE A1.cust-name = A2.cust-name))