COMP518: Database and Information Systems
The Relational Model
(Chapter 4, Connolly & Begg)
The Relational Model
In a real-life databases, data are “represented” in tables with
Columns (with distinct names)
Rows (with distinct inputs)
Keys define the relationships between tables
The Relational Model gives a formal foundation for tables
Edgar Frank Codd
The relational model was proposed in Codd’s paper
“A Relational Model of Data for Large Shared Data
Banks”, 1970
Codd received the Turing Award in 1981 “For his
fundamental and continuing contributions to the
theory and practice of database management
systems, esp. relational databases”
Lecture Objectives
Terminology of the relational model.
How tables are used to represent data.
Connection between mathematical relations and relations in the
relational model.
Properties of database relations.
How to identify candidate keys, primary keys and foreign keys.
Meaning of entity integrity and referential integrity.
Purpose and advantages of views.
Relational Model Terminology
A relation (or entity or file) is a table with columns and rows.
An attribute (or field) is a named column of a relation (distinct
names in the same relation).
A tuple (or occurrence or record) is a row of a relation.
Examples of Attribute Domains
A domain is the set of allowable values for one or more attributes.
Relational Model Terminology
The degree is the number of attributes in a relation.
The cardinality is the number of tuples in a relation.
Example - Branch and Staff Relations
A Relational Database is a collection of relations with distinct relation names.
This is a logical structure.
This is not the actual way
our data are physically
stored in the hard disk.
Mathematical Definition of Relation
Consider two sets, D1, D2, where D1 = {2, 4} and D2 = {1, 3, 5}.
The Cartesian product, D1 x D2, is the set of all ordered pairs,
where the first element is a member of D1 and the second
element is a member of D2.
D1 x D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
Alternative way is to find all combinations of elements with
first element from D1 and second from D2.
Mathematical Definition of Relation
D1 x D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
Any subset of Cartesian product is a relation; e.g. R = {(2, 1), (4, 1)}
May specify which pairs are in relation using some condition for
selection; e.g.
second element is 1: R = {(x, y) | x 2 D1, y 2 D2, and y = 1}
first element is always twice the second:
S = {(x, y) | x 2 D1, y 2 D2, and x = 2y}
Mathematical Definition of Relation
Consider three sets D1, D2, D3 with Cartesian Product D1 x D2 x D3 ;
e.g.
D1 = {1, 3}, D2 = {2, 4}, D3 = {5, 6}
D1 x D2 x D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), (3,2,5), (3,2,6), (3,4,5), (3,4,6)}
Any subset of these ordered triples is a relation.
Mathematical Definition of Relation
Cartesian product of n sets D1, D2, . . ., Dn is:
D1 x D2 x . . . x Dn = {(d1, d2, . . . , dn) | d1 2 D1, d2 2 D2, . . . , dn2 Dn}
usually written as:
n
Π Di
i=1
Any subset of n-tuples from this Cartesian product is a relation
on the n sets.
Database Relations
Relation schema
The definition/description of a relation
Named relation defined by a set of attribute and domain name pairs.
Set of attributes A1 , A2 , . . . , An
Attributes’ domains D1 , D2 , . . . , Dn
Relation schema S= {A1 : D1 , A2 : D2 , . . . , An : Dn }
Examples of Attribute Domains
D1
D2
D3
D4
Example - Branch and Staff Relations
Database Relations
n-tuple (defined by a relation schema S)
(A1 : d1 , A2 : d2 , . . . , An : dn )
where d1 2 D1 , d2 2 D2 , . . . , dn 2 Dn
or shortly (d1 , d2 , . . . , dn )
Relation
A relation R is a set of n-tuples. R ✓ D1 ⇥ . . . ⇥ Dn
Examples of Attribute Domains
R ✓ D1 ⇥ . . . ⇥ Dn
D1
D2
D3
D4
Example - Branch and Staff Relations
Database
Relational Database Schema
A set of relation schemas, each with a distinct name
S = {S1 , . . . , Sn }
Where Si is a relation schema
Si ={A1 : D1 , A2 : D2 , . . . , An : Dn }
Relational Database
A set of relations, each with a distinct name
Properties of Relations
A relation name is distinct from all
other relation names in a relational
database schema.
Each cell of a relation contains
exactly one atomic (single) value.
Properties of Relations
Each attribute has a distinct name
in the same relation.
Values of an attribute are all from
the same domain.
Order of attributes has no
significance.
Properties of Relations
Each tuple is distinct; there are
no duplicate tuples.
Order of tuples has no
significance, theoretically.
Can you see any practical
reason, where the order may be
important?
Relational Keys
Superkey
An attribute, or set of attributes, that uniquely
identifies each tuple within a relation.
Branch and Staff Relations
Consider the two
relations. What are
possible superkeys?
Relational Keys
Superkey
An attribute, or set of attributes, that uniquely identifies a tuple within
a relation.
Candidate Key
Superkey (K) such that no proper subset is a superkey within the
relation.
Uniqueness - In each tuple of R, values of K uniquely identify that tuple.
Irreducibility - No proper subset of K has the uniqueness property.
Branch and Staff Relations
Consider the two
relations. What are
possible candidate
keys?
Branch Relation
branchNo Street City postcode
B005 22 Deer Rd London SW1 4EH
An instance of a relation cannot be used to
prove that a set of attributes is a candidate key.
Branch Relation
branchNo Street City postcode
B005 22 Deer Rd London SW1 4EH
B003 56 Clover Dr London NW10 6EU
An instance of a relation cannot be used to
prove that a set of attributes is a candidate key.
Branch and Staff Relations
A candidate key
may be composite
Relational Keys
clientNo propertyNo viewDate Comment
CR56 PA14 24-May-04 Too small
CR76 PG4 20-Apr-04 Too remote
CR56 PG4 26-May-04
CR62 PG36 28-Apr-04 No dining room
A candidate key may be composite
{clientNo,propertyNo}
Relational Keys
Primary Key
Candidate key selected to identify tuples uniquely within
relation.
Usually we prefer to select attributes that are not likely to change.
E.g. BranchNo but not {street,postcode}
Alternate Keys
Candidate keys that are not selected to be primary key.
Relational Keys
Foreign Key
Attribute, or set of attributes, within one relation that matches
the primary key of some (possibly the same) relation.
Branch and Staff Relations
Foreign keys express
relationships among
relations
Relational Keys
Foreign Key
Attribute, or set of attributes, within one relation that matches
the primary key of some (possibly the same) relation.
StaffNo ManagerNo
SL41 SL2
SG3 SL2
SL2
SG45 SG3
Integrity Constraints
Domain Constraints
Every attribute has an associated domain
Entity Integrity
Referential Integrity
General Constraints
Example
StaffNo fname Salary telNumber
001 George 10000
002 John 10000 01567889
003 Tom 12000 01567888
007 Dave 10000 01567828
004 Tony 20000 01537888
George doesn’t have a number yet. What
kind of value are we going to put?
Example
StaffNo fname Salary telNumber
001 George 10000
002 John 10000 01567889
003 Tom -1 01567888
007 Dave 10000 01567828
004 Tony 20000 01537888
Tom is a staff member, but he is still in negotiations about
the salary. What kind of value are we going to put?
Is -1 a good solution?
Example
StaffNo fname Salary telNumber
001 George 10000
002 John 10000 01567889
003 Tom -1 01567888
007 Dave 10000 01567828
004 Tony 20000 01537888
No because someone might ask about the
average salary, and -1 will affect the result.
Nulls
Null
Represents value for an attribute that is currently
unknown or not applicable for tuple.
Deals with incomplete or exceptional data.
Represents the absence of a value and is not the
same as zero or spaces, which are values.
Example
StaffNo fname Salary telNumber
001 George 10000
002 John 10000 01567889
003 Tom Null 01567888
007 Dave 10000 01567828
004 Tony 20000 01537888
Null will not affect the average salary!
Entity Integrity
In a base relation, no attribute of a primary key can be null.
branchNo Street City postcode
B005 22 Deer Rd London SW1 4EH
B007 16 Argyll Street Aberdeen AB2 3SU
B004 32 Manse Rd Bristol BS99 1NZ
NULL 162 Main St Glasgow G11 9QX
B003 56 Clover Dr London NW10 6EU
Not allowed! Because the primary key is meant to identify tuples uniquely.
Entity Integrity
In a base relation, no attribute of a primary key can be null.
clientNo propertyNo viewDate Comment
CR56 PA14 24-May-04 Too small
CR76 PG4 20-Apr-04 Too remote
CR56 NULL 26-May-04
CR62 PG36 28-Apr-04 No dining room
Not allowed! Because the primary key is meant to identify tuples uniquely.
Referential Integrity
If a foreign key exists in a relation,
either the foreign key value must match a
primary key value of some tuple in its
home relation
or foreign key value must be wholly null.
We cannot add a new Staff
member with branchNo=B456
But we can add a new Staff
member with branchNo=NULL
Integrity Constraints
General Constraints
Additional rules specified by users or database administrators that
define or constrain some aspect of the enterprise.
For example, each staff member belongs to only one branch,
has a salary no more than £30,000 and manages no more than
100 properties.
Views
Base Relation
Named relation corresponding to an entity in conceptual
schema, whose tuples are physically stored in database.
View
Dynamic result of one or more relational operations
operating on base relations to produce another relation.
Views Example
Base Relation View
branchNo Street City postcode branchNo City
B005 22 Deer Rd London SW1 4EH B005 London
B007 16 Argyll Street Aberdeen AB2 3SU B007 Aberdeen
B004 32 Manse Rd Bristol BS99 1NZ B004 Bristol
B002 162 Main St Glasgow G11 9QX B002 Glasgow
B003 56 Clover Dr London NW10 6EU B003 London
List all branch Numbers along
with the city they are based
Views Example
Base Relation View
branchNo Street City postcode City
B005 22 Deer Rd London SW1 4EH London
B007 16 Argyll Street Aberdeen AB2 3SU Aberdeen
B004 32 Manse Rd Bristol BS99 1NZ Bristol
B002 162 Main St Glasgow G11 9QX Glasgow
B003 56 Clover Dr London NW10 6EU
List all the cities of the branches
Views Example
branchNo Street City postcode
B005 22 Deer Rd London SW1 4EH
Base B007 16 Argyll Street Aberdeen AB2 3SU
B004 32 Manse Rd Bristol BS99 1NZ
Relation
B002 162 Main St Glasgow G11 9QX
B003 56 Clover Dr London NW10 6EU
branchNo Street City postcode
View List all branches
B005 22 Deer Rd London SW1 4EH
B003 56 Clover Dr London NW10 6EU
in London
Views
A virtual relation that does not necessarily actually exist in the
database but is produced upon request, at time of request.
Contents of a view are defined as a query on one or more base
relations.
If a new attribute is added to a relation this will not affect users who
are using a particular view. This is called logical data independency.
Views are dynamic, meaning that changes made to base relations
that affect view attributes are immediately reflected in the view.
Purpose of Views
Provides powerful and flexible security mechanism by hiding parts of
database from certain users.
Permits users to access data in a customized way, so that the same data
can be seen by different users in different ways, at the same time.
View Examples
Members of staff (excluding managers) should not see each others
salary details.
We may wish to have a view containing Branch details as well as the
manager of that Branch. This requires data from both tables but it is
presented in a single view.
Conclusion
Tables, rows, columns and keys give a way of implementing Entity-
Relationship models
The relational model gives a theoretical grounding for database
tables
Next we will be looking at the relational algebra defining
operations on relations.