0% found this document useful (0 votes)
271 views

3 Relational Model - Srel

The document describes the relational model used to structure data in a relational database. Some key points: 1) Data is organized into tables with rows and columns, where each column represents an attribute and each row represents a tuple of data. 2) Relations (tables) can be manipulated using relational algebra operations like select, project, join, and divide to query and restructure the data. 3) Integrity constraints like entity integrity and referential integrity enforce data quality rules.

Uploaded by

Karma Dorji
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views

3 Relational Model - Srel

The document describes the relational model used to structure data in a relational database. Some key points: 1) Data is organized into tables with rows and columns, where each column represents an attribute and each row represents a tuple of data. 2) Relations (tables) can be manipulated using relational algebra operations like select, project, join, and divide to query and restructure the data. 3) Integrity constraints like entity integrity and referential integrity enforce data quality rules.

Uploaded by

Karma Dorji
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

3.

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

 In a relational database, data are stored in tables.


 A table stores data about a type of entities or relationships.
{ A column is an attribute.
{ For each attribute, there is a set of permitted values, called the domain of that
attribute.
{ A row represents an instance of entity or relationship. It is an ordered set of
values, one for each attribute.
 A relation is a table.
 Mathematically, a relation is de ned to be a subset of a Cartesian product of a list
of domains.
 Representing data as relations allows us to use methods in set theory to manipulate
data.

3.2 Basic Concepts

 An attribute is a column in a relation/table.


 A null value is a special value meaning \not known", \not applicable",\no infor-
mation", ...
 A relation schema de nes the structure of a relation by specifying its name, its
attributes and their types.

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 de ned.
 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:

Dept (dept-id, name, location)


Prof (prof-id, name, dept-id)
 An integrity constraint is a condition that must be satis ed by every valid instance
of a database/table/column.
Integrity rules

{ 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, identi able 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.

3.3 Relational Algebra

Relational algebra is a theoretical query language for relational databases. It has a


set of operators which operate on relations and produce new relations.
 Set Operations
4
{ Union: r1 [ r2
The union of r1 and r2 contains the tuples that are in either r1 or r2.
{ Intersection: r1 \ r2

The intersection of r1 and r2 contains the tuples that are in both r1 and r2.
{ Set Di erence: r1 r2

The di erence 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

is, have the same set of attributes (the same schema).


{ Cartesian Product: r1  r2

The Cartesian product contains the tuples formed by concatenating a tuple in


r1 with a tuple in r2, for every possible combination of a tuple in r1 and a tuple

in r2. (This may result in a very large relation.)

Example : (Cartesian product)


r1 r2 r1  r2

 Relational operators
{ Selection: C (r)
where C is a selection condition. Let R be the schema of r. C is speci ed 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

Example: Find all vendors in Waterloo.


City = \Waterloo"(v)

Vno Vname City Vbalance


V2 WalMart Waterloo 671.05
V4 Esso Waterloo 225.00

{ 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:

List all cities where there is a vendor.

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))

where A = fall attributes of R1 and R2


except R2:A1; :::; R2:Amg and
C = (r1:A1 = r2:A1; :::; r1:Am = r2:Am)

Example:

Join vendor information with transactions. t 1 v


{ Generalized join (theta-join):

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

{ Outer join: r1 1right R2


Same as a natural join with the exception that all tuples in r2 (the one indicated
by the superscript right) are retained.
If a tuple of r2 does not (natually) join with any tuple in r1, it is extended with
null values for all attributes of r1 and the resulting tuple is output.

Example:

Join vendors and transactions, retaining all vendors.


t 1right v
Tno Vno Account ::: Vname City Vbalance
null V1 null ::: Sears Toronto 200.00
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

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))

3.4 Transforming E-R to Relational

Transforming entity sets

 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.

3.5 Relational Calculus

10
 Note: Syntax may be di erent from those in the reference books.
 Relational calculus is nonprocedural; a relational calculus expression is a formal
de nition 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"
speci es 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 (_; ^; :)
{ quanti ed tuple variables (8t(r), 9t(r))
 Semantics:
Every tuple in the Cartesian product of t1; t2; :::; tn that satis es 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 quanti ed 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 quanti ed.
 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

Reading: Sections 3.1, 3.2, 5.1, 5.2

14

You might also like