CSC311: Database Systems Chap 3: Relational Model: Lecture Contents
CSC311: Database Systems Chap 3: Relational Model: Lecture Contents
1
Basic Structure
A relational database consists of a collection of tables, each of
which is assigned a unique name.
A row(or tuple) in a table represents a relationship among a set of values.
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
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)} 2
is a relation over customer-name x customer-street x customer-city
Relation Schema
A1, A2, …, An are attributes
R = (A1, A2, …, An ) is a relation schema
E.g.:
3
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 4
Relations are Unordered
Order of tuples is irrelevant (tuples may be
stored in an arbitrary order)
E.g. account relation with unordered tuples
5
Schema for the Bank Enterprise
depositor=(customer-name, account-number)
borrower=(customer-name, loan-number)
6
Relational Algebra
The relational algebra is a Procedural query language
Six basic operators
select
project
union
set difference
cartesian product
rename
The operators take two or more relations as inputs and give a
new relation as a result.
In addition to the fundamental operations, there are several
other operations. E.g, set intersection, natural join, division, 7
assignment.
Select Operation
Notation: σ p(r)
p is called the selection predicate
Greek letter Sigma (σ ) for select operation
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) 8
Select Operation – Example
• Relation r A B C D
α α 1 7
α β 5 7
β β 12 3
β β 23 10
α α 1 7
β β 23 10
9
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 10
Project Operation
Notation:
∏A1, A2, …, 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
E.g. To eliminate the branch-name attribute of account
12
Union Operation – Example
Relations r, s:
A B A B r ∪ s: A B
α 1 α 2 α 1
r s
α 2 β 3 α 2
β 1 β 1
β 3
E.g. to find all customers with either an account or a loan,
or both
13
Set Difference Operation – Example
Relations r, s: A B A B
α 1 α 2
α 2 β 3
β 1 s
r
r – s: A B
α 1
β 1
14
Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t ∈ r and t ∉ s}
15
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
β γ
16
2 10 b
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t ∈ r and q ∈ s}
17
Composition of Operations
Can build expressions using multiple operations
Example: σA =C(r x s) A B C D E
rxs α 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 β 20 a
β 2 β 20 b 18
Rename Operation
Allows us to name, and therefore to refer to, the results of
relational-algebra expressions.
Denoted by Greek letter rho ( ρ)
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
ρx (A1, A2, …, An) (E)
returns the result of expression E under the name X, and
with the attributes renamed to A1, A2, …., An.
19
Schema for the Bank Enterprise
depositor=(customer-name, account-number)
borrower=(customer-name, loan-number)
20
Example Queries
Find all loans of over $1200
σamount> 1200 (loan)
21
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.
∏customer-name (σbranch-name = “Perryridge”
– ∏customer-name(depositor)
22
Example Queries
Find the largest account balance in the bank
First, compute a temporary relation consisting of
those balances that are not the largest and then
take set difference.
Rename account relation as d
23
Example Queries
account account x ρd (account)
Ac Br Bl Ac Br Bl dAc dBr dBl
5 D 10 5 D 10 5 D 10
6 C 20 5 D 10 6 C 20
7 S 30 5 D 10 7 S 30
6 C 20 5 D 10
ρd (account) 6 C 20 6 C 20
6 C 20 7 S 30
dAc dBr dBl 7 S 30 5 D 10
5 D 10 7 S 30 6 C 20
6 C 20 7 S 30 7 S 30
7 S 30
Bl Bl Bl
10 _ 10
20 30
20
30 24
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
25
Set-Intersection Operation
Notation: r ∩ s
Defined as:
r ∩ s ={ t | t ∈ r and t ∈ s }
Assume:
r,s have the same number of attributes
attributes of r and s are compatible
Note: r ∩ s = r - (r - s)
26
Set-Intersection Operation - Example
Relation r, s: A B A B
α 1 α 2
α 2 β 3
β 1
r s
A B
r∩s α 2
α 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 δ 29
Division Operation
Notation: r ÷ s
Suited to queries that include the phrase “for all”.
Find all customers who have an account at all the
branches located in Brooklyn.
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
α 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 γ
32
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: Select all loan records with amount in the
range of 0 to 50
l1 ← σ amount ≥ 0 and amount ≤ 50 (loan)
The result to the right of the ← is assigned to the relation
variable on the left of the ←.
33
May use variable in subsequent expressions.
Example Query
1. Find all customer name who have an account from at
least the “Downtown” and the “Redwood” branches.
Query 1:
∏CN(σBN=“Downtown”(depositor account)) ∩
∏CN(σBN=“Redwood”(depositor account))
34
Example Queries
Find all customers who have an account from at least the
“Downtown” and the “Uptown” branches.
Query 2:
35
Example Queries
2. Find all customer name who have an account at
all branches located in Brooklyn city.
Query is:
36
Extended Relational-Algebra-Operations
The basic relational algebra has been
extended in several ways.
A simple extension is to allow arithmetic
operations in the projection operation,
arithmetic operation in a specific attribute
etc.
GeneralizedProjection
Aggregate Functions
Outer Join 37
Generalized Projection
Extends the projection operation by allowing arithmetic
functions to be used in the projection list.
∏ F1, F2, …, Fn(E), E is any relational-algebra expression
α α 7
α β 7
β β 3
β β 10
27
40
Aggregate Operation – Example
Find the total account balance of each branch
branch-name g sum(balance) (account)
Relation account grouped by branch-name:
branch-name account-number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700
branch-name balance
Result of the Perryridge 1300
query: Brighton 1500
Redwood 700 41
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
43
Aggregate Functions (Cont..)
Solution:
44
Aggregate Functions (Cont..)
Find the maximum salary for the part-time employees at
each branch, in addition to the sum of salaries, of the
bank separately.
Solution:
45
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 (← ).
46
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.
47
Deletion Examples
Delete all account records in the Banani branch.
account ← account – σ branch-name = “Banani” (account)
Delete all loan records with amount in the range of 0 to 50
loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan)
Delete all accounts at branches located in Dhaka city. (Note, need
to delete corresponding records from depositor table also)
r1 ← σ branch-city = “Dhaka” (account branch)
r2 ← ∏account-number, branch-name, balance (r1)
r3 ← ∏ customer-name, account-number (r2 depositor)
account ← account – r2
depositor ← depositor – r3
48
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.
49
Insertion Examples
Insert information in the database specifying that Smith has
$1200 in account A-973 at the Perryridge branch.
account ← account ∪ {(“Perryridge”, “A-973”, 1200)}
depositor ← depositor ∪ {(“Smith”, “A-973”)}
50
Updating
A mechanism to change a value in a tuple without
changing all values in the tuple
Use the generalized projection operator to do this task
r ← ∏ F1, F2, …, FI, (r)
Each F, is either the ith attribute of r, if the ith 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
51
Update Examples
52
Views
In some cases, it is not desirable for all users to see
the entire logical model (i.e., all the actual relations
stored in the database.)
Consider a person who needs to know a customer’s
loan number but has no need to see the loan amount.
This person should see a relation described, in the
relational algebra, by
∏customer-name, loan-number (borrower loan)
Any relation that is not of the conceptual model but is
made visible to a user as a “virtual relation” is called a
view.
53
View Definition
A view is defined using the create view statement
which has the form
create view v as <query expression>
where <query expression> is any legal relational
algebra query expression. The view name is
represented by v.
Once a view is defined, the view name can be used
to refer to the virtual relation that the view generates.
View definition is not the same as creating a new
relation by evaluating the query expression
54
View Examples
Consider the view (named all-customer) consisting of
branches and their customers.
create view all-customer as
∏branch-name, customer-name (depositor account)
∪ ∏branch-name, customer-name (borrower loan)