0% found this document useful (0 votes)
43 views43 pages

Relational Model Relational Model

Uploaded by

navneetccna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views43 pages

Relational Model Relational Model

Uploaded by

navneetccna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

CHAPTER 2

RELATIONAL MODEL
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

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 2
Example of a Relation

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 3
Basic Structure
ƒ Formally,
ggiven
v sets D1, D2, …. Dn a relation r is a subset
b of D1 x D2 x … x Dn

R ⊆ D1, X ….. X Dn (n: degree of R)


or
R = { <d1, ..., dn> | d1 ∈ D1, ..., dn ∈ Dn } (set of tuples)

ƒ Example: 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

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 4
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
A ib values l are ((normally)
ll ) required
i d to be
b atomic,
i that
h iis,
indivisible
à E.g.
E g multivalued attribute values are not atomic
à E.g. 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
p
à we shall ignore the effect of null values in our main presentation and
consider their effect later

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 5
Relation Schema
ƒ A1, A2, …, An are attributes
ƒ R = (A1, A2, …, An ) is a relation schema
E.g. Customer-schema = (customer-name, customer-street, customer-city)
ƒ r(R) is a relation (variable) on the relation schema R
E.g. customer(Customer-schema)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 6
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

attributes

customer-name
customer name customer
customer-street
street customer-city
customer city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye
Lindsay Park Pittsfield

customer

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 7
Relations are Unordered
ƒ Order of tuples is irrelevant (tuples may be stored in an arbitrary
order)
ƒ E.g. account relation with unordered tuples

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 8
Relational 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
g account : information about accounts
E.g.:
customer : information about customers
depositor : information about which customer owns which
account

ƒ Storing all information as a single relation such as results in


à repetition
i i off information
i f i (e.g.
( two customers own an account))
à the need for null values (e.g. represent a customer without an account)
bank(account-number, balance, customer-name, ... )

ƒ Relational database design (Chapter 6 & 7) deals with how to


design
g relational schemas

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 9
The customer relation & depositor relation

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 10
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
B “possible
“ ibl r”” we mean a relation
l i r that
h couldld exist
i iin the
h
enterprise we are modeling.
à Example: {customer-name,
{customer name customer-street}
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}
{customer name} is a candidate key for Customer,
Customer
since it is a superkey, and no subset of it is a superkey.

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 11
Schema Diagram for the Banking Enterprise

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 12
Query Languages
ƒ Language in which user requests information from the database.
ƒ Categories of languages
à procedural
‚ specify what data are needed and how to get those data
à nonprocedural
‚ declarative
‚ specify only what data are needed
ƒ “Pure” languages:
à Relational
R l ti l Algebra
Al b
à Tuple Relational Calculus
à Domain Relational Calculus
ƒ Pure languages form underlying basis of query languages that
people
p p use.

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 13
Relational Algebra
ƒ Algebra : operators and operands
à Relational algebra
‚ operands : relations
‚ operators : basic operators (+ additional operations)
 take
k two or more relations
l i as inputs
i andd give
i a new relation
l i as a result.
l
ƒ Procedural language
ƒ 6 Fundamental Operators
à select
à project
à union
à set difference
à Cartesian product
à rename

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 14
Select Operation – Example

„ Relation r: A B C D

α α 1 7
α β 5 7
β β 12 3
β β 23 10

„ σA=B ^ D>5 (r) :


A B C D

α α 1 7
β β 23 10

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 15
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),
(and) ∨ (or),
(or) ¬ (not)
à Each term is one of:
<attribute> opp <attribute> or <constant>
where op is one of: =, ≠, >, ≥, <, ≤
ƒ Example:
p
σ branch-name=“Perryridge”(account)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 16
Project Operation – Example

ƒ Relation r: A B C

α 10 1
α 20 1
β 30 1
β 40 2

„ ∏A,C (r) A C A C

α 1 α 1
α 1 = β 1
β 1 β 2
β 2

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 17
Project Operation
ƒ Notation:
∏A1, A2, …, Ak (r)
()
where A1, A2 are attribute names and r is a relation name.
ƒ The
Th resultl is
i defined
d fi d as the
h relation
l i off k columns
l obtained
b i db by
erasing the columns that are not listed
ƒ Duplicate
D li t rows removedd from f result
lt – since
i relations
l ti are sets
t
ƒ Example: To eliminate the branch-name attribute of account

ƒ ∏account-number, balance (account)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 18
Union Operation – Example

ƒ Relations r, s: A B A B

α 1 α 2
α 2 β 3
β 1
s
r

ƒ r ∪ s: A B

α 1
α 2
β 1
β 3

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 19
Union Operation
ƒ Notation: r ∪ s
ƒ Defined
D fi d as:
r ∪ s = {t | t∈r or t∈s}

ƒ For r ∪ s to be valid (Union compatible)


11. rr, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type
yp of values as does the 2nd
column of s)
à E.g.: to find all customers with either an account or a loan

∏customer-name (depositor) ∪ ∏customer-name (borrower)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 20
Set Difference Operation – Example

ƒ Relations r, s: A B A B

α 1 α 2
α 2 β 3
β 1 s
r

ƒ r – s: A B

α 1
β 1

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 21
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
ib ddomains
i off r and
d s must be
b compatible
ibl

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 22
Cartesian-Product Operation – Example

ƒ Relations r, s: A B C D E

α 1 α 10 a
β 10 a
β 2
β 20 b
r γ 10 b
s

ƒ r x s: A B C D E

α 1 α 10 a
α 1 β 19 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
Original Slides: Intro to DB (2008-1)
© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 23
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; i.e.,


R ∩ S = ∅.
à If not, renaming of attributes is needed.

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 24
Composition of Operations
ƒ Can build expressions using multiple operations
ƒ Example: σA=C(r x s)
ƒ rxs
A B C D E
α 1 α 10 a
α 1 β 19 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b

ƒ σA=C(r x s) A B C D E
α 1 α 10 a
β 2 β 20 a
Original Slides:
β 2 β 20 b
Intro to DB (2008-1)
© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 25
Rename Operation
ƒ Allows us to name, and therefore to refer to, the results of
relational-algebra
relational algebra eexpressions.
pressions
ƒ Allows us to refer to a relation by more than one name.

ƒ ρN (E) returns the expression E under the name N

ƒ If a relational-algebra expression E has arity n, then


ρN (A1, A2, …, An) (E)

returns the
h result
l off expression E under
d the
h name N, and
d
with the attributes renamed to A1, A2, …., An.

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 26
Banking Example

branch (branch-name
(branch name, branch-city
branch city, assets)
customer (customer-name, customer-street, customer-only)

account (account-number, branch-name, balance)

loan (loan-number, branch-name, amount)

d p it ((customer-name,
depositor t account-number)
t b )

borrower ((customer-name,, loan-number))

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 27
Example Queries
ƒ Find all loans of over $1200
σamount >1200 (loan)

ƒ Find the loan number for each loan of an amount greater than
$1200
∏loan-number (σamount>1200 (loan))

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 28
Example Queries
ƒ Find the names of all customers who have a loan, an account, or
both from the bank
both,

∏customer-name
t (borrower) ∪ ∏customer-name
t (depositor)

ƒ Find the names of all customers who have a loan and an account
at bank.

∏customer-name (borrower) ∩ ∏customer-name (depositor)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 29
Example Queries
ƒ Find the names of all customers who have a loan at the Perryridge branch.
∏customer-name (σbranch-name=“Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))

ƒ Find the names of all customers who have a loan at the Perryridge branch but do not
have an account at anyy branch of the bank.
∏customer-name (σbranch-name = “Perryridge”
(σborrower.loan-number
b l b (borrower
b = loan.loan-number
l l x loan)))

– ∏customer-name(depositor)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 30
Example Queries
ƒ Find the names of all customers who have a loan at the Perryridge branch.

Query 1
∏customer-name(σbranch-name = “Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))

Query 2
∏customer-name(
((σloan.loan-number borrower loan-number(
loan loan-number = borrower.loan-number
( σbranch-name = “Perryridge”(loan) ) x borrower
)
)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 31
Example Queries
ƒ Find the largest account balance
à Rename
R account relation
l i as d
à The query is:

∏balance(account) –
∏account.balance
b l (
σaccount.balance < d.balance (account x ρd (account) )
)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 32
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
à ρN (E1), N is the new name for the result of E1

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 33
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

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 34
Set-Intersection Operation
ƒ Notation: r ∩ s
ƒ Defined as:
r ∩ s ={ t | t ∈ r and t ∈ s }

ƒ Assume union compatibility:


à r, s have the same arity
à attributes of r and s are compatible

ƒ Note: r∩s=r–(r–s)

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 35
Set-Intersection Operation – Example
ƒ Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1

r s

A B
ƒ r∩s
α 2

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 36
Natural-Join Operation
ƒ Notation: r s
ƒ Let r and s be relations on schemas R and S,, respectively.
p y The result is a relation on
schema R ∪ S which is obtained by considering 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, a tuple
p t is added
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,
(E B,
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))

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 37
Natural-Join Operation – Example
ƒ Relations r, s:

A B C D B D E

α 1 α a 1 a α
β 2 γ a 3 a β
γ 4 β b 1 a γ
α 1 γ a 2 b δ
δ 2 β b 3 b ∈
r s

„ r s
A B C D E
α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ
Original Slides: Intro to DB (2008-1)
© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 38
Properties
ƒ ∏ A1,…,Ak(r) ∩ ∏ A1,…,Ak(s) = ∏ A1 ,…,Ak (r s)
ƒ (r
( s)) t=r ((s t))
ƒ If R∩S=∅ then r s = r × s (important)
ƒ If R=S
R S then
th r s=r∩s

ƒ Theta
Th t Join
J i
à combine selection with Cartesian product
r θs = σθ (r × s)
where θ is a predicate on R×S

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 39
Assignment Operation
ƒ The assignment operation (←) provides a convenient way to
express complex queries,
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)
p ← ∏R-S
temp2 R S(( p × s)) – ∏R-S,S
((temp1 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 the variable in subsequent expressions.
expressions

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 40
Example Queries
ƒ Find all customers who have an account from at least the
“Do nto n” and the “Uptown
“Downtown “Upto n” branches.
branches
à Query 1
∏customer-name(σbranch-name=“Downtown”(depositor account)) ∩

∏customer-name(σbranch-name=“Uptown”(depositor account))

à Query 2
∏customer-name, branch-name (depositor
(d p it account)
t)
÷ ρtemp(branch-name) ({(“Downtown”), (“Uptown”)})

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 41
Example Queries

ƒ Find all customers who have an account at all branches located in


Brooklyn city.

∏customer-name, branch-name (depositor account)


÷ ∏branch-name
b h (σbranch-city “B kl ” (branch))
b h it = “Brooklyn”

Original Slides: Intro to DB (2008-1)


© Silberschatz, Korth and Sudarshan Copyright © 2006 - 2008 by S.-g. Lee Chap 2 - 42
END OF CHAPTER 2

You might also like