Download presentation
Presentation is loading. Please wait.
Published byRaymond Farmer Modified over 8 years ago
1
1 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.
2
2 Relational Algebra
3
uFour basic operators wset difference wCartesian product wrename wassignment uThe operators take one or more relations as inputs and give a new relation as a result.
4
Set Difference Operation uNotation r – s uDefined as: r – s = {t | t r and t s} uSet differences must be taken between compatible relations. wr and s must have the same arity. wattribute domains of r and s must be compatible.
5
5 Set Difference Operation – Example For example, find all customers who have an account and haven't a loan. u customer-name (depositor) - customer-name (borrower) Customer -name Accou nt-no. AliA-101 MahmoodA-201 AhmidA-217 LindaA-222 RanaA-305 Customer- name Loan- no. AliL-11 KasimL-11 AhmidL-25 LindaL-11 RanaL-34 depositor borrower
6
Set Difference Operation – Example u customer-name (depositor) customer-name (borrower) Customer- name Ali Mahmood Ahmid Linda Rana …. Customer -name Ali Kasim Ahmid Linda Rana …..
7
Set Difference Operation – Example uThe result relation is: Customer- name Mahmood
8
8 Set Difference Operation – Example For example, find all customers who have a loan, but they haven't an account. u customer-name (borrower) - customer-name (depositor) Customer- name Account- no. AliA-101 MahmoodA-201 AhmidA-217 LindaA-222 RanaA-305 Customer- name Loan- no. AliL-11 KasimL-11 AhmidL-25 LindaL-11 RanaL-34 borrowerdepositor
9
Set Difference Operation – Example u customer-name (borrower) customer-name (depositor) Customer- name Ali Mahmood Ahmid Linda Rana …. Customer -name Ali Kasim Ahmid Linda Rana …..
10
Set Difference Operation – Example uThe result relation is: Customer- name Kasim
11
Cartesian-Product Operation uNotation r x s uDefined as: r x s = {t q | t r and q s} uAssume that attributes of r(R) and s(S) are disjoint. (That is, R S = ). uIf attributes of r(R) and s(S) are not disjoint, then renaming must be used.
12
Cartesian-Product Operation Relations r and s : AB a11 a22 CDE c1d11 c2d22 c3d33 c4d42 r s
13
13 Cartesian-Product Operation The relation r x s is: ABCDE a11c1d11 a11c2d22 a11c3d33 a11c4d42 a22c1d11 a22c2d22 a22c3d33 a22c4d42
14
Composition of Operations uCan build expressions using multiple operations uExample: B=E (r x s) ABCDE a11c1d11 a22c2d22 a22c4d42
15
Rename Operation uAllows us to name, and therefore to refer to, the results of relational-algebra expressions. uAllows 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.
16
Rename Operation uR d (account) d
17
Assignment Operation uThe assignment operation ( ) provides a convenient way to express complex queries. w 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. wAssignment must always be made to a temporary relation variable.
18
Assignment Operation uExample: Write u customer-name (borrower) - customer-name (depositor) temp1 customer-name (borrower) temp2 customer-name (depositor) result = temp1 – temp2 wThe result to the right of the is assigned to the relation variable on the left of the . wMay use variable in subsequent expressions.
19
19 Relational Algebra Summary: In this learning sequence, we discussed another four basic operators of the topic relational algebra.
20
20 END
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.