Chapter 2: Relational Model
Chapter 2: Relational Model
Chapter 2: Relational Model
2.2
Example of a Relation
2.3
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
2.5
Relation Schema
A1, A2, %, An are attributes
Example:
Customer_schema = (customer_name, customer_street, customer_city)
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
(or columns)
customer_name customer_street customer_city
customer
2.7
2.8
Database
A database consists of multiple relations
2.10
The depositor Relation
2.11
Keys
Let K ⊆ R
K is a superkey of R if values for K are sufficient to identify a unique tuple of
each possible relation r(R)
by “possible r ” we mean a relation r that could exist in the enterprise we
are modeling.
Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly have
the same name
In real life, an attribute such as customer_id would be used instead of
customer_name to uniquely identify customers, but we omit it to keep
our examples small, and instead assume customer names are unique.
2.12
Keys (Cont.)
2.13
Foreign Keys
A relation schema may have an attribute that corresponds to the primary
key of another relation. The attribute is called a foreign key.
E.g. customer_name and account_number attributes of depositor are
foreign keys to customer and account respectively.
Only values occurring in the primary key attribute of the referenced
relation may occur in the foreign key attribute of the referencing
relation.
Schema diagram
2.14
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.
2.15
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.
2.16
Select Operation – Example
Relation r
A B C D
α α 1 7
α β 5 7
β β 12 3
β β 23 10
α α 1 7
β β 23 10
2.17
Select Operation
Notation: σ p(r)
p is called the selection predicate
Defined as:
Example of selection:
σ branch_name=“Perryridge”(account)
2.18
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
2.19
Project Operation
Notation:
∏ A1 , A2 ,K, Ak (r )
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
2.20
Union Operation – Example
Relations r, s: A B A B
α 1 α 2
α 2 β 3
β 1 s
r
A B
r ∪ s: α 1
α 2
β 1
β 3
2.21
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)
2.22
Set Difference Operation – Example
Relations r, s:
A B A B
α 1 α 2
α 2 β 3
β 1 s
r
r – s:
A B
α 1
β 1
2.23
2.24
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 β 10 a
α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b
2.25
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.
2.26
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 β 10 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 β 10 a
β 2 β 20 b
2.27
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)
ρ x ( A ,A
1 2 ,..., A n ) (E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , 4., An .
2.28
Banking Example
branch (branch_name, branch_city, assets)
2.29
Example Queries
Find all loans of over $1200
Find the loan number for each loan of an amount greater than
$1200
Find the names of all customers who have a loan, an account, or both,
from the bank
2.30
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 any branch of
the bank.
2.31
Example Queries
Find the names of all customers who have a loan at the Perryridge branch.
Query 1
Query 2
∏customer_name(σloan.loan_number = borrower.loan_number (
(σbranch_name = “Perryridge” (loan)) x borrower))
2.32
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)))
2.33
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
Set intersection
Natural join
Division
Assignment
2.35
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)
2.36
Set-Intersection Operation – Example
Relation r, s:
A B A B
α 1 α 2
α 2 β 3
β 1
r s
r∩s
A B
α 2
2.37
Natural-Join Operation
Notation: r s
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))
2.38
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 δ
2.39
Division Operation
Notation: r÷s
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
2.40
Division Operation – Example
Relations r, s:
A B
B
α 1 1
α 2
α 3 2
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6
∈ 1
β 2
r ÷ s: A r
α
β
2.41
α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1 s
β a γ a 1
β a γ b 3
γ a γ a 1
γ a γ b 1
γ a β b 1
r
r ÷ s:
A B C
α a γ
γ a γ
2.42
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
To see why
∏R-S,S (r) simply reorders attributes of r
2.43
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
2.44
Bank Example Queries
Find the names of all customers who have a loan and an account at
bank.
Find the name of all customers who have a loan at the bank and the
loan amount
2.45
Query 2
∏customer_name, branch_name (depositor account)
÷ ρtemp(branch_name) ({(“Downtown” ), (“Uptown” )})
Note that Query 2 uses a constant relation.
2.46
Bank Example Queries
Find all customers who have an account at all branches located in
Brooklyn city.
2.47
Extended Relational-Algebra-Operations
Generalized Projection
Aggregate Functions
Outer Join
2.48
Generalized Projection
Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
∏F ,F ,...,F (E)
1 2 n
2.49
G1,G2 ,K,Gn
ϑF ( A ),F ( A ,K,F ( A ) (E )
1 1 2 2 n n
2.50
Aggregate Operation – Example
Relation r:
A B C
α α 7
α β 7
β β 3
β β 10
27
2.51
2.52
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
2.53
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:
2.54
Outer Join – Example
Relation loan
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
2.55
loan borrower
2.56
Outer Join – Example
Right Outer Join
loan borrower
2.57
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.
For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same (as in SQL)
2.58
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
2.59
2.60
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.
2.61
Deletion Examples
Delete all account records in the Perryridge branch.
2.62
Insertion
2.63
Insertion Examples
Insert information in the database specifying that Smith has $1200 in
account A-973 at the Perryridge branch.
2.64
Updating
r ← ∏ F ,F ,K,F , (r )
1 2 l
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.65
Update Examples
2.66
End of Chapter 2