SlideShare a Scribd company logo
1 of 96
Database System Concepts, 5th Ed.
ŠSilberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 2: Relational Model
Š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
ŠSilberschatz, Korth and Sudarshan
2.3
Database System Concepts - 5th Edition, Oct 5, 2006
Example of a Relation
Š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
Š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
Š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)
Š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)
Š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
Š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
ŠSilberschatz, Korth and Sudarshan
2.10
Database System Concepts - 5th Edition, Oct 5, 2006
The customer Relation
ŠSilberschatz, Korth and Sudarshan
2.11
Database System Concepts - 5th Edition, Oct 5, 2006
The depositor Relation
Š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.
Š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
Š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
Š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.
Š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.
Š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
Š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)
Š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)
Š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 ī‹
īƒ•
Š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
Š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)
Š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
Š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
Š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
Š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.
Š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
Š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
ī˛
Š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)
Š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)
Š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)))
Š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)))
Š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)))
Š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
Š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
Š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)
Š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
Š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))
Š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
Š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
Š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
Š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
Š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.
Š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.
Š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)
Š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.
Š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))
ŠSilberschatz, Korth and Sudarshan
2.48
Database System Concepts - 5th Edition, Oct 5, 2006
Extended Relational-Algebra-Operations
īŽ Generalized Projection
īŽ Aggregate Functions
īŽ Outer Join
Š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
īƒ•
Š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 ī‹
ī‹
īŠ
Š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
Š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
Š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)
Š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
Š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
Š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
Š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
Š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)
Š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
Š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.
Š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.
Š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 )
Š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.
Š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)
Š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 ī‹
īƒ•
ī‚Ŧ
Š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)
Database System Concepts, 5th Ed.
ŠSilberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
End of Chapter 2
ŠSilberschatz, Korth and Sudarshan
2.68
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.3. The branch relation
ŠSilberschatz, Korth and Sudarshan
2.69
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.6: The loan relation
ŠSilberschatz, Korth and Sudarshan
2.70
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.7: The borrower relation
ŠSilberschatz, Korth and Sudarshan
2.71
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.9
Result of īŗbranch_name = “Perryridge” (loan)
Š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
Š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
ŠSilberschatz, Korth and Sudarshan
2.74
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.12:
Customers with an account but no loan
ŠSilberschatz, Korth and Sudarshan
2.75
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.13: Result of borrower |X| loan
ŠSilberschatz, Korth and Sudarshan
2.76
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.14
ŠSilberschatz, Korth and Sudarshan
2.77
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.15
ŠSilberschatz, Korth and Sudarshan
2.78
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.16
ŠSilberschatz, Korth and Sudarshan
2.79
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.17
Largest account balance in the bank
Š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
Š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
ŠSilberschatz, Korth and Sudarshan
2.82
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.20
ŠSilberschatz, Korth and Sudarshan
2.83
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.21
ŠSilberschatz, Korth and Sudarshan
2.84
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.22
ŠSilberschatz, Korth and Sudarshan
2.85
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.23
ŠSilberschatz, Korth and Sudarshan
2.86
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.24: The credit_info relation
ŠSilberschatz, Korth and Sudarshan
2.87
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.25
ŠSilberschatz, Korth and Sudarshan
2.88
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.26: The pt_works relation
ŠSilberschatz, Korth and Sudarshan
2.89
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.27
The pt_works relation after regrouping
ŠSilberschatz, Korth and Sudarshan
2.90
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.28
ŠSilberschatz, Korth and Sudarshan
2.91
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.29
ŠSilberschatz, Korth and Sudarshan
2.92
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.30
The employee and ft_works relations
ŠSilberschatz, Korth and Sudarshan
2.93
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.31
ŠSilberschatz, Korth and Sudarshan
2.94
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.32
ŠSilberschatz, Korth and Sudarshan
2.95
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.33
ŠSilberschatz, Korth and Sudarshan
2.96
Database System Concepts - 5th Edition, Oct 5, 2006
Figure 2.34

More Related Content

Similar to Database System Concepts Chapter 2: Relational Model Summary

Similar to Database System Concepts Chapter 2: Relational Model Summary (20)

Ch3
Ch3Ch3
Ch3
 
Ch 2.pdf
Ch 2.pdfCh 2.pdf
Ch 2.pdf
 
ch2
ch2ch2
ch2
 
Ch2
Ch2Ch2
Ch2
 
Ch2
Ch2Ch2
Ch2
 
Relational Algebra and relational queries .ppt
Relational Algebra and relational queries .pptRelational Algebra and relational queries .ppt
Relational Algebra and relational queries .ppt
 
ch3[1].ppt
ch3[1].pptch3[1].ppt
ch3[1].ppt
 
Ch3
Ch3Ch3
Ch3
 
Functional dependency.pptx
Functional dependency.pptxFunctional dependency.pptx
Functional dependency.pptx
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
Ch7
Ch7Ch7
Ch7
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
Intro to Relational Model
Intro to Relational ModelIntro to Relational Model
Intro to Relational Model
 
Ch14
Ch14Ch14
Ch14
 
Lec02
Lec02Lec02
Lec02
 
relational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptrelational model in Database Management.ppt.ppt
relational model in Database Management.ppt.ppt
 
Lllll
LllllLllll
Lllll
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
 
Lecture_W9_Normalization.ppt
Lecture_W9_Normalization.pptLecture_W9_Normalization.ppt
Lecture_W9_Normalization.ppt
 

Recently uploaded

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

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