Ch03 Relational Model
Ch03 Relational Model
E.g. Customer-schema =
(customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema R
specified by a table
An element t of r is a tuple, represented by a row in a table
attributes
customer
Customer
Depositor
Branch
Borrower
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.
K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for Customer,
since it is a superkey {assuming no two customers can possibly
have the same name), and no subset of it is a superkey.
Strong entity set. The primary key of the entity set becomes
the primary key of the relation.
Weak entity set. The primary key of the relation consists of the
union of the primary key of the strong entity set and the
discriminator of the weak entity set.
Relationship set. The union of the primary keys of the related
entity sets becomes a super key of the relation.
For binary many-to-one relationship sets, the primary key of the
“many” entity set becomes the relation’s primary key.
For one-to-one relationship sets, the relation’s primary key can be
that of either entity set.
For many-to-many relationship sets, the union of the primary keys
becomes the relation’s primary key
Multivalued Attributes: The primary key of the entity or relationship set
together with the multivalued attribute column becomes the primary key.
Loan
branch-name = “Perryridge” (loan)
Notation: p(r)
p is called the selection predicate
Defined as:
A B C A,C (r) A C A C
10 1 1 1
20 1 1 = 1
30 1 1 2
40 2 2
Notation:
Relations r, s:
A B A B
1 2
2 3
1 s
r
r s: A B
1
2
1
3
Notation: r s
Defined as:
r s = {t | t r or t s}
For r s to be valid.
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s: A B
1
1
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 arity
attribute domains of r and s must be compatible
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 19 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
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=C(r x s)
A B C D E
1 10 a
2 20 a
2 20 b
Database System Concepts 3.27 ©Silberschatz, Korth and Sudarshan
Rename Operation
3.2.1.7
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.
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(depositor)
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
Set intersection
Natural join
Division
Assignment
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)
Relation r, s:
A B A B
1 2
2 3
1
r s
rs
A B
2
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 t on s
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))
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
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 ) }
Relations r, s: A B
B
1
1
2
3 2
1 s
1
1
3
4
6
1
2
r s: A r
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
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
A B
A B A B
2
1 1 3
2 2
3 A (R-S(r) X s) - R-S,S(r)
1
1 2
1 B 1
1 2 A
3 1 1
2 2
2
r s R-S(r) R-S(r) X s R-S((R-S(r) X s) - R-S,S(r))
R-S,S(r) A
r s = R-S (r) –R-S ( (R-S (r) x s) – R-S,S(r))
Database System Concepts 3.45 ©Silberschatz, Korth and Sudarshan
Assignment Operation
3.2.3.4
CN(BN=“Downtown”(depositor account))
CN(BN=“Uptown”(depositor account))
Generalized Projection
Outer Join
Aggregate Functions
A B C
7
7
3
10
sum-C
g sum(c) (r)
27
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.
Will study precise meaning of comparisons with nulls later
loan Borrower
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
operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment
operator.
rr–E
where r is a relation and E is a relational algebra query.
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.
form
branch-name
(branch-name = “Perryridge” (all-customer))
views.
Let view v1 be defined by an expression e1 that may itself contain
uses of view relations.
View expansion of an expression repeats the following
replacement step:
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
As long as the view definitions are not recursive, this loop will
terminate
{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
A tuple variable is said to be a free variable unless it is quantified
by a or .
over $1200
{t | t loan t [amount] 1200}
Find the loan number for each loan of an amount greater than
$1200
{t | s loan (t[loan-number] = s[loan-number]
s [amount] 1200}
{t | s borrower(t[customer-name] = s[customer-name])
u depositor(t[customer-name] = u[customer-name])
branch
{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
{t | s loan(s[branch-name] = “Perryridge”
u borrower (u[loan-number] = s[loan-number]
t [customer-name] = u[customer-name])
v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-
city])))}
infinite relations.
For example, {t | t r} results in an infinite relation if the
domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable
expressions to safe expressions.
An expression {t | P(t)} in the tuple relational calculus is safe if
every component of t appears in one of the relations, tuples, or
constants that appear in P
relational calculus
Each query is an expression of the form:
$1200