Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra.

Similar presentations


Presentation on theme: "Relational Algebra."— Presentation transcript:

1 Relational Algebra

2 Query Languages Language in which user requests information from the database. Pure languages form underlying basis of query languages that people use.

3 Relational Algebra Basic operators
Select Project Union Intersection Set difference Cartesian product Rename Assignment Join Division Generalized Project Aggregate functions The operators take one or more relations as inputs and give a new relation as a result.

4 Select Operation Notation:  p(r) p is called the selection predicate
Example of selection:  branch-name=“Perryridge” (account)

5 Select Operation – Example
Account

6 Select Operation – Example
The result is the relation: Account-number Branch-name balance A-101 perryridge 400 6

7 Select Operation – Example
 Balance >“700” (account) Account-number Branch-name balance A-201 Brighton 900 A-217 750 7

8 Project Operation Notation: A1, A2, …, Ak (r)
where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed

9 Project Operation E.g. To eliminate the branch-name attribute of account account-number, balance (account) The result relation is:

10 Project Operation Account-number Balance A-101 500 A102 400 A-201 900
700 A-217 750 A-222 A-305 350 10

11 Union Operation Notation: r  s Defined as:
r  s = {t | t  r or t  s} For r  s to be valid. 1. r, s must have the same number of attributes 2. The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values as does the 2nd column of s)

12 Union Operation E.g. to find all customers with either an account or a loan customer-name (depositor)  customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower

13 Union Operation Customer-name Ali Mahmood Ahmid Linda Rana ….
customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..

14 Union Operation Customer-name Ali Mahmood Ahmid Linda Rana Kasim
The result relation is: Customer-name Ali Mahmood Ahmid Linda Rana Kasim

15 Intersection Operation
Notation: r  s Defined as: r  s ={ t | t  r and t  s } Assume: r, s have the same number of attributes attributes of r and s are compatible

16 Intersection Operation
E.g. to find all customers with an account and a loan customer-name (depositor)  customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower

17 Intersection Operation
customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..

18 Intersection Operation
The result relation is: Customer-name Ali Ahmid Linda Rana

19 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 number of attributes. attribute domains of r and s must be compatible.

20 Set Difference Operation – Example
For example, find all customers who have an account and haven't a loan. customer-name (depositor) - customer-name (borrower) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 depositor borrower 20

21 Set Difference Operation – Example
customer-name (depositor) customer-name (borrower) Customer-name Ali Mahmood Ahmid Linda Rana …. Customer-name Ali Kasim Ahmid Linda Rana …..

22 Set Difference Operation – Example
The result relation is: Customer-name Mahmood

23 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) Customer-name Account-no. Ali A-101 Mahmood A-201 Ahmid A-217 Linda A-222 Rana A-305 Customer-name Loan-no. Ali L-11 Kasim Ahmid L-25 Linda Rana L-34 borrower depositor 23

24 Set Difference Operation – Example
customer-name (borrower) customer-name (depositor) Customer-name Ali Kasim Ahmid Linda Rana ….. Customer-name Ali Mahmood Ahmid Linda Rana ….

25 Set Difference Operation – Example
The result relation is: Customer-name Kasim

26 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.

27 Cartesian-Product Operation
Relations r and s : r s C D E c1 d1 1 c2 d2 2 c3 d3 3 c4 d4 A B a1 1 a2 2

28 Cartesian-Product Operation
The relation r x s is: A B C D E a1 1 c1 d1 c2 d2 2 c3 d3 3 c4 d4 a2 28

29 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 a number of attributes 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.

30 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.

31 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.

32 Relational Schema for the Banking Enterprise
branch (branch-name, branch-city, assets) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)

33 loan-number (amount > 1200 (loan))
Example Queries Find all loans of over $1200. amount > 1200 (loan) Find the loan number for each loan of an amount greater than $1200. loan-number (amount > 1200 (loan))

34 Example Queries Find the names of all customers who have a loan, an account, or both, from the bank. customer-name (borrower)  customer-name (depositor) Find the names of all customers who have a loan and an account at bank. customer-name (borrower)  customer-name (depositor)

35 Example Queries Find the names of all customers who have a loan at the Perryridge branch. customer-name (branch-name=“Perryridge” (borrower.loan-number = loan.loan-number(borrower x loan))) OR customer-name(loan.loan-number = borrower.loan-number( (branch-name = “Perryridge”(loan)) x borrower)) OR customer-name (branch-name=“Perryridge” and borrower.loan-number = loan.loan-number (borrower x loan))

36 Example Queries Find the names of all customers who have a loan at the Perryridge branch but they are not depositors. customer-name (branch-name = “Perryridge” (borrower.loan-number = loan.loan-number(borrower x loan))) – customer-name(depositor)

37 Example Queries Find the largest account balance
Rename account relation as d The query is: balance(account) - account.balance (account.balance < d.balance (account x rd (account)))

38 Natural-Join Operation
Notation: r s Natural join () is an operator that is written as (R   S) where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names.

39 Natural-Join Operation
Notation: r 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 and r.D = s.D (r x s)) It is a special case of the “Cartesian Product”

40 Natural Join Operation – Example
Relations r, s: B D E A B C D 1 3 2 a b 1 2 4 a b r s r s A B C D E 1 2 a b

41 Division Operation r  s
Suited to queries that include the phrase “for all”. The division is a binary operation that is written as R ÷ S. The result consists of the restrictions of tuples in the header of R but not in the header of S, and all their combinations with tuples in S are present in R.

42 Division Operation Example

43 Division Operation – Example
B B Relations r, s: 1 2 3 4 6 1 2 s r  s: A r

44 Division Operation Example
Relations r, s: A B C D E D E a a b 1 3 a b 1 s r A B C r  s: a

45 Example Queries (1) Find 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))

46 Example Queries (2) Find 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”)})

47 Example Queries (3) Find all customers who have an account at all branches located in Brooklyn city. customer-name, branch-name (depositor account)  branch-name (branch-city = “Brooklyn” (branch))

48 Generalized Projection
Extends the projection operation by allowing arithmetic functions to be used in the projection list.  F1, F2, …, Fn(E) E is any relational-algebra expression Each of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E.

49 Generalized Projection
Given relation credit-info(customer-name, limit, credit-balance). find how much more each person can spend: customer-name, limit – credit-balance (credit-info)

50 Generalized Projection
Credit-info customer-name Limit credit-balance Ali 1500 380 Ahmed 2000 1400 Rana 1000 500 Kasim 3500

51 Generalized Projection
The result relation is: customer-name Limit-credit-balance Ali 1220 Ahmed 600 Rana 500 Kasim 2100

52 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

53 Aggregate Functions and Operations
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) E is any relational-algebra expression G1, G2 …, Gn is a list of attributes on which to group (can be empty) Each Fi is an aggregate function Each Ai is an attribute name

54 Aggregate Operation – Example
B C Relation r: 7 3 10 sum-C g sum(c) (r) 27

55 Aggregate Operation – Example
account branch-name account-number balance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-name g sum(balance) (account) branch-name balance Perryridge Brighton Redwood 1300 1500 700

56 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) Branch-name Sum-balance Perryridge 1300 Brighton 1500 Redwood 700

57 More Examples

58 Relational Instances for the Purchasing System
The Supplier relation: S-number S-name S-city S100 Ahmed Amman S200 Ali Jarash S300 Kasim Irbid S400 Jasim Aqaba S500 Rana

59 The Part relation: P-number P-name Color Price P-city P1 TV Silver 300
Amman P2 Camera Black 100 Jarash P3 Video 200 P4 PC 400 Irbid P5 Printer Red P6 Scanner silver 150 59

60 The shipment relation:
S-number P-number Quantity S100 P1 100 P2 150 P3 200 P4 160 S200 S300 400 S400 80 S500 60

61 Q1- Find The cities for all suppliers .
 S-city (Supplier)

62 Q1- Find The cities for all suppliers .
 S-city (Supplier) S-city Amman Jarash Irbid Aqaba The result relation

63 Q2- Find city for Ahmed.

64 Q2- Find city for Ahmed. Temp1   S-name = ‘Ahmed’ (Supplier)

65 Q2- Find city for Ahmed. Temp1   S-name = ‘Ahmed’ (Supplier) S-number S-name S-city S100 Ahmed Amman Temp1

66 Q2- Find city for Ahmed. Temp1   S-name = ‘Ahmed’ (Supplier) S-number S-name S-city S100 Ahmed Amman Temp1 Result   S-city (Temp1) S-city Amman Result

67 Q3- Find the number and name for suppliers in Amman.

68 Q3- Find the number and name for suppliers in Amman.
Temp1   S-city= ‘Amman’ (Supplier)

69 Q3- Find the number and name for suppliers in Amman.
Temp1   S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1

70 Q3- Find the number and name for suppliers in Amman.
Temp1   S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1 Result   S-number, S-name (Temp1)

71 Q3- Find the number and name for suppliers in Amman.
Temp1   S-city= ‘Amman’ (Supplier) S-number S-name S-city S100 Ahmed Amman S500 Rana Temp1 Result   S-number, S-name (Temp1) S-number S-name S100 Ahmed S500 Rana Result

72 Q4- For each part shipped, find the part names and the names of all cities storing these parts.

73 Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Temp1   P-number ( Shipment )

74 Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Temp1   P-number ( Shipment ) P-number P1 P2 P3 P4 Temp1

75 Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Temp1   P-number ( Shipment ) Temp2  Part Temp1

76 Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Temp1   P-number ( Shipment ) Temp2  Part Temp1 P-number P-name Color Price P-city P1 TV Silver 300 Amman P2 Camera Black 100 Jarash P3 Video 200 P4 PC 400 Irbid Temp2

77 Q4- For each part shipped, find the part names and the names of all cities storing these parts.
Temp2  Part Shipment Result   P-name, p-city(Temp2) P-name P-city TV Amman Camera Jarash Video PC Irbid Result

78 Q5- For each part shipped, find the part numbers and names of all cities supplying the parts.

79 Q5- For each part shipped, find the part numbers and names of all cities supplying the parts.
Temp1  Shipment Supplier

80 Q5- For each part shipped, find the part numbers and names of all cities supplying the parts.
Temp1  Shipment Supplier S-number P-number Quantity S-name S-city S100 P1 100 Ahmed Amman P2 150 P3 200 P4 160 S200 Ali Jarash S300 400 Kasim Irbid S400 Jasim Aqaba 80 S500 Rana Temp1

81 Result   P-number, S-city ( Temp1 )
Q5- For each part shipped, find the part numbers and names of all cities supplying the parts. Temp1  Shipment Supplier P-number S-city P1 Amman P2 P3 P4 Jarash Irbid Aqaba Result   P-number, S-city ( Temp1 ) Result

82 Q6- Get supplier numbers for suppliers who supply part p2

83 Q6- Get supplier numbers for suppliers who supply part p2
Temp1   p-number = ‘p2’ (Shipment)

84 Q6- Get supplier numbers for suppliers who supply part p2
Temp1   p-number = ‘p2’ (Shipment) S-number P-number Quantity S100 P2 150 S200 S300 400 S400 Temp1

85 Q6- Get supplier numbers for suppliers who supply part p2
Temp1   P-number = ‘p2’ (Shipment) Result   S-number ( Temp1 ) S-number S100 S200 S300 S400 Result

86 Example Queries Q7- Get names of suppliers who supply at least one silver part.

87 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part)

88 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) P-number P-name Color Price P-city P1 TV Silver 300 Amman P4 PC 400 Irbid P6 Scanner silver 150 Jarash Temp1

89 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) Temp2  Shipment Temp1

90 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) Temp2  Shipment Temp1 S-number P-number Quantity P-name Color Price P-city S100 P1 100 TV Silver 300 Amman P4 160 PC 400 Irbid S200 200 S400 80 S500 Temp2

91 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) S-number S100 S200 S400 S500 Temp3

92 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3 S-number S-name S-city S100 Ahmed Amman S200 Ali Jarash S400 Jasim Aqaba S500 Rana Temp4

93 Example Queries Q7- Get names of suppliers who supply at least one silver part. Temp1   P-color = ‘silver’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3 Result   S-name (Temp4) S-name Ahmed Ali Jasim Rana Result

94 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid.

95 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part)

96 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) P-number P-name Color Price P-city P4 PC Silver 400 Irbid P5 Printer Red 100 Temp1

97 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp2 S-number P-number Quantity P-name Color Price P-city S100 P4 160 PC Silver 400 Irbid P5 50 Printer Red 100 S400 80 S500

98 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) S-number S100 S400 S500 Temp3

99 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3

100 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3 S-number S-name S-city S100 Ahmed Amman S400 Jasim Aqaba S500 Rana Temp4

101 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3 Result   S-name , S-city (Temp4)

102 Example Queries Q8- Get the names and cities of suppliers who supply parts stored in Irbid. Temp1   P-city = ‘Irbid’ (Part) Temp2  Shipment Temp1 Temp3   S-number (Temp2) Temp4  Supplier Temp3 Result   S-name , S-city (Temp4) S-name S-city Ahmed Amman Jasim Aqaba Rana Result

103 Example Queries Q9- Find supplier names for suppliers who supply all parts.

104 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part)

105 Example Queries P-number
Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) P-number P1 P2 P3 P4 P5 P6 Temp1

106 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment)

107 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) S-number P-number S100 P1 P2 P3 P4 P5 P6 S200 S300 S400 S500 Temp2   S-number , P-number (Shipment) Temp2

108 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1

109 Example Queries S-number S100
Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1 S-number S100 Temp3

110 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1 Temp4  Supplier Temp3

111 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1 Temp4  Supplier Temp3 Temp4 S-number S-name S-city S100 Ahmed Amman

112 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1 Temp4  Supplier Temp3 Result   S-name (Temp4)

113 Example Queries Q9- Find supplier names for suppliers who supply all parts. Temp1   P-number (Part) Temp2   S-number , P-number (Shipment) Temp3  Temp2  Temp1 Temp4  Supplier Temp3 Result   S-name (Temp4) S-name Ahmed Result

114 Example Queries Q10- How many parts they have?

115 Example Queries P-number 6 Q10- How many parts they have?
g count (P-number) (Part) P-number 6

116 Example Queries Q11- Find the total quantities supplied by each supplier.

117 Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) (Shipment)

118 Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) (Shipment) S-number Quantity S100 730 S200 350 S300 400 S400 250 S500 100

119 Example Queries Q11- Find the total quantities supplied by each supplier. S-number g sum (Quantity) as ( sum-quantity) (Shipment) S-number Sum-Quantity S100 730 S200 350 S300 400 S400 250 S500 100

120 END


Download ppt "Relational Algebra."

Similar presentations


Ads by Google