1. Database System Concepts, 5th Ed.
ŠSilberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 2: Relational Model
2. ŠSilberschatz, Korth and Sudarshan
2.2
Database System Concepts - 5th Edition, June 15, 2005
Chapter 2: Relational Model
īŽ Structure of Relational Databases
īŽ Fundamental Relational-Algebra-Operations
īŽ Additional Relational-Algebra-Operations
īŽ Extended Relational-Algebra-Operations
īŽ Null Values
īŽ Modification of the Database
3. ŠSilberschatz, Korth and Sudarshan
2.3
Database System Concepts - 5th Edition, June 15, 2005
Example of a Relation
4. ŠSilberschatz, Korth and Sudarshan
2.4
Database System Concepts - 5th Edition, June 15, 2005
Basic Structure
īŽ Formally, given sets D1, D2, âĻ. Dn a relation r is a subset of
D1 x D2 x âĻ x Dn
Thus, a relation is a set of n-tuples (a1, a2, âĻ, an) where each ai ī Di
īŽ Example: If
customer_name = {Jones, Smith, Curry, Lindsay}
customer_street = {Main, North, Park}
customer_city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield) }
is a relation over
customer_name x customer_street x customer_city
5. ŠSilberschatz, Korth and Sudarshan
2.5
Database System Concepts - 5th Edition, June 15, 2005
Attribute Types
īŽ Each attribute of a relation has a name
īŽ The set of allowed values for each attribute is called the domain of the
attribute
īŽ Attribute values are (normally) required to be atomic; that is, indivisible
īŦ Note: multivalued attribute values are not atomic
īŦ Note: composite attribute values are not atomic
īŽ The special value null is a member of every domain
īŽ The null value causes complications in the definition of many operations
īŦ We shall ignore the effect of null values in our main presentation
and consider their effect later
6. ŠSilberschatz, Korth and Sudarshan
2.6
Database System Concepts - 5th Edition, June 15, 2005
Relation Schema
īŽ A1, A2, âĻ, An are attributes
īŽ R = (A1, A2, âĻ, An ) is a relation schema
Example:
Customer_schema = (customer_name, customer_street, customer_city)
īŽ r(R) is a relation on the relation schema R
Example:
customer (Customer_schema)
7. ŠSilberschatz, Korth and Sudarshan
2.7
Database System Concepts - 5th Edition, June 15, 2005
Relation Instance
īŽ The current values (relation instance) of a relation are specified by
a table
īŽ An element t of r is a tuple, represented by a row in a table
Jones
Smith
Curry
Lindsay
customer_name
Main
North
North
Park
customer_street
Harrison
Rye
Rye
Pittsfield
customer_city
customer
attributes
(or columns)
tuples
(or rows)
8. ŠSilberschatz, Korth and Sudarshan
2.8
Database System Concepts - 5th Edition, June 15, 2005
Relations are Unordered
īŽ Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
īŽ Example: account relation with unordered tuples
9. ŠSilberschatz, Korth and Sudarshan
2.9
Database System Concepts - 5th Edition, June 15, 2005
Database
īŽ A database consists of multiple relations
īŽ Information about an enterprise is broken up into parts, with each
relation storing one part of the information
account : stores information about accounts
depositor : stores information about which customer
owns which account
customer : stores information about customers
īŽ Storing all information as a single relation such as
bank(account_number, balance, customer_name, ..)
results in
īŦ repetition of information (e.g., two customers own an account)
īŦ the need for null values (e.g., represent a customer without an
account)
īŽ Normalization theory (Chapter 7) deals with how to design relational
schemas
10. ŠSilberschatz, Korth and Sudarshan
2.10
Database System Concepts - 5th Edition, June 15, 2005
The customer Relation
11. ŠSilberschatz, Korth and Sudarshan
2.11
Database System Concepts - 5th Edition, June 15, 2005
The depositor Relation
12. ŠSilberschatz, Korth and Sudarshan
2.12
Database System Concepts - 5th Edition, June 15, 2005
Keys
īŽ Let K ī R
īŽ K is a superkey of R if values for K are sufficient to identify a unique
tuple of each possible relation r(R)
īŦ by âpossible r â we mean a relation r that could exist in the enterprise
we are modeling.
īŦ Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly
have the same name.
īŽ K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, since it is a
superkey (assuming no two customers can possibly have the same
name), and no subset of it is a superkey.
īŽ Primary Key
13. ŠSilberschatz, Korth and Sudarshan
2.13
Database System Concepts - 5th Edition, June 15, 2005
Query Languages
īŽ Language in which user requests information from the database.
īŽ Categories of languages
īŦ Procedural
īŦ Non-procedural, or declarative
īŽ âPureâ languages:
īŦ Relational algebra
īŦ Tuple relational calculus
īŦ Domain relational calculus
īŽ Pure languages form underlying basis of query languages that people
use.
14. ŠSilberschatz, Korth and Sudarshan
2.14
Database System Concepts - 5th Edition, June 15, 2005
Relational Algebra
īŽ Procedural language
īŽ Six basic operators
īŦ select: īŗ
īŦ project: ī
īŦ union: ī
īŦ set difference: â
īŦ Cartesian product: x
īŦ rename: ī˛
īŽ The operators take one or two relations as inputs and produce a new
relation as a result.
15. ŠSilberschatz, Korth and Sudarshan
2.15
Database System Concepts - 5th Edition, June 15, 2005
Select Operation â Example
īŽ Relation r
A B C D
īĄ
īĄ
īĸ
īĸ
īĄ
īĸ
īĸ
īĸ
1
5
12
23
7
7
3
10
īĄ īŗA=B ^ D > 5 (r)
A B C D
īĄ
īĸ
īĄ
īĸ
1
23
7
10
16. ŠSilberschatz, Korth and Sudarshan
2.16
Database System Concepts - 5th Edition, June 15, 2005
Select Operation
īŽ Notation: īŗ p(r)
īŽ p is called the selection predicate
īŽ Defined as:
īŗ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:
<attribute> op <attribute> or <constant>
where op is one of: =, īš, >, īŗ. <. īŖ
īŽ Example of selection:
īŗ branch_name=âPerryridgeâ(account)
17. ŠSilberschatz, Korth and Sudarshan
2.17
Database System Concepts - 5th Edition, June 15, 2005
Project Operation â Example
īŽ Relation r: A B C
īĄ
īĄ
īĸ
īĸ
10
20
30
40
1
1
1
2
A C
īĄ
īĄ
īĸ
īĸ
1
1
1
2
=
A C
īĄ
īĸ
īĸ
1
1
2
īA,C (r)
18. ŠSilberschatz, Korth and Sudarshan
2.18
Database System Concepts - 5th Edition, June 15, 2005
Project Operation
īŽ Notation:
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
īŽ Duplicate rows removed from result, since relations are sets
īŽ Example: To eliminate the branch_name attribute of account
īaccount_number, balance (account)
)
(
,
,
, 2
1
r
k
A
A
A ī
ī
19. ŠSilberschatz, Korth and Sudarshan
2.19
Database System Concepts - 5th Edition, June 15, 2005
Union Operation â Example
īŽ Relations r, s:
īŽ r ī s:
A B
īĄ
īĄ
īĸ
1
2
1
A B
īĄ
īĸ
2
3
r
s
A B
īĄ
īĄ
īĸ
īĸ
1
2
1
3
20. ŠSilberschatz, Korth and Sudarshan
2.20
Database System Concepts - 5th Edition, June 15, 2005
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 arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column
of r deals with the same type of values as does the 2nd
column of s)
īŽ Example: to find all customers with either an account or a loan
īcustomer_name (depositor) ī īcustomer_name (borrower)
21. ŠSilberschatz, Korth and Sudarshan
2.21
Database System Concepts - 5th Edition, June 15, 2005
Set Difference Operation â Example
īŽ Relations r, s:
īŽ r â s:
A B
īĄ
īĄ
īĸ
1
2
1
A B
īĄ
īĸ
2
3
r
s
A B
īĄ
īĸ
1
1
22. ŠSilberschatz, Korth and Sudarshan
2.22
Database System Concepts - 5th Edition, June 15, 2005
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
23. ŠSilberschatz, Korth and Sudarshan
2.23
Database System Concepts - 5th Edition, June 15, 2005
Cartesian-Product Operation â Example
īŽ Relations r, s:
īŽ r x s:
A B
īĄ
īĸ
1
2
A B
īĄ
īĄ
īĄ
īĄ
īĸ
īĸ
īĸ
īĸ
1
1
1
1
2
2
2
2
C D
īĄ
īĸ
īĸ
ī§
īĄ
īĸ
īĸ
ī§
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
C D
īĄ
īĸ
īĸ
ī§
10
10
20
10
E
a
a
b
b
r
s
24. ŠSilberschatz, Korth and Sudarshan
2.24
Database System Concepts - 5th Edition, June 15, 2005
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.
25. ŠSilberschatz, Korth and Sudarshan
2.25
Database System Concepts - 5th Edition, June 15, 2005
Composition of Operations
īŽ Can build expressions using multiple operations
īŽ Example: īŗA=C(r x s)
īŽ r x s
īŽ īŗA=C(r x s)
A B
īĄ
īĄ
īĄ
īĄ
īĸ
īĸ
īĸ
īĸ
1
1
1
1
2
2
2
2
C D
īĄ
īĸ
īĸ
ī§
īĄ
īĸ
īĸ
ī§
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
A B C D E
īĄ
īĸ
īĸ
1
2
2
īĄ
īĸ
īĸ
10
10
20
a
a
b
26. ŠSilberschatz, Korth and Sudarshan
2.26
Database System Concepts - 5th Edition, June 15, 2005
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
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , âĻ., An .
)
(
)
,...,
,
( 2
1
E
n
A
A
A
x
ī˛
27. ŠSilberschatz, Korth and Sudarshan
2.27
Database System Concepts - 5th Edition, June 15, 2005
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)
28. ŠSilberschatz, Korth and Sudarshan
2.28
Database System Concepts - 5th Edition, June 15, 2005
Example Queries
īŽ 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. ŠSilberschatz, Korth and Sudarshan
2.29
Database System Concepts - 5th Edition, June 15, 2005
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 and an account at
bank.
īcustomer_name (borrower) ī īcustomer_name (depositor)
īcustomer_name (borrower) ī īcustomer_name (depositor)
30. ŠSilberschatz, Korth and Sudarshan
2.30
Database System Concepts - 5th Edition, June 15, 2005
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 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. ŠSilberschatz, Korth and Sudarshan
2.31
Database System Concepts - 5th Edition, June 15, 2005
Example Queries
īŽ 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. ŠSilberschatz, Korth and Sudarshan
2.32
Database System Concepts - 5th Edition, June 15, 2005
Example Queries
īŽ Find the largest account balance
īŦ Strategy:
ī´ Find those balances that are not the largest
â Rename account relation as d so that we can compare each
account balance with all others
ī´ Use set difference to find those account balances that were not found
in the earlier step.
īŦ The query is:
ībalance(account) - īaccount.balance
(īŗaccount.balance < d.balance (account x ī˛d (account)))
33. ŠSilberschatz, Korth and Sudarshan
2.33
Database System Concepts - 5th Edition, June 15, 2005
Formal Definition
īŽ A basic expression in the relational algebra consists of either one of the
following:
īŦ A relation in the database
īŦ A constant relation
īŽ Let E1 and E2 be relational-algebra expressions; the following are all
relational-algebra expressions:
īŦ E1 ī E2
īŦ E1 â E2
īŦ E1 x E2
īŦ īŗp (E1), P is a predicate on attributes in E1
īŦ īs(E1), S is a list consisting of some of the attributes in E1
īŦ ī˛ x (E1), x is the new name for the result of E1
34. ŠSilberschatz, Korth and Sudarshan
2.34
Database System Concepts - 5th Edition, June 15, 2005
Additional Operations
We define additional operations that do not add any power to the
relational algebra, but that simplify common queries.
īŽ Set intersection
īŽ Natural join
īŽ Division
īŽ Assignment
35. ŠSilberschatz, Korth and Sudarshan
2.35
Database System Concepts - 5th Edition, June 15, 2005
Set-Intersection Operation
īŽ Notation: r ī s
īŽ Defined as:
īŽ r ī s = { t | t ī r and t ī s }
īŽ Assume:
īŦ r, s have the same arity
īŦ attributes of r and s are compatible
īŽ Note: r ī s = r â (r â s)
36. ŠSilberschatz, Korth and Sudarshan
2.36
Database System Concepts - 5th Edition, June 15, 2005
Set-Intersection Operation â Example
īŽ Relation r, s:
īŽ r ī s
A B
īĄ
īĄ
īĸ
1
2
1
A B
īĄ
īĸ
2
3
r s
A B
īĄ 2
37. ŠSilberschatz, Korth and Sudarshan
2.37
Database System Concepts - 5th Edition, June 15, 2005
īŽ Notation: r s
Natural-Join Operation
īŽ Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R ī S obtained as follows:
īŦ Consider each pair of tuples tr from r and ts from s.
īŦ If tr and ts have the same value on each of the attributes in R ī S, add
a tuple t to the result, where
ī´ t has the same value as tr on r
ī´ t has the same value as ts on 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 ī r.D = s.D (r x s))
38. ŠSilberschatz, Korth and Sudarshan
2.38
Database System Concepts - 5th Edition, June 15, 2005
Natural Join Operation â Example
īŽ Relations r, s:
A B
īĄ
īĸ
ī§
īĄ
ī¤
1
2
4
1
2
C D
īĄ
ī§
īĸ
ī§
īĸ
a
a
b
a
b
B
1
3
1
2
3
D
a
a
a
b
b
E
īĄ
īĸ
ī§
ī¤
ī
r
A B
īĄ
īĄ
īĄ
īĄ
ī¤
1
1
1
1
2
C D
īĄ
īĄ
ī§
ī§
īĸ
a
a
a
a
b
E
īĄ
ī§
īĄ
ī§
ī¤
s
īŽ r s
39. ŠSilberschatz, Korth and Sudarshan
2.39
Database System Concepts - 5th Edition, June 15, 2005
Division Operation
īŽ Notation:
īŽ Suited to queries that include the phrase âfor allâ.
īŽ Let r and s be relations on schemas R and S respectively
where
īŦ R = (A1, âĻ, Am , B1, âĻ, Bn )
īŦ S = (B1, âĻ, Bn)
The result of r ī¸ s is a relation on schema
R â S = (A1, âĻ, Am)
r ī¸ s = { t | t ī ī R-S (r) ī īĸ u ī s ( tu ī r ) }
Where tu means the concatenation of tuples t and u to
produce a single tuple
r ī¸ s
40. ŠSilberschatz, Korth and Sudarshan
2.40
Database System Concepts - 5th Edition, June 15, 2005
Division Operation â Example
īŽ Relations r, s:
īŽ r ī¸ s: A
B
īĄ
īĸ
1
2
A B
īĄ
īĄ
īĄ
īĸ
ī§
ī¤
ī¤
ī¤
ī
ī
īĸ
1
2
3
1
1
1
3
4
6
1
2
r
s
41. ŠSilberschatz, Korth and Sudarshan
2.41
Database System Concepts - 5th Edition, June 15, 2005
Another Division Example
A B
īĄ
īĄ
īĄ
īĸ
īĸ
ī§
ī§
ī§
a
a
a
a
a
a
a
a
C D
īĄ
ī§
ī§
ī§
ī§
ī§
ī§
īĸ
a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
īŽ Relations r, s:
īŽ r ī¸ s:
D
a
b
E
1
1
A B
īĄ
ī§
a
a
C
ī§
ī§
r
s
42. ŠSilberschatz, Korth and Sudarshan
2.42
Database System Concepts - 5th Edition, June 15, 2005
Division Operation (Cont.)
īŽ Property
īŦ Let q = r ī¸ s
īŦ Then q is the largest relation satisfying q x s ī r
īŽ Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S ī R
r ī¸ s = īR-S (r ) â īR-S ( ( īR-S (r ) x s ) â īR-S,S(r ))
To see why
īŦ īR-S,S (r) simply reorders attributes of r
īŦ īR-S (īR-S (r ) x s ) â īR-S,S(r) ) gives those tuples t in
īR-S (r ) such that for some tuple u ī s, tu ī r.
43. ŠSilberschatz, Korth and Sudarshan
2.43
Database System Concepts - 5th Edition, June 15, 2005
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.
īŽ Example: Write r ī¸ s as
temp1 īŦ īR-S (r )
temp2 īŦ īR-S ((temp1 x s ) â īR-S,S (r ))
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.
44. ŠSilberschatz, Korth and Sudarshan
2.44
Database System Concepts - 5th Edition, June 15, 2005
Bank Example Queries
īŽ Find the names of all customers who have a loan and an account at
bank.
īcustomer_name (borrower) ī īcustomer_name (depositor)
īŽ Find the name of all customers who have a loan at the bank and the
loan amount
)
( loan
borrower
ount
number, am
ame, loan-
customer-n
ī
45. ŠSilberschatz, Korth and Sudarshan
2.45
Database System Concepts - 5th Edition, June 15, 2005
īŦ Query 2
īcustomer_name, branch_name (depositor account)
ī¸ ī˛temp(branch_name) ({(âDowntownâ ), (âUptownâ )})
Note that Query 2 uses a constant relation.
Bank Example Queries
īŽ Find all customers who have an account from at least the âDowntownâ
and the Uptownâ branches.
īŦ Query 1
īcustomer_name (īŗbranch_name = âDowntownâ (depositor account )) ī
īcustomer_name (īŗbranch_name = âUptownâ (depositor account))
46. ŠSilberschatz, Korth and Sudarshan
2.46
Database System Concepts - 5th Edition, June 15, 2005
īŽ Find all customers who have an account at all branches located in
Brooklyn city.
Example Queries
īcustomer_name, branch_name (depositor account)
ī¸ ībranch_name (īŗbranch_city = âBrooklynâ (branch))
47. ŠSilberschatz, Korth and Sudarshan
2.47
Database System Concepts - 5th Edition, June 15, 2005
Extended Relational-Algebra-Operations
īŽ Generalized Projection
īŽ Aggregate Functions
īŽ Outer Join
48. ŠSilberschatz, Korth and Sudarshan
2.48
Database System Concepts - 5th Edition, June 15, 2005
Generalized Projection
īŽ Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
īŽ E is any relational-algebra expression
īŽ Each of F1, F2, âĻ, Fn are are arithmetic expressions involving constants
and attributes in the schema of E.
īŽ Given relation credit_info(customer_name, limit, credit_balance), find
how much more each person can spend:
īcustomer_name, limit â credit_balance (credit_info)
)
(
,...,
, 2
1
E
n
F
F
F
ī
49. ŠSilberschatz, Korth and Sudarshan
2.49
Database System Concepts - 5th Edition, June 15, 2005
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
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
)
(
)
(
,
,
(
),
(
,
,
, 2
2
1
1
2
1
E
n
n
n A
F
A
F
A
F
G
G
G ī
ī
ī
50. ŠSilberschatz, Korth and Sudarshan
2.50
Database System Concepts - 5th Edition, June 15, 2005
Aggregate Operation â Example
īŽ Relation r:
A B
īĄ
īĄ
īĸ
īĸ
īĄ
īĸ
īĸ
īĸ
C
7
7
3
10
īŽ g sum(c) (r) sum(c )
27
51. ŠSilberschatz, Korth and Sudarshan
2.51
Database System Concepts - 5th Edition, June 15, 2005
Aggregate Operation â Example
īŽ Relation account grouped by branch-name:
branch_name g sum(balance) (account)
branch_name account_number balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch_name sum(balance)
Perryridge
Brighton
Redwood
1300
1500
700
52. ŠSilberschatz, Korth and Sudarshan
2.52
Database System Concepts - 5th Edition, June 15, 2005
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)
53. ŠSilberschatz, Korth and Sudarshan
2.53
Database System Concepts - 5th Edition, June 15, 2005
Outer Join
īŽ An extension of the join operation that avoids loss of information.
īŽ Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
īŽ Uses null values:
īŦ null signifies that the value is unknown or does not exist
īŦ All comparisons involving null are (roughly speaking) false by
definition.
ī´ We shall study precise meaning of comparisons with nulls later
54. ŠSilberschatz, Korth and Sudarshan
2.54
Database System Concepts - 5th Edition, June 15, 2005
Outer Join â Example
īŽ Relation loan
īŽ Relation borrower
customer_name loan_number
Jones
Smith
Hayes
L-170
L-230
L-155
3000
4000
1700
loan_number amount
L-170
L-230
L-260
branch_name
Downtown
Redwood
Perryridge
55. ŠSilberschatz, Korth and Sudarshan
2.55
Database System Concepts - 5th Edition, June 15, 2005
Outer Join â Example
īŽ Inner Join
loan Borrower
loan_number amount
L-170
L-230
3000
4000
customer_name
Jones
Smith
branch_name
Downtown
Redwood
Jones
Smith
null
loan_number amount
L-170
L-230
L-260
3000
4000
1700
customer_name
branch_name
Downtown
Redwood
Perryridge
īŽ Left Outer Join
loan Borrower
56. ŠSilberschatz, Korth and Sudarshan
2.56
Database System Concepts - 5th Edition, June 15, 2005
Outer Join â Example
loan_number amount
L-170
L-230
L-155
3000
4000
null
customer_name
Jones
Smith
Hayes
branch_name
Downtown
Redwood
null
loan_number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer_name
Jones
Smith
null
Hayes
branch_name
Downtown
Redwood
Perryridge
null
īŽ Full Outer Join
loan borrower
īŽ Right Outer Join
loan borrower
57. ŠSilberschatz, Korth and Sudarshan
2.57
Database System Concepts - 5th Edition, June 15, 2005
Null Values
īŽ It is possible for tuples to have a null value, denoted by null, for some
of their attributes
īŽ null signifies an unknown value or that a value does not exist.
īŽ The result of any arithmetic expression involving null is null.
īŽ Aggregate functions simply ignore null values (as in SQL)
īŽ For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same (as in SQL)
58. ŠSilberschatz, Korth and Sudarshan
2.58
Database System Concepts - 5th Edition, June 15, 2005
Null Values
īŽ Comparisons with null values return the special truth value: unknown
īŦ If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
īŽ Three-valued logic using the truth value unknown:
īŦ OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
īŦ AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
īŦ NOT: (not unknown) = unknown
īŦ In SQL âP is unknownâ evaluates to true if predicate P evaluates to
unknown
īŽ Result of select predicate is treated as false if it evaluates to unknown
59. ŠSilberschatz, Korth and Sudarshan
2.59
Database System Concepts - 5th Edition, June 15, 2005
Modification of the Database
īŽ The content of the database may be modified using the following
operations:
īŦ Deletion
īŦ Insertion
īŦ Updating
īŽ All these operations are expressed using the assignment
operator.
60. ŠSilberschatz, Korth and Sudarshan
2.60
Database System Concepts - 5th Edition, June 15, 2005
Deletion
īŽ A delete request is expressed similarly to a query, except instead
of displaying tuples to the user, the selected tuples are removed
from the database.
īŽ Can delete only whole tuples; cannot delete values on only
particular attributes
īŽ A deletion is expressed in relational algebra by:
r īŦ r â E
where r is a relation and E is a relational algebra query.
61. ŠSilberschatz, Korth and Sudarshan
2.61
Database System Concepts - 5th Edition, June 15, 2005
Deletion Examples
īŽ Delete all account records in the Perryridge branch.
īŽ Delete all accounts at branches located in Needham.
r1 īŦ īŗbranch_city = âNeedhamâ (account branch )
r2 īŦ ībranch_name, account_number, balance (r1)
r3 īŦ ī customer_name, account_number (r2 depositor)
account īŦ account â r2
depositor īŦ depositor â r3
īŽ Delete all loan records with amount in the range of 0 to 50
loan īŦ loan â īŗ amount īŗī 0 and amount īŖ 50 (loan)
account īŦ account â īŗī branch_name = âPerryridgeâ (account )
62. ŠSilberschatz, Korth and Sudarshan
2.62
Database System Concepts - 5th Edition, June 15, 2005
Insertion
īŽ To insert data into a relation, we either:
īŦ specify a tuple to be inserted
īŦ write a query whose result is a set of tuples to be inserted
īŽ in relational algebra, an insertion is expressed by:
r īŦ r ī E
where r is a relation and E is a relational algebra expression.
īŽ The insertion of a single tuple is expressed by letting E be a constant
relation containing one tuple.
63. ŠSilberschatz, Korth and Sudarshan
2.63
Database System Concepts - 5th Edition, June 15, 2005
Insertion Examples
īŽ Insert information in the database specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
īŽ Provide as a gift for all loan customers in the Perryridge
branch, a $200 savings account. Let the loan number serve
as the account number for the new savings account.
account īŦ account ī {(âPerryridgeâ, A-973, 1200)}
depositor īŦ depositor ī {(âSmithâ, A-973)}
r1 īŦ (īŗbranch_name = âPerryridgeâ (borrower loan))
account īŦ account ī ībranch_name, loan_number,200 (r1)
depositor īŦ depositor ī īcustomer_name, loan_number (r1)
64. ŠSilberschatz, Korth and Sudarshan
2.64
Database System Concepts - 5th Edition, June 15, 2005
Updating
īŽ A mechanism to change a value in a tuple without charging all values in
the tuple
īŽ Use the generalized projection operator to do this task
īŽ Each Fi is either
īŦ the I th attribute of r, if the I th attribute is not updated, or,
īŦ if the attribute is to be updated Fi is an expression, involving only
constants and the attributes of r, which gives the new value for the
attribute
)
(
,
,
,
, 2
1
r
r l
F
F
F ī
ī
īŦ
65. ŠSilberschatz, Korth and Sudarshan
2.65
Database System Concepts - 5th Edition, June 15, 2005
Update Examples
īŽ Make interest payments by increasing all balances by 5 percent.
īŽ Pay all accounts with balances over $10,000 6 percent interest
and pay all others 5 percent
account īŦ ī account_number, branch_name, balance * 1.06 (īŗ BAL īž 10000 (account ))
ī ī account_number, branch_name, balance * 1.05 (īŗBAL īŖ 10000
(account))
account īŦ ī account_number, branch_name, balance * 1.05 (account)
66. Database System Concepts, 5th Ed.
ŠSilberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
End of Chapter 2
67. ŠSilberschatz, Korth and Sudarshan
2.67
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.3. The branch relation
68. ŠSilberschatz, Korth and Sudarshan
2.68
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.6: The loan relation
69. ŠSilberschatz, Korth and Sudarshan
2.69
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.7: The borrower relation
70. ŠSilberschatz, Korth and Sudarshan
2.70
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.8: Schema diagram
71. ŠSilberschatz, Korth and Sudarshan
2.71
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.9
Result of īŗbranch_name = âPerryridgeâ (loan)
72. ŠSilberschatz, Korth and Sudarshan
2.72
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.10:
Loan number and the amount of the loan
73. ŠSilberschatz, Korth and Sudarshan
2.73
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.11: Names of all customers who
have either an account or an loan
74. ŠSilberschatz, Korth and Sudarshan
2.74
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.12:
Customers with an account but no loan
75. ŠSilberschatz, Korth and Sudarshan
2.75
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.13: Result of borrower |X| loan
76. ŠSilberschatz, Korth and Sudarshan
2.76
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.14
77. ŠSilberschatz, Korth and Sudarshan
2.77
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.15
78. ŠSilberschatz, Korth and Sudarshan
2.78
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.16
79. ŠSilberschatz, Korth and Sudarshan
2.79
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.17
Largest account balance in the bank
80. ŠSilberschatz, Korth and Sudarshan
2.80
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.18: Customers who live on the
same street and in the same city as
Smith
81. ŠSilberschatz, Korth and Sudarshan
2.81
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.19: Customers with both an
account and a loan at the bank
82. ŠSilberschatz, Korth and Sudarshan
2.82
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.20
83. ŠSilberschatz, Korth and Sudarshan
2.83
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.21
84. ŠSilberschatz, Korth and Sudarshan
2.84
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.22
85. ŠSilberschatz, Korth and Sudarshan
2.85
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.23
86. ŠSilberschatz, Korth and Sudarshan
2.86
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.24: The credit_info relation
87. ŠSilberschatz, Korth and Sudarshan
2.87
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.25
88. ŠSilberschatz, Korth and Sudarshan
2.88
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.26: The pt_works relation
89. ŠSilberschatz, Korth and Sudarshan
2.89
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.27
The pt_works relation after regrouping
90. ŠSilberschatz, Korth and Sudarshan
2.90
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.28
91. ŠSilberschatz, Korth and Sudarshan
2.91
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.29
92. ŠSilberschatz, Korth and Sudarshan
2.92
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.30
The employee and ft_works relations
93. ŠSilberschatz, Korth and Sudarshan
2.93
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.31
94. ŠSilberschatz, Korth and Sudarshan
2.94
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.32
95. ŠSilberschatz, Korth and Sudarshan
2.95
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.33
96. ŠSilberschatz, Korth and Sudarshan
2.96
Database System Concepts - 5th Edition, June 15, 2005
Figure 2.34