Session 3 Welcome: To session 3-the third learning sequence “ Relational algebra “ Recap : In the previous learning sequences, we discussed the four operators of relational algebra. Present learning: We shall explore the following topic: - Another four operators. 1 Relational Algebra 2 Relational Algebra Four basic operators set difference Cartesian product rename assignment The operators take one or more relations as inputs and give a new relation as a result. Set Difference Operation Notation r – s Defined as: r – s = {t | t r and t s} Set differences must be taken between compatible relations. r and s must have the same arity. attribute domains of r and s must be compatible. Set Difference Operation – Example For example, find all customers who have an account and haven't a loan. customer-name (depositor) - customer-name (borrower) Accou nt-no. A-101 A-201 A-217 A-222 A-305 borrower depositor Customer -name Ali Mahmood Ahmid Linda Rana Customer- Loanname no. Ali L-11 Kasim L-11 Ahmid L-25 Linda L-11 5 Rana L-34 Set Difference Operation – Example customer-name (depositor) Customername Ali Mahmood Ahmid Linda Rana …. customer-name (borrower) Customer -name Ali Kasim Ahmid Linda Rana ….. Set Difference Operation – Example The result relation is: Customername Mahmood Set Difference Operation – Example For example, find all customers who have a loan, but they haven't an account. customer-name (borrower) - customer-name (depositor) depositor borrower Customer- Loanname no. Ali L-11 Kasim L-11 Ahmid L-25 Linda L-11 Rana L-34 Customer- Accountname no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 8 Set Difference Operation – Example customer-name (borrower) Customer -name Ali Kasim Ahmid Linda Rana ….. customer-name (depositor) Customername Ali Mahmood Ahmid Linda Rana …. Set Difference Operation – Example The result relation is: Customername Kasim Cartesian-Product Operation Notation r x s Defined as: r x s = {t q | t r and q s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R S = ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used. Cartesian-Product Operation Relations r and s : r A B a1 1 a2 2 s C D E c1 d1 1 c2 d2 2 c3 d3 3 c4 d4 2 Cartesian-Product Operation The relation r x s is: A a1 a1 a1 a1 a2 a2 a2 a2 B 1 1 1 1 2 2 2 2 C c1 c2 c3 c4 c1 c2 c3 c4 D d1 d2 d3 d4 d1 d2 d3 d4 E 1 2 3 2 1 2 3 2 13 Composition of Operations Can build expressions using multiple operations Example: B=E(r x s) A B C D E a1 a2 a2 1 2 2 c1 c2 c4 d1 d2 d4 1 2 2 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 x (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the attributes renamed to A1, A2, …., An. Rename Operation R d (account) d Assignment Operation The assignment operation () provides a convenient way to express complex queries. Write query as a sequential program consisting of • a series of assignments. • followed by an expression whose value is displayed as a result of the query. Assignment must always be made to a temporary relation variable. Assignment Operation Example: Write customer-name (borrower)-customer-name (depositor) temp1 customer-name (borrower) temp2 customer-name (depositor) result = temp1 – temp2 The result to the right of the is assigned to the relation variable on the left of the . May use variable in subsequent expressions. Relational Algebra Summary: In this learning sequence, we discussed another four basic operators of the topic relational algebra. 19 END 20