0% found this document useful (0 votes)
76 views

CSC311: Database Systems Chap 3: Relational Model: Lecture Contents

The document summarizes the contents of Chapter 3 of the CSC311 Database Systems course, which covers the relational model. It discusses the basic structure of relational databases including tables, rows, tuples, relations, and relation schemas. It also describes the six basic relational query operators - select, project, union, set difference, cartesian product, and rename - and provides examples of how to use them to write queries on a sample bank database schema.
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)
76 views

CSC311: Database Systems Chap 3: Relational Model: Lecture Contents

The document summarizes the contents of Chapter 3 of the CSC311 Database Systems course, which covers the relational model. It discusses the basic structure of relational databases including tables, rows, tuples, relations, and relation schemas. It also describes the six basic relational query operators - select, project, union, set difference, cartesian product, and rename - and provides examples of how to use them to write queries on a sample bank database schema.
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/ 56

CSC311 : Database Systems

Chap 3: Relational Model


Lecture Contents:
„ Basic Structure of Relational Database
„ Relational Schema Diagram
„ Six basic relational query operators
„ Additional relational query operators
„ Generalized Project
„ Database Modification operators
„ Aggregate Functions
„ View Definitions

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.:

Customer-schema = (customer-name, customer-street,


customer-city)
„ r(R) is a relation on the relation schema R
E.g. customer (Customer-schema)

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-name customer-street customer-city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye
Lindsay Park Pittsfield

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

branch= (branch-name, branch-city, assets)

customer= (customer-name, customer-street, cust-city)

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

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

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

• σA=B Λ D > 5 (r) A B C D

α α 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

∏account-number, balance (account)


11
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 (e.g., 2nd
column of r deals with the same type of values as does
the 2nd column of s)

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

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

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}

„ Set differences must be taken between


compatible relations.
„ r and s must have the same number of attributes
„ attribute domains of r and s must be compatible

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}

„ 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.

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

branch= (branch-name, branch-city, assets)

customer= (customer-name, customer-street, cust-city)

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

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

depositor=(customer-name, account-number)

borrower=(customer-name, loan-number)
20
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))

„ Find the names of those customers who live in Harrison


∏customer-name (σcustomer-city = “Harrison” (customer))

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”

(σborrower.loan-number = loan.loan-number(borrower x loan)))

– ∏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

„ The query is:


∏balance(account) - ∏account.balance
(σaccount.balance < d.balance (account x ρd(account)))

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

„ Find the names of all customers who have a loan and an


account at bank.
∏customer-name (borrower) ∩ ∏customer-name (depositor)
27
Natural-Join Operation
„ Notation: r s
„ Let r and s be relations on schemas R and S respectively.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 t is added to the result, where
„ t has the same value as t on r
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))
28
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 δ 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

R – S = (A1, …, Am) and tuples:


r ÷ s = { t | t ∈ ∏ R-S(r) ∧ ∀ u ∈ s ( t u ∈ r ) }
30
Division Operation – Example
Relations r, s: A B B
α 1 1
α 2
α 3 2
β 1 s
γ 1
δ 1
δ 3
δ 4
∈ 6 r ÷ s: A
∈ 1
β 2 α
r β
31
Another Division Example
Relations r, s:
A B C D E D E

α 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:

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


÷ ρtemp(branch-name) ({(“Downtown”), (“Uptown”)})

35
Example Queries
2. Find all customer name who have an account at
all branches located in Brooklyn city.
„ Query is:

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


÷ ∏branch-name (σbranch-city = “Brooklyn” (branch))

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

„ Each of F1, F2, …, Fn 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)
∏customer-name, (limit – credit-balance) as credit-available (credit-info)
38
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

G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)


„ 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 39
Aggregate Operation – Example
„ Relation r: A B C

α α 7
α β 7
β β 3
β β 10

g sum(c) (r) sum-C

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

branch-name g sum(balance) as sum-balance (account)


branch-name sum-balance
Perryridge 1300
Brighton 1500
Redwood 700
42
Aggregate Functions (Cont..)
‡ Find the total salary sum of all part-time employees at
each branch of the bank separately.
‡ pt-works relation:

43
Aggregate Functions (Cont..)
Solution:

branch-name ς sum(salary) (pt-works)

The pt-works Relation After


Grouping Result of the
Relational Query

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:

branch-name ς sum (salary) as sum-salary, max(salary)


as max-salary (pt-works)

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”)}

„ 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.
r1 ← (σbranch-name = “Perryridge” (borrower loan))
account ← account ∪ ∏ loan-number, branch-name, 200 (r1)
depositor ← depositor ∪ ∏customer-name, loan-number,(r1)

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

„ Make interest payments by increasing all balances by 5 percent.


account ← ∏ AN, BN, BAL * 1.05 (account)
where AN, BN and BAL stand for account-number, branch-name
and balance, respectively.

„ Pay all accounts with balances over $10,000 a 6 percent interest


and pay all others 5 percent

account ← ∏ AN, BN, BAL * 1.06 (σ BAL > 10000 (account))


∪ ∏AN, BN, BAL * 1.05 (σBAL ≤ 10000 (account))

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)

„ We can find all customers of the Perryridge branch by


writing:
∏ customer-name
(σbranch-name = “Perryridge” (all-customer))
55
Updates Through View
„ Database modifications expressed as views must be
translated to modifications of the actual relations in the
database.
„ Consider the person who needs to see all loan data in
the loan relation except amount. The view given to the
person, branch-loan, is defined as:
create view branch-loan as
∏branch-name, loan-number (loan)
„ Since we allow a view name to appear wherever a
relation name is allowed, the person may write:
branch-loan ← branch-loan ∪ {(“Perryridge”, L-37)}
56

You might also like