unit 2 relational model
unit 2 relational model
It is theoretical basis.
Based on concept of relations.
Relation is based on concept of sets.
Proposed by E.F.Codd in 1970 to
model the data in the form of table.
Example of a Relation
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
Degree: It refers to the total number of attributes that are there in the relation.
attribute definitions
name
type/domain:
customer
Database
A database consists of multiple relations
E.g.
repetition of information
Categories of languages
“Pure” languages:
Relational algebra
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.
Unary Operations : select, project and rename
Relational Algebra
Select Operation – Example
Relation r
A B C D
1 7
5 7
12 3
23 10
1 7
23 10
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
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
Set Difference Operation – Example
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s:
A B
1
1
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
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 , …., An .
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
Banking Example
branch (branch_name, branch_city, assets)
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
customer_name(loan.loan_number =
borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))
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) - account.balance
(account.balance < d.balance (account x d (account)))
Account relation
Additional Operations
Additional Operations
Set intersection
Natural join
Aggregation
Outer Join
Division
operations.
Set-Intersection Operation – Example
Relation r, s:
A B A B
1 2
2 3
1
r s
rs
A B
2
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
Division Operation
Notation: rs
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
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
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
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
Generalized Projection
Aggregate Functions
Outer Join
Generalized Projection
Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
G1,G2 ,,Gn
F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n
A B C
7
7
3
10
g sum(c) (r)
sum(c )
27
Aggregate Operation – Example
Relation account grouped by branch-name:
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
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
Computes the join and then adds tuples from one relation that does not
match tuples in the other relation to the result of the join.
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
Join
loan borrower
For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same (as in SQL)
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
Modification of the Database
particular attributes
rr–E
r F ,F ,,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
Update Examples
Make interest payments by increasing all balances by 5 percent.
Find the name of all customers who have a loan at the bank and the
loan amount
Query 2
customer_name, branch_name (depositor account)
temp(branch_name) ({(“Downtown” ), (“Uptown” )})
Note that Query 2 uses a constant relation.
Bank Example Queries
Find all customers who have an account at all branches located in
Brooklyn city.
E 1 E2
E 1 – E2
E1 x E2
Example of selection:
branch_name=“Perryridge”(account)
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
Example: To eliminate the branch_name attribute of account
Assume that attributes of r(R) and s(S) are disjoint. (That is, R S = ).
If attributes of r and s are not disjoint, then renaming must be used.
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)
Tuple Relational Calculus
A nonprocedural query language, where each query is of the
form
{t | P (t ) }
It is the set of all tuples t such that predicate P is true for t
t is a tuple variable, t [A ] denotes the value of tuple t on
attribute A
t r denotes that tuple t is in relation r
P is a formula similar to that of the predicate calculus
https://www.youtube.com/watch?v=KTD6
nU0s3Ak
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , , , , )
3. Set of connectives: and (), or (v)‚ not ()
4. Implication (): x y, if x if true, then y is true
x y x v y
5. Set of quantifiers:
t r (Q (t )) ”there exists” a tuple in t in relation r
such that predicate Q (t ) is true
t r (Q (t )) Q is true “for all” tuples t in relation r
Example Queries
Find the loan number for each loan of an amount greater than $1200
Find the names of all customers who have a loan and an account
at the bank (both condition true)
Find the names of all customers having a loan at the Perryridge branch
Find the names of all customers having a loan from the Perryridge
branch, and the cities in which they live
Find the names of all customers who have an account at all branches
:
located in Brooklyn