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

More Related Content

Similar to jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb

relational algebra and calculus queries .ppt
relational algebra and calculus queries .pptrelational algebra and calculus queries .ppt
relational algebra and calculus queries .pptShahidSultan24
 
Functional dependency.pptx
Functional dependency.pptxFunctional dependency.pptx
Functional dependency.pptxssuser7e9b941
 
Intro to Relational Model
Intro to Relational ModelIntro to Relational Model
Intro to Relational ModelHazemWaheeb1
 
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docx
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docxDatabase System Concepts, 6th Ed.ŠSilberschatz, Korth and .docx
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docxrandyburney60861
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptxRUpaliLohar
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part iParthNavale
 
ch6.ppt
ch6.pptch6.ppt
ch6.pptShobi29
 
Lecture_W9_Normalization.ppt
Lecture_W9_Normalization.pptLecture_W9_Normalization.ppt
Lecture_W9_Normalization.pptSadiaArifinSmrity
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages uoitc
 
Query optimization
Query optimizationQuery optimization
Query optimizationNeha Behl
 

Similar to jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb (20)

relational algebra and calculus queries .ppt
relational algebra and calculus queries .pptrelational algebra and calculus queries .ppt
relational algebra and calculus queries .ppt
 
Ch3 a
Ch3 aCh3 a
Ch3 a
 
Ch3
Ch3Ch3
Ch3
 
Functional dependency.pptx
Functional dependency.pptxFunctional dependency.pptx
Functional dependency.pptx
 
Ch7
Ch7Ch7
Ch7
 
Intro to Relational Model
Intro to Relational ModelIntro to Relational Model
Intro to Relational Model
 
Ch2
Ch2Ch2
Ch2
 
Ch2
Ch2Ch2
Ch2
 
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docx
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docxDatabase System Concepts, 6th Ed.ŠSilberschatz, Korth and .docx
Database System Concepts, 6th Ed.ŠSilberschatz, Korth and .docx
 
Relational algebra.pptx
Relational algebra.pptxRelational algebra.pptx
Relational algebra.pptx
 
Lec02
Lec02Lec02
Lec02
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
ch6.ppt
ch6.pptch6.ppt
ch6.ppt
 
Lecture_W9_Normalization.ppt
Lecture_W9_Normalization.pptLecture_W9_Normalization.ppt
Lecture_W9_Normalization.ppt
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
Ch14
Ch14Ch14
Ch14
 
ch3[1].ppt
ch3[1].pptch3[1].ppt
ch3[1].ppt
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
Ch7
Ch7Ch7
Ch7
 

More from WrushabhShirsat3

unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptunit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptWrushabhShirsat3
 
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFptWrushabhShirsat3
 
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...WrushabhShirsat3
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufWrushabhShirsat3
 
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiuem150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiueWrushabhShirsat3
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytWrushabhShirsat3
 
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhdcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhWrushabhShirsat3
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawWrushabhShirsat3
 
Ch01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfCh01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfWrushabhShirsat3
 
chapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptchapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptWrushabhShirsat3
 

More from WrushabhShirsat3 (20)

unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptunit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
 
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
 
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiuem150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhdcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudaw
 
lecture1-intro.ppt
lecture1-intro.pptlecture1-intro.ppt
lecture1-intro.ppt
 
IntroT.ppt
IntroT.pptIntroT.ppt
IntroT.ppt
 
scan.pptx
scan.pptxscan.pptx
scan.pptx
 
2.pptx
2.pptx2.pptx
2.pptx
 
papp01.pptx
papp01.pptxpapp01.pptx
papp01.pptx
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
asabydqwfdytwdv.ppt
asabydqwfdytwdv.pptasabydqwfdytwdv.ppt
asabydqwfdytwdv.ppt
 
Ch01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfCh01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdf
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
chapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptchapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
ABCD.ppt
ABCD.pptABCD.ppt
ABCD.ppt
 

Recently uploaded

PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024CristobalHeraud
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...babafaisel
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVAAnastasiya Kudinova
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
ARt app | UX Case Study
ARt app | UX Case StudyARt app | UX Case Study
ARt app | UX Case StudySophia ViganÃ˛
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CANestorGamez6
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,bhuyansuprit
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
NATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailNATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailDesigntroIntroducing
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
How to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our SiteHow to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our Sitegalleryaagency
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝soniya singh
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...Suhani Kapoor
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 

Recently uploaded (20)

PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
PORTFOLIO DE ARQUITECTURA CRISTOBAL HERAUD 2024
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
Kala jadu for love marriage | Real amil baba | Famous amil baba | kala jadu n...
 
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
PORTAFOLIO   2024_  ANASTASIYA  KUDINOVAPORTAFOLIO   2024_  ANASTASIYA  KUDINOVA
PORTAFOLIO 2024_ ANASTASIYA KUDINOVA
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi đŸĢĻ No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi đŸĢĻ No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi đŸĢĻ No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi đŸĢĻ No Advance VVIP 🍎 SER...
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
ARt app | UX Case Study
ARt app | UX Case StudyARt app | UX Case Study
ARt app | UX Case Study
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
 
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
Bus tracking.pptx ,,,,,,,,,,,,,,,,,,,,,,,,,,
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
NATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detailNATA 2024 SYLLABUS, full syllabus explained in detail
NATA 2024 SYLLABUS, full syllabus explained in detail
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
How to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our SiteHow to Be Famous in your Field just visit our Site
How to Be Famous in your Field just visit our Site
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi đŸ’¯Call Us 🔝8264348440🔝
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 

jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb

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