3 Relational Model - Srel
3 Relational Model - Srel
RELATIONAL MODEL
This example database for a tiny credit card company will be used frequently in this
section of the course.
v (Vendor)
Vno Vname City Vbalance
V1 Sears Toronto 200.00
V2 WalMart Waterloo 671.05
V3 Esso Windsor 0.00
V4 Esso Waterloo 225.00
c (Customer)
Account Cname Province Cbalance Crlimit
A1 Smith ONT 2515.00 2000
A2 Jones BC 2014.00 2500
A3 Doc ONT 150.00 1000
t (Transaction)
Tno Vno Account T Date Amount
T1 V2 A1 020915 1325.00
T2 V2 A3 010916 1900.00
T3 V3 A1 020915 2500.00
T4 V4 A2 010920 1613.00
T5 V4 A3 000925 3312.00
1
3.1 Basic Structure
2
A tuple is a row in a relation. It is an (ordered) set of values, one for each attribute
in the relation schema over which the tuple is dened.
A relation (instance) is a set of tuples over the schema of the relation. In other
words, it is a table with one column for each attribute in the schema.
A relational database is a set of relations, each one with a unique name.
In the same way as for relations, we distinguish between the schema and an instance
of the database.
{ A relational database schema is a set of relation schemas.
{ A relational database is a set of relations.
In a normalized relation every value in the relation is atomic, that is, every
attribute in every tuple contains a single value from an atomic (or primitive) data
type, not a set of values.
We will use R for relation schemas and r for relation instances.
Keys:
{ A superkey of a relation is a set of one or more attributes which, taken collec-
tively, allow us to identifying an tuple in the relation uniquely. No two tuples
can have the same values for this set of attributes.
{ A candidate key is a minimal set of attributes which has the uniqueness prop-
erty (i.e. which is a superkey).
{ The primary key of a relation is a candidate key designated as the main key
of the relation.
3
{ A foreign key of a relation is an attribute (or combination of attributes) whose
values are restricted to a subset of the values of the primary key of another
relation (the referenced relation). A foreign key is also called a connection key
or reference attribute.
Example:
{ Entity integrity
Primary key values must be unique and no component of a primary key value
may be null. Motivation: a tuple must represent some unique, identiable entity
in the real world.
{ Referential integrity
A tuple with a value for a foreign key that does not match the primary key value
of a tuple in the referenced relation is not allowed. In other words, a foreign key
must reference a tuple that exists.
The intersection of r1 and r2 contains the tuples that are in both r1 and r2.
{ Set Dierence: r1 r2
The dierence between r1 and r2 contains the tuples that are in r1 but not in
r2 .
{ Note: r1 and r2 must be (union-) compatible for the above 3 operations, that
Relational operators
{ Selection: C (r)
where C is a selection condition. Let R be the schema of r. C is specied in
terms of the attributes in R. A selection operation selects all tuples from a
relation that satisfy a given condition, that is, extracts a \horizontal" subset of
the relation.
5
A B C D
1 x a s
C D
A B 1 x b t
a s
1 x 1 x c u
b t
2 y 2 y a s
c u
2 y b t
2 y c u
{ Project: A(r)
where A is a list of attributes of r. That is, A R.
A projection operation constructs a new relation by deleting all attributes
(columns) from a relation which are not mentioned in A, and eliminating du-
plicate tuples. In other words, projection extracts a \vertical" subset of the
relation.
Example:
6
City(v)
City
Toronto
Waterloo
Windsor
{ Join (natural): r1 1 r2
Assume that the attributes common (with the same name) to R1 and R2 are
A1 , ..., Am . Then the natural join constructs a new relation by (pairwise)
concatenation of all tuples from r1 and r2 that satisfy the condition r1:A1 =
r2 :A1, ..., r1 :Am = r2 :Am, and eliminating attributes r2:A1; :::; r2:Am from the
result.
r1 1 r2 = A (C (r1 r2))
Example:
r1 1 r2 = (r1 r2)
where can be any boolean expression formed by using =, 6=, <, , >, , _,
^, :, and the attributes of R1 and R2.
7
Tno Vno Account ::: Vname City Vbalance
T1 V2 A1 ::: WalMart Waterloo 671.05
T2 V2 A3 ::: WalMart Waterloo 671.05
T3 V3 A1 ::: Esso Windsor 0.00
T4 V4 A2 ::: Esso Waterloo 225.00
T5 V4 A3 ::: Esso Waterloo 225.00
Example:
8
The outer join discussed above is a right outer join. We may also have left
outer join and full outer join, which can be expressed using 1lef t and 1f ull .
Division: r1 r2
Assume that R1 and R2 are the schemas of r1 and r2, the attributes of R2 is a subset
of those of R1, and R1 has m additional attributes. The relation of r1 r2 is on the
m attributes.
r1 r2 =
R 1 R2 (r1) R 1 R2 ((R 1 R2 (r1) r2) R
1 R2 ;R2 (r1))
Each entity set is transformed into a relation that includes all the attributes of the
entity set.
The primary key of the entity set becomes the primary key of the corresponding
relation.
Each nonkey attribute of the entity set becomes a nonkey attribute of the relation.
Transforming relationship sets
A binary 1:N relationship set can be represented by adding the primary key of
the entity set on the one-side of the relationship set, as a foreign key in the relation
that corresponds to the entity set on the many-side of the relationship set.
9
A binary M:N relationship set can be represented by creating a separate relation.
The primary key of the new relation is a composite key consists of the primary keys
of the entity sets in the relationship set. Any nonkey attributes associated with the
relationship set are included in the new relation.
In a unary 1:N relationship set , the entity set is transformed into a relation.
The primary key of the relation is the same as for the entity set. Then a foreign key
is added to the relation that references the primary key values.
For a unary M:N relationship set, the entity set is tramsformed into a relation.
Then a separate relation is created to represent the M:N relationship set. The
primary key of the new relation is a composite key of two attributes which take
values from the same primary key domain.
An n-ary relationship set can be represented by creating a separate relation. The
primary key of the new relation is a composite key consists of the primary keys of
the entity sets in the relationship set. Any nonkey attributes associated with the
relationship are included in the new relation.
For an ISA relationship,
{ create a separate relation for the super-set and each of the sub-sets,
{ include the common attributes in the relation for the super-set,
{ include the primary key (of the super-set) and the attributes unique to a sub-set
in the relation for the sub-set.
10
Note: Syntax may be dierent from those in the reference books.
Relational calculus is nonprocedural; a relational calculus expression is a formal
denition of a new relation in terms of other relations.
Two variants of relational calculus: tuple calculus and domain calculus. (Domain
calculus not considered in this course.)
Tuple variable = a variable that ranges over a named relation, that is, whose
values are all tuples from that relation. It can also be considered as a pointer
moving throughout all the tuples in the relation.
Example 1:
Get the names of all vendors in Waterloo.
8 vx(v)
f<vx.Vname> : vx.City='Waterloo'g
Syntax:
8t1(r1), 8t2(r2),..., 8tn(rn)
f < target-list > : predicate g
where t1, t2, ..., tn are tuple variables, r1, r2, ..., rn are relation names, \target-list"
species the attributes of the resulting relation, and \predicate" is a boolean formula
giving a condition that tuples must satisfy to qualify for the resulting relation.
The predicate is constructed from
{ attribute names
11
{ constants
{ comparison operators (=; 6=; >; ; <; )
{ logical connectives (_; ^; :)
{ quantied tuple variables (8t(r), 9t(r))
Semantics:
Every tuple in the Cartesian product of t1; t2; :::; tn that satises the predicate is
part of the result. The nal result is obtained after projection onto the attributes
in target-list and elimination of duplicates.
The query in Syntax is equivalent to
target-list(predicate(r1 r2 ::: rn))
Example 2:
Get the dates, amounts and vendor names of all transactions by customer A2.
8tx(t), 8vx(v)
f< tx.T Date, tx.Amount, vx.Vname > :
tx.Account='A2' ^ tx.Vno=vx.Vnog
Example 3:
Get the account numbers and names of the customers having at least one transaction.
8cx(c)
f< cx.Account, cx.Cname > :
9tx(t)(tx.Account=cx.Account)g
12
Any tuple variable not \declared" in the rst part of the query must be bound in the
predicate. A tuple variable in a predicate is bound if it is quantied in the predicate,
otherwise it is free (in the predicate).
In the example \tx" is bound and \cx" is free in the predicate.
Every tuple variable in the target list must be universally quantied.
Example 4:
Get the account numbers and names of the customers whose transactions are all
with vendor V4.
8cx(c)
f< cx.Account, cx.Cname > :
8tx(t)(tx.Account=cx.Account ) tx.Vno='V4')
^ 9ty(t)(ty.Account=cx.Account)g
Note that the implication is also true for customers not having any transactions.
The second part of the predicate is necessary to disqualify such customers.
Example 5:
Get the account numbers and names of all customers who have transactions with
both vendors V1 and V2.
8cx(c)
f< cx.Account, cx.Cname > :
9tx1(t)(cx.Account=tx1.Account ^ tx1.Vno='V1')
^ 9tx2(t)(cx.Account=tx2.Account ^ tx2.Vno='V2')g
13
Example 6:
Get the account numbers and names of all customers who have no transactions with
Esso.
8cx(c)
f< cx.Account, cx.Cname > :
: (9tx(t),9vx(v)(cx.Account=tx.Account
^ tx.Vno=vx.Vno ^ vx.Vname='Esso'))g
Note that customers with no transactions at all will also qualify.
Example 7:
Get vendor numbers and names of the vendors who have transactions with all cus-
tomers in Ontario.
8vx(v)
f< vx.Vno, vx.Vname > :
8cx(c)(cx.Province='ONT' )
9tx(t)(tx.Account=cx.Account ^ tx.Vno=vx.Vno))g
14