Relational Model
Database System Concepts - 5th Edition, June 15, 2005 2.1 ©Silberschatz, Korth and Sudarshan
Schema Diagram
Database System Concepts - 5th Edition, June 15, 2005 2.2 ©Silberschatz, Korth and Sudarshan
Query Languages
Language in which user requests information from the database.
Categories of languages
Procedural - Algebra
Non-procedural, or declarative - Calculus
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
Pure languages form underlying basis of query languages that people
use.
Database System Concepts - 5th Edition, June 15, 2005 2.3 ©Silberschatz, Korth and Sudarshan
Relational Algebra
Procedural language
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
Additional operations:
Intersection, join, division, renaming: Not essential, but (very!)
useful.
The operators take one or two relations as inputs and produce a new
relation as a result.
Database System Concepts - 5th Edition, June 15, 2005 2.4 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.5 ©Silberschatz, Korth and Sudarshan
Project Operation – Example
Relation r: A B C
10 1
20 1
• Deletes attributes that are not
30 1 in projection list.
• Projection operator has to
40 2
eliminate duplicates
A,C (r) A C A C
1 1
1 = 1
1 2
2
Database System Concepts - 5th Edition, June 15, 2005 2.6 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.7 ©Silberschatz, Korth and Sudarshan
Union Operation – Example
Database System Concepts - 5th Edition, June 15, 2005 2.8 ©Silberschatz, Korth and Sudarshan
Set Difference Operation – Example
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s:
A B
1
1
Database System Concepts - 5th Edition, June 15, 2005 2.9 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.10 ©Silberschatz, Korth and Sudarshan
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
x ( A ,A 1 2 ,..., An )
(E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Database System Concepts - 5th Edition, June 15, 2005 2.11 ©Silberschatz, Korth and Sudarshan
Conditions for the compatible relations
The relations r and s must be of the same arity (the degree of a
relation i.e. number of attributes).
The domains of the ith attribute of r and the domain of ith
attribute of s must be the same for all i.
Compulsory for Union, Set Difference and Intersection.
In database technology, domain refers to the description of
an attribute's allowed values.
Database System Concepts - 5th Edition, June 15, 2005 2.12 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.13 ©Silberschatz, Korth and Sudarshan
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)
Database System Concepts - 5th Edition, June 15, 2005 2.14 ©Silberschatz, Korth and Sudarshan
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 all customers who have a loan, an account, or both,
from the bank
customer_name (borrower) customer_name (depositor)
Database System Concepts - 5th Edition, June 15, 2005 2.15 ©Silberschatz, Korth and Sudarshan
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)
Database System Concepts - 5th Edition, June 15, 2005 2.16 ©Silberschatz, Korth and Sudarshan
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)))
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))
Database System Concepts - 5th Edition, June 15, 2005 2.17 ©Silberschatz, Korth and Sudarshan
Additional Operations
Additional Operations
Set intersection
Natural join
Aggregation
Outer Join
Division
All above, other than aggregation, can be expressed using basic
operations we have seen earlier
Database System Concepts - 5th Edition, June 15, 2005 2.18 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation – Example
Relation r, s:
A B A B
1 2
2 3
1
r s
r s == r – (r - s)
A B
2
Database System Concepts - 5th Edition, June 15, 2005 2.19 ©Silberschatz, Korth and Sudarshan
Joins: used to combine relations
Condition Join:
Result schema same as that of cross-product.
Fewer tuples than cross-product, might be able to
compute more efficiently
Sometimes called a theta-join.
Database System Concepts - 5th Edition, June 15, 2005 2.20 ©Silberschatz, Korth and Sudarshan
Joins
Equi-Join: A special case of condition join where the
condition c contains only equalities.
Result schema similar to cross-product, but only one copy
of fields for which equality is specified.
Natural Join: Equijoin on all common fields.
Database System Concepts - 5th Edition, June 15, 2005 2.21 ©Silberschatz, Korth and Sudarshan
sid sname rating age sid bid day
22 dustin 7 45.0 R1 22 101 10/10/96
S1
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
sid sname rating age bid day
22 dustin 7 45.0 101 10/ 10/ 96
58 rusty 10 35.0 103 11/ 12/ 96
Database System Concepts - 5th Edition, June 15, 2005 2.22 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.23 ©Silberschatz, Korth and Sudarshan
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))
Database System Concepts - 5th Edition, June 15, 2005 2.24 ©Silberschatz, Korth and Sudarshan
Bank 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) - [Link]
([Link] < [Link] (account x d (account)))
Database System Concepts - 5th Edition, June 15, 2005 2.25 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.26 ©Silberschatz, Korth and Sudarshan
Outer Join – Example
Relation loan
loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Database System Concepts - 5th Edition, June 15, 2005 2.27 ©Silberschatz, Korth and Sudarshan
Outer Join – Example
Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
Left Outer Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
Database System Concepts - 5th Edition, June 15, 2005 2.28 ©Silberschatz, Korth and Sudarshan
Outer Join – Example
Right Outer Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
Full Outer Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes
Database System Concepts - 5th Edition, June 15, 2005 2.29 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.30 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.31 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 5th Edition, June 15, 2005 2.32 ©Silberschatz, Korth and Sudarshan
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3 B1 p4
s1 p4
B2
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1
A A/B1 A/B2 A/B3
Database System Concepts - 5th Edition, June 15, 2005 2.33 ©Silberschatz, Korth and Sudarshan
Example of Division
Find all customers who have an account at all branches located in
Chville
Branch (bname, assets, bcity)
Account (bname, acct#, cname, balance)
Database System Concepts - 5th Edition, June 15, 2005 2.34 ©Silberschatz, Korth and Sudarshan
Example of Division
R1: Find all branches in Chville
R2: Find (bname, cname) pair from Account
R3: Customers in r2 with every branch name in r1
r1 ( Branch)
bname bcity 'Chville'
r 2 ( Account)
bname,cname
r3r 2 r1
Database System Concepts - 5th Edition, June 15, 2005 2.35 ©Silberschatz, Korth and Sudarshan
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 F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n
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
Database System Concepts - 5th Edition, June 15, 2005 2.37 ©Silberschatz, Korth and Sudarshan
Aggregate Operation – Example
Relation r:
A B C
7
7
3
10
g sum(c) (r) sum(c )
27
Question: Which aggregate operations cannot be
expressed using basic relational operations?
Database System Concepts - 5th Edition, June 15, 2005 2.38 ©Silberschatz, Korth and Sudarshan
Aggregate Operation – Example
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 g sum(balance) (account)
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
Database System Concepts - 5th Edition, June 15, 2005 2.39 ©Silberschatz, Korth and Sudarshan
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)
Database System Concepts - 5th Edition, June 15, 2005 2.40 ©Silberschatz, Korth and Sudarshan
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
customer_name, loan_number, amount (borrower loan)
Database System Concepts - 5th Edition, June 15, 2005 2.41 ©Silberschatz, Korth and Sudarshan
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))
Query 2
customer_name, branch_name (depositor account)
temp(branch_name) ({(“Downtown” ),
(“Uptown” )})
Database System Concepts - 5th Edition, June 15, 2005 2.42 ©Silberschatz, Korth and Sudarshan
Bank Example Queries
Find all customers who have an account at all branches located in
Brooklyn city.
customer_name, branch_name (depositor account)
branch_name (branch_city = “Brooklyn” (branch))
Database System Concepts - 5th Edition, June 15, 2005 2.43 ©Silberschatz, Korth and Sudarshan