Chapter 2b

advertisement
Chapter 2: Relational Model
Database System Concepts, 5th Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Set Difference Operation – Example
 Relations r, s:
A
B
A
B

1

2

2

3

1
s
r
 r – s:
Database System Concepts - 5th Edition, Oct 5, 2006
A
B

1

1
2.2
©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, Oct 5, 2006
2.3
©Silberschatz, Korth and Sudarshan
Example
Find the names of all customers who have an
account but no loan from the bank.
customer_name (depositor) - customer_name (borrower)
Depositor
Database System Concepts - 5th Edition, Oct 5, 2006
Borrower
2.4
©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation – Example
 Relations r, s:
A
B
C
D
E

1

2




10
10
20
10
a
a
b
b
r
s
 r x s:
A
B
C
D
E








1
1
1
1
2
2
2
2








10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
Database System Concepts - 5th Edition, Oct 5, 2006
2.5
©Silberschatz, Korth and Sudarshan
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.
Database System Concepts - 5th Edition, Oct 5, 2006
2.6
©Silberschatz, Korth and Sudarshan
Composition of Operations
 Can build expressions using multiple operations
 Example: A=C(r x s)
 rxs
A
B
C
D
E








1
1
1
1
2
2
2
2








10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
A
B
C
D
E



1
2
2
 10
 10
 20
a
a
b
 A=C(r x s)
Database System Concepts - 5th Edition, Oct 5, 2006
2.7
©Silberschatz, Korth and Sudarshan
Banking Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Database System Concepts - 5th Edition, Oct 5, 2006
2.8
©Silberschatz, Korth and Sudarshan
Example
 Find the names of all customers who have a loan at the Mirpur
branch.
customer_name (branch_name=“Mirpur”
(borrower.loan_number = loan.loan_number(borrower x loan)))
 Find the names of all customers who have a loan at the
Mirpur branch but do not have an account at any branch of
the bank.
customer_name (branch_name = “Mirpur”
(borrower.loan_number = loan.loan_number(borrower x loan))) –
customer_name(depositor)
Database System Concepts - 5th Edition, Oct 5, 2006
2.9
©Silberschatz, Korth and Sudarshan
Examples
 Find out the customer names who live in Khulna city
customer_name (customer_city=“khulna”(customer))
 Find out the customer names who have an account but do not have
a loan at any branch of the bank.
customer_name (depositor) – customer_name(borrower)
Database System Concepts - 5th Edition, Oct 5, 2006
2.10
©Silberschatz, Korth and Sudarshan
Download