This document summarizes key concepts from Chapter 2 of the textbook "Database System Concepts" by Silberschatz, Korth and Sudarshan. It discusses the relational model, including the structure of relational databases, relational algebra operations, keys such as primary and foreign keys, and query languages. Example relations are provided to illustrate concepts like the select, project, union and cartesian product operations in relational algebra.
Database System Concepts Chapter 2: Relational Model Summary
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, Oct 5, 2006
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, Oct 5, 2006
Example of a Relation
4. ŠSilberschatz, Korth and Sudarshan
2.4
Database System Concepts - 5th Edition, Oct 5, 2006
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, âĻ}
/* Set of all customer names */
īŦ customer_street = {Main, North, Park, âĻ} /* set of all street names*/
īŦ customer_city = {Harrison, Rye, Pittsfield, âĻ} /* set of all city names */
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, Oct 5, 2006
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
īŦ E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
īŽ Domain is said to be atomic if all its members are 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, Oct 5, 2006
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) denotes a relation r on the relation schema R
Example:
customer (Customer_schema)
7. ŠSilberschatz, Korth and Sudarshan
2.7
Database System Concepts - 5th Edition, Oct 5, 2006
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, Oct 5, 2006
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, Oct 5, 2006
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.,if two customers own an account (What gets repeated?)
īŦ the need for null values
ī´ e.g., to 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, Oct 5, 2006
The customer Relation
11. ŠSilberschatz, Korth and Sudarshan
2.11
Database System Concepts - 5th Edition, Oct 5, 2006
The depositor Relation
12. ŠSilberschatz, Korth and Sudarshan
2.12
Database System Concepts - 5th Edition, Oct 5, 2006
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
ī´ In real life, an attribute such as customer_id would be used instead of
customer_name to uniquely identify customers, but we omit it to keep
our examples small, and instead assume customer names are unique.
13. ŠSilberschatz, Korth and Sudarshan
2.13
Database System Concepts - 5th Edition, Oct 5, 2006
Keys (Cont.)
īŽ K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, since it
is a superkey and no subset of it is a superkey.
īŽ Primary key: a candidate key chosen as the principal means of
identifying tuples within a relation
īŦ Should choose an attribute whose value never, or very rarely,
changes.
īŦ E.g. email address is unique, but may change
14. ŠSilberschatz, Korth and Sudarshan
2.14
Database System Concepts - 5th Edition, Oct 5, 2006
Foreign Keys
īŽ A relation schema may have an attribute that corresponds to the primary
key of another relation. The attribute is called a foreign key.
īŦ E.g. customer_name and account_number attributes of depositor are
foreign keys to customer and account respectively.
īŦ Only values occurring in the primary key attribute of the referenced
relation may occur in the foreign key attribute of the referencing
relation.
īŽ Schema diagram
15. ŠSilberschatz, Korth and Sudarshan
2.15
Database System Concepts - 5th Edition, Oct 5, 2006
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.
16. ŠSilberschatz, Korth and Sudarshan
2.16
Database System Concepts - 5th Edition, Oct 5, 2006
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.
17. ŠSilberschatz, Korth and Sudarshan
2.17
Database System Concepts - 5th Edition, Oct 5, 2006
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
18. ŠSilberschatz, Korth and Sudarshan
2.18
Database System Concepts - 5th Edition, Oct 5, 2006
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)
19. ŠSilberschatz, Korth and Sudarshan
2.19
Database System Concepts - 5th Edition, Oct 5, 2006
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)
20. ŠSilberschatz, Korth and Sudarshan
2.20
Database System Concepts - 5th Edition, Oct 5, 2006
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 ī
ī
21. ŠSilberschatz, Korth and Sudarshan
2.21
Database System Concepts - 5th Edition, Oct 5, 2006
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
22. ŠSilberschatz, Korth and Sudarshan
2.22
Database System Concepts - 5th Edition, Oct 5, 2006
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)
23. ŠSilberschatz, Korth and Sudarshan
2.23
Database System Concepts - 5th Edition, Oct 5, 2006
Set Difference Operation â Example
īŽ Relations r, s:
īŽ r â s:
A B
īĄ
īĄ
īĸ
1
2
1
A B
īĄ
īĸ
2
3
r
s
A B
īĄ
īĸ
1
1
24. ŠSilberschatz, Korth and Sudarshan
2.24
Database System Concepts - 5th Edition, Oct 5, 2006
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
25. ŠSilberschatz, Korth and Sudarshan
2.25
Database System Concepts - 5th Edition, Oct 5, 2006
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
26. ŠSilberschatz, Korth and Sudarshan
2.26
Database System Concepts - 5th Edition, Oct 5, 2006
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. ŠSilberschatz, Korth and Sudarshan
2.27
Database System Concepts - 5th Edition, Oct 5, 2006
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
28. ŠSilberschatz, Korth and Sudarshan
2.28
Database System Concepts - 5th Edition, Oct 5, 2006
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
ī˛
29. ŠSilberschatz, Korth and Sudarshan
2.29
Database System Concepts - 5th Edition, Oct 5, 2006
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)
30. ŠSilberschatz, Korth and Sudarshan
2.30
Database System Concepts - 5th Edition, Oct 5, 2006
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))
īŽ Find the names of all customers who have a loan, an account, or both,
from the bank
īcustomer_name (borrower) ī īcustomer_name (depositor)
31. ŠSilberschatz, Korth and Sudarshan
2.31
Database System Concepts - 5th Edition, Oct 5, 2006
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)))
32. ŠSilberschatz, Korth and Sudarshan
2.32
Database System Concepts - 5th Edition, Oct 5, 2006
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)))
33. ŠSilberschatz, Korth and Sudarshan
2.33
Database System Concepts - 5th Edition, Oct 5, 2006
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)))
34. ŠSilberschatz, Korth and Sudarshan
2.34
Database System Concepts - 5th Edition, Oct 5, 2006
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
35. ŠSilberschatz, Korth and Sudarshan
2.35
Database System Concepts - 5th Edition, Oct 5, 2006
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
36. ŠSilberschatz, Korth and Sudarshan
2.36
Database System Concepts - 5th Edition, Oct 5, 2006
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)
37. ŠSilberschatz, Korth and Sudarshan
2.37
Database System Concepts - 5th Edition, Oct 5, 2006
Set-Intersection Operation â Example
īŽ Relation r, s:
īŽ r ī s
A B
īĄ
īĄ
īĸ
1
2
1
A B
īĄ
īĸ
2
3
r s
A B
īĄ 2
38. ŠSilberschatz, Korth and Sudarshan
2.38
Database System Concepts - 5th Edition, Oct 5, 2006
īŽ 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))
39. ŠSilberschatz, Korth and Sudarshan
2.39
Database System Concepts - 5th Edition, Oct 5, 2006
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
40. ŠSilberschatz, Korth and Sudarshan
2.40
Database System Concepts - 5th Edition, Oct 5, 2006
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
41. ŠSilberschatz, Korth and Sudarshan
2.41
Database System Concepts - 5th Edition, Oct 5, 2006
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
42. ŠSilberschatz, Korth and Sudarshan
2.42
Database System Concepts - 5th Edition, Oct 5, 2006
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
43. ŠSilberschatz, Korth and Sudarshan
2.43
Database System Concepts - 5th Edition, Oct 5, 2006
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.
44. ŠSilberschatz, Korth and Sudarshan
2.44
Database System Concepts - 5th Edition, Oct 5, 2006
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.
45. ŠSilberschatz, Korth and Sudarshan
2.45
Database System Concepts - 5th Edition, Oct 5, 2006
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
īcustomer_name, loan_number, amount (borrower loan)
46. ŠSilberschatz, Korth and Sudarshan
2.46
Database System Concepts - 5th Edition, Oct 5, 2006
īŦ Query 1
īcustomer_name (īŗbranch_name = âDowntownâ (depositor account )) ī
īcustomer_name (īŗbranch_name = âUptownâ (depositor account))
īŦ 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.
47. ŠSilberschatz, Korth and Sudarshan
2.47
Database System Concepts - 5th Edition, Oct 5, 2006
īŽ Find all customers who have an account at all branches located in
Brooklyn city.
Bank Example Queries
īcustomer_name, branch_name (depositor account)
ī¸ ībranch_name (īŗbranch_city = âBrooklynâ (branch))
48. ŠSilberschatz, Korth and Sudarshan
2.48
Database System Concepts - 5th Edition, Oct 5, 2006
Extended Relational-Algebra-Operations
īŽ Generalized Projection
īŽ Aggregate Functions
īŽ Outer Join
49. ŠSilberschatz, Korth and Sudarshan
2.49
Database System Concepts - 5th Edition, Oct 5, 2006
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
ī
50. ŠSilberschatz, Korth and Sudarshan
2.50
Database System Concepts - 5th Edition, Oct 5, 2006
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 ī
ī
ī
51. ŠSilberschatz, Korth and Sudarshan
2.51
Database System Concepts - 5th Edition, Oct 5, 2006
Aggregate Operation â Example
īŽ Relation r:
A B
īĄ
īĄ
īĸ
īĸ
īĄ
īĸ
īĸ
īĸ
C
7
7
3
10
īŽ g sum(c) (r) sum(c )
27
52. ŠSilberschatz, Korth and Sudarshan
2.52
Database System Concepts - 5th Edition, Oct 5, 2006
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
53. ŠSilberschatz, Korth and Sudarshan
2.53
Database System Concepts - 5th Edition, Oct 5, 2006
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)
54. ŠSilberschatz, Korth and Sudarshan
2.54
Database System Concepts - 5th Edition, Oct 5, 2006
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
55. ŠSilberschatz, Korth and Sudarshan
2.55
Database System Concepts - 5th Edition, Oct 5, 2006
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
56. ŠSilberschatz, Korth and Sudarshan
2.56
Database System Concepts - 5th Edition, Oct 5, 2006
Outer Join â Example
īŽ 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
57. ŠSilberschatz, Korth and Sudarshan
2.57
Database System Concepts - 5th Edition, Oct 5, 2006
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
58. ŠSilberschatz, Korth and Sudarshan
2.58
Database System Concepts - 5th Edition, Oct 5, 2006
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)
59. ŠSilberschatz, Korth and Sudarshan
2.59
Database System Concepts - 5th Edition, Oct 5, 2006
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
60. ŠSilberschatz, Korth and Sudarshan
2.60
Database System Concepts - 5th Edition, Oct 5, 2006
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.
61. ŠSilberschatz, Korth and Sudarshan
2.61
Database System Concepts - 5th Edition, Oct 5, 2006
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.
62. ŠSilberschatz, Korth and Sudarshan
2.62
Database System Concepts - 5th Edition, Oct 5, 2006
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 īŦ ī account_number, branch_name, 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 )
63. ŠSilberschatz, Korth and Sudarshan
2.63
Database System Concepts - 5th Edition, Oct 5, 2006
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.
64. ŠSilberschatz, Korth and Sudarshan
2.64
Database System Concepts - 5th Edition, Oct 5, 2006
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 ī {(âA-973â, âPerryridgeâ, 1200)}
depositor īŦ depositor ī {(âSmithâ, âA-973â)}
r1 īŦ (īŗbranch_name = âPerryridgeâ (borrower loan))
account īŦ account ī īloan_number, branch_name, 200 (r1)
depositor īŦ depositor ī īcustomer_name, loan_number (r1)
65. ŠSilberschatz, Korth and Sudarshan
2.65
Database System Concepts - 5th Edition, Oct 5, 2006
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 ī
ī
īŦ
66. ŠSilberschatz, Korth and Sudarshan
2.66
Database System Concepts - 5th Edition, Oct 5, 2006
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)
67. Database System Concepts, 5th Ed.
ŠSilberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
End of Chapter 2
68. ŠSilberschatz, Korth and Sudarshan
2.68
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.3. The branch relation
69. ŠSilberschatz, Korth and Sudarshan
2.69
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.6: The loan relation
70. ŠSilberschatz, Korth and Sudarshan
2.70
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.7: The borrower relation
71. ŠSilberschatz, Korth and Sudarshan
2.71
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.9
Result of īŗbranch_name = âPerryridgeâ (loan)
72. ŠSilberschatz, Korth and Sudarshan
2.72
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.10:
Loan number and the amount of the loan
73. ŠSilberschatz, Korth and Sudarshan
2.73
Database System Concepts - 5th Edition, Oct 5, 2006
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, Oct 5, 2006
Figure 2.12:
Customers with an account but no loan
75. ŠSilberschatz, Korth and Sudarshan
2.75
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.13: Result of borrower |X| loan
76. ŠSilberschatz, Korth and Sudarshan
2.76
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.14
77. ŠSilberschatz, Korth and Sudarshan
2.77
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.15
78. ŠSilberschatz, Korth and Sudarshan
2.78
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.16
79. ŠSilberschatz, Korth and Sudarshan
2.79
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.17
Largest account balance in the bank
80. ŠSilberschatz, Korth and Sudarshan
2.80
Database System Concepts - 5th Edition, Oct 5, 2006
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, Oct 5, 2006
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, Oct 5, 2006
Figure 2.20
83. ŠSilberschatz, Korth and Sudarshan
2.83
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.21
84. ŠSilberschatz, Korth and Sudarshan
2.84
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.22
85. ŠSilberschatz, Korth and Sudarshan
2.85
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.23
86. ŠSilberschatz, Korth and Sudarshan
2.86
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.24: The credit_info relation
87. ŠSilberschatz, Korth and Sudarshan
2.87
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.25
88. ŠSilberschatz, Korth and Sudarshan
2.88
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.26: The pt_works relation
89. ŠSilberschatz, Korth and Sudarshan
2.89
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.27
The pt_works relation after regrouping
90. ŠSilberschatz, Korth and Sudarshan
2.90
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.28
91. ŠSilberschatz, Korth and Sudarshan
2.91
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.29
92. ŠSilberschatz, Korth and Sudarshan
2.92
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.30
The employee and ft_works relations
93. ŠSilberschatz, Korth and Sudarshan
2.93
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.31
94. ŠSilberschatz, Korth and Sudarshan
2.94
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.32
95. ŠSilberschatz, Korth and Sudarshan
2.95
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.33
96. ŠSilberschatz, Korth and Sudarshan
2.96
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.34