Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.

Similar presentations


Presentation on theme: "Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics."— Presentation transcript:

1 Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics

2 ER for Banking Enterprise

3 Schema Diagram for the Banking Enterprise

4 Query Languages Categories of languages Categories of languages procedural procedural non-procedural non-procedural “Pure” languages: “Pure” languages: Relational Algebra Relational Algebra Tuple Relational Calculus Tuple Relational Calculus Domain Relational Calculus Domain Relational Calculus Declarative languages: Declarative languages: SQL SQL

5 Relational Algebra Procedural language Procedural language Six basic operators Six basic operators Select Select  Projection  Projection  Union Union  set difference set difference – Cartesian product Cartesian product x Rename  Rename  The operators take one or more relations as inputs and give a new relation as a result. The operators take one or more relations as inputs and give a new relation as a result.

6 Select Operation – Example Relation r ABCD   1 5 12 23 7 3 10  A=B ^ D > 5 (r) ABCD   1 23 7 10

7 Select Operation Notation:  p (r) Notation:  p (r) p is called the selection predicate p is called the selection predicate Defined as: Defined as:  p ( r) = {t | t  r and p(t)}  p ( r) = {t | t  r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by :  (and),  (or),  (not) Each term is one of: op or op or where op is one of: =, , >, ., . <. 

8 Example of selection  branch-name=“Perryridge ” (account)  branch-name=“Perryridge ” (account) Selection gives a horizontal subset of a relation Selection gives a horizontal subset of a relation a subset of all the tuples (rows) of a relation a subset of all the tuples (rows) of a relation  branch-name=“Perryridge”(account)  branch-name=“Perryridge”(account) ? ? account

9 Project Operation – Example Relation r: Relation r: ABC  10 20 30 40 11121112 AC  11121112 = AC  112112  A,C (r)  A,C (r)

10 Project Operation Notation:  A1, A2, …, Ak (r) Notation:  A1, A2, …, Ak (r) where A 1, A 2 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 The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Duplicate rows removed from result, since relations are sets

11 Example of Projection To eliminate the branch-name attribute of account  account-number, balance (account) To eliminate the branch-name attribute of account  account-number, balance (account) Projection gives a vertical subset of a relation Projection gives a vertical subset of a relation a subset of all the columns of a relation a subset of all the columns of a relation  account-number, balance (account) ? account

12 Union Operation – Example Relations r, s: Relations r, s: r  s: AB  121121 AB  2323 r s AB  12131213

13 Union Operation Notation: r  s Notation: r  s Defined as: Defined as: r  s = {t | t  r or t  s} For r  s to be valid. For r  s to be valid. 1. r, s must have the same arity (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)

14 Example of Union Find all customers with either an account or a loan  customer-name (depositor)   customer-name (borrower) Find all customers with either an account or a loan  customer-name (depositor)   customer-name (borrower) depositor borrower  customer-name (depositor)  customer-name (borrower)  ? ? ?

15 Set Difference Operation – Example Relations r, s: Relations r, s: r – s : AB  121121 AB  2323 r s AB  1111

16 Set Difference Operation Notation r – s Notation r – s Defined as: Defined as: r – s = {t | t  r and t  s} r – s = {t | t  r and t  s} Set differences must be taken between compatible relations. Set differences must be taken between compatible relations. r and s must have the same arity r and s must have the same arity attribute domains of r and s must be compatible attribute domains of r and s must be compatible

17 Example of Set Difference Find all customers with either an account or a loan  customer-name (depositor)   customer-name (borrower) Find all customers with either an account or a loan  customer-name (depositor)   customer-name (borrower) depositor borrower  customer-name (depositor)  customer-name (borrower) - ? ? ?

18 Cartesian-Product Operation-Example Relations r, s: r x s: AB  1212 AB  1111222211112222 CD  10 20 10 20 10 E aabbaabbaabbaabb CD  20 10 E aabbaabb r s

19 Cartesian-Product Operation Notation r x s Notation r x s Defined as: 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 =  ). 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. If attributes of r(R) and s(S) are not disjoint, then renaming must be used.

20 the borrower relation

21 the loan relation

22 Result of borrower |X| loan

23 Cartesian-Product Operation Cartesian-Product itself is usually not so useful Cartesian-Product itself is usually not so useful It is often used as a “pre-processing” It is often used as a “pre-processing” Other operators such as selection and projection will follow Other operators such as selection and projection will follow

24 Composition of Operations Can build expressions using multiple operations Can build expressions using multiple operations Example:  A=C (r x s) Example:  A=C (r x s) r x s r x s  A=C (r x s)  A=C (r x s) AB  1111222211112222 CD  10 20 10 20 10 E aabbaabbaabbaabb ABCDE  122122  20 aabaab

25 Rename Operation Allows us to refer to a relation by more than one name. Allows us to refer to a relation by more than one name. Example:  x (E) Example:  x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then If a relational-algebra expression E has arity n, then  x (A1, A2, …, An) (E)  x (A1, A2, …, An) (E) returns the result of expression E under the name X, and with the attributes renamed to A 1, A2, …., An.

26 Rename Operation Example: Example:  downtown-account(account-number,branch- name,balance) (  branch-name=“Downtown” (account) account ?

27 Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)

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

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

30 Example Queries Find the names of all customers who have a loan at the Perryridge branch. Find the names of all customers who have a loan at the Perryridge branch. n Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.  customer-name (  branch-name = “Perryridge” (  borrower.loan-number = loan.loan-number ( borrower x loan ))) –  customer-name ( depositor )  customer-name (  branch-name=“Perryridge ” (  borrower.loan-number = loan.loan-number (borrower x loan)))

31 Example Queries Find the names of all customers who have a loan at the Perryridge branch. Find the names of all customers who have a loan at the Perryridge branch.  Query 2  customer-name (  loan.loan-number = borrower.loan-number ( (  branch-name = “Perryridge” (loan)) x borrower))  Query 1  customer-name (  branch-name = “Perryridge” (  borrower.loan-number = loan.loan-number (borrower x loan)))

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

33 branch branch-name assets branch

34 account-number branch-name balance account

35 customer-name account-number depositor

36 customer-name customer-street customer-city customer

37 customer-name loan-number borrower

38 loan-number branch-name amount loan

39 customer

40 branch

41 loan

42 account

43 borrower

44 depositor


Download ppt "Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics."

Similar presentations


Ads by Google