Relational Model PDF
Relational Model PDF
Relational Model PDF
3
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
attributes
(or columns)
customer_name customer_street customer_city
customer
8
2.8
Database
A database consists of multiple relations
Information about an enterprise is broken up into parts, with each relation
storing one part of the information
account : stores information about accounts
depositor : stores information about which customer
owns which account
customer : stores information about customers
repetition of information
e.g.,if two customers own an account (What gets repeated?)
the need for null values
e.g., to represent a customer without an account
Normalization theory (Chapter 7) deals with how to design relational schemas
By Ms. Shilpa Gupta and Mr. Varun Srivastava
2.9 CSE Department, BVCOE
The customer Relation
10
2.10
The depositor Relation
11
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.
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.
1 7
5 7
12 3
23 10
1 7
23 10
17
2.17
Select Operation
Notation: p(r)
p is called the selection predicate
Defined as:
Example of selection:
branch_name=“Perryridge”(account)
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
1 2
2 3
1 s
r
A B
r s: 1
2
1
3
1 2
2 3
1 s
r
r – s:
A B
1
1
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
25
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.
A B C D E
1 10 a
2 10 a
2 20 b
By Ms. Shilpa Gupta and Mr. Varun Srivastava
2.27 CSE Department, BVCOE
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 ,...,A ) (E )
1 2 n
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
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 (branch_name=“Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan)))
Query 1
Query 2
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
E1 E2
E1 – E2
E1 x E2
Set intersection
Natural join
Division
Assignment
Relation r, s:
A B A B
1 2
2 3
1
r s
rs
A B
2
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
By Ms. Shilpa Gupta and Mr. Varun Srivastava
2.39 CSE Department, BVCOE
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
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
42
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
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.
F ,F ,..., F (E )
1 2 n
G1,G2 ,,Gn
F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n
A B C
7
7
3
10
27
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
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)
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
68
2.68
Figure 2.6: The loan relation
69
2.69
Figure 2.7: The borrower relation
70
2.70
Figure 2.9
Result of branch_name = “Perryridge” (loan)
71
2.71
Figure 2.10:
Loan number and the amount of the loan
72
2.72
Figure 2.11: Names of all customers who
have either an account or an loan
73
2.73
Figure 2.12:
Customers with an account but no loan
74
2.74
Figure 2.13: Result of borrower |X| loan
75
2.75
Figure 2.14
76
2.76
Figure 2.15
77
2.77
Figure 2.16
78
2.78
Figure 2.17
Largest account balance in the bank
79
2.79
Figure 2.18: Customers who live on the
same street and in the same city as
Smith
80
2.80
Figure 2.19: Customers with both an
account and a loan at the bank
81
2.81
Figure 2.20
82
2.82
Figure 2.21
83
2.83
Figure 2.22
84
2.84
Figure 2.23
85
2.85
Figure 2.24: The credit_info relation
86
2.86
Figure 2.25
87
2.87
Figure 2.26: The pt_works relation
88
2.88
Figure 2.27
The pt_works relation after regrouping
89
2.89
Figure 2.28
90
2.90
Figure 2.29
91
2.91
Figure 2.30
The employee and ft_works relations
92
2.92
Figure 2.31
93
2.93
Figure 2.32
94
2.94
Figure 2.33
95
2.95
Figure 2.34
96
2.96