Slide 1
What is a Relation ?
Objectives of the Lecture :
•To consider how Set Theory defines the structure
of relations;
•To consider how relations can be exploited to provide
the equivalent of bags of duplicates &
ordered sequences.
Slide 2
Set Theory
The relational model is based on mathematical sets.
Definition : “A set is a collection of things”.
A set has two main characteristics :
It has no sequence or structure.
It has no duplicates.
Example :
{ 2, 7, 5, 4 }
{ 7, 5, 4, 2 }
They are the same set because sets have no order.
Example :
{ 2, 2, 7, 5, 4, 8, 8 }
is not a set because it has duplicates.
Note. A „set‟ whose values are ordered is a Sequence.
A „set‟ with duplicates is a Bag (or Multiset).
A set is the simplest construct that exists in mathematics. It is merely a collection of
things, called the members of the set. It has no other properties. So sequences and
bags are not sets.
Traditionally the contents of a set are written enclosed in curly brackets.
In principle in mathematics, a set can be a collection of any kinds of things. For
example, a set could consist of
{ a car, a bus ticket, a building, a person, a flight, a colour }
However in computing, sets are invariably constrained to be typed sets, meaning that
the members of a set all have to be of the same kind or type. Thus the above example
of a type is not a typed set (unless you can find some way of generalising them all so
that they are indeed members of some strange type !). Typed sets would be a set of
cars, a set of bus tickets, etc.
Databases in general and relational databases in particular all use typed sets. Look
out for how this applies as you go through the course.
Slide 3
Relation = Set of Tuples
Definition : a relation is a set of tuples.
Hence the two relations below are the same, even though their
tuples appear in a different order.
ENo
E3
1
E5
2
E6
4
EName M-S Sal
Smith
5
S
2 12,500
2
Mitchell
6
M
4 21,000
4
Blake
8
M
8 54,000
8
ENo
E1
3
E3
1
E6
4
EName M-S Sal
Robson
7
D
6 32,500
6
Smith
5
S
2 12,500
2
Blake
8
M
8 54,000
8
E8 Jones
W 68,000
E8 Jones
W 68,000
E1
3 Robson
7
D
6 32,500
6
E5
2 Mitchell
6
M
4 21,000
4
Neither version of the relation has any particular
rhyme or reason for the ordering of its tuples.
Because a relation is a set of tuples, its tuples are not fixed in a particular order,
because an ordering is a kind of structure and sets have no structure. Or put another
way, the same relation can appear with its tuples in any order.
In practice, a relation might have a default sequence in which it appears, this being
typically determined by the physically storage of the relation.
The number of tuples in a relation is called the cardinality of the relation. So the
example relation above has a cardinality of 5.
Slide 4
Relation - No Duplicate Tuples
This cannot be a relation as some tuples appear more than once.
2 Smiths
3 Blakes
ENo
E3
1
E3
1
E5
2
E1
3
EName M-S Sal
Smith
5
S
2 12,500
2
Smith
5
S
2 12,500
2
Mitchell
6
M
4 21,000
4
Robson
7
D
6 32,500
6
E6
4 Blake
8
M
8 54,000
8
E6
4 Blake
8
M
8 54,000
8
E6
4 Blake
8
M
8 54,000
8
E8 Jones
W 68,000
Because a relation is a set of tuples, it cannot have duplicate tuples, because
duplicates don‟t exist in a set.
Sometimes it is important to deal with matters of
duplication and ordering, because these things
occur in reality.
We will now consider how these two aspects can be handled using relations. We will
consider duplication first and then ordering.
Slide 5
Storing a Bag in a Relation
Example : a library holds information about books, but it often
has many copies of the same book.
Title
Computing
5
Relational6 DBMS
Nuclear Physics
7
Genetics 8
Solar Energy
Data about the books.
Author
No.
2 mith & Jones 212
S
4 J Date
C
421
6 Dewhirst
D
65
M Platt
8
84
Will & Smythe
6
The number of
copies of the book.
The above example still uses a relation – each tuple in it is unique.
It uses the strategy of recording one instance of each item of data about some entity
(in this case, a book), and how many times that entity occurs. This saves storage
space and is generally less confusing. (It is what manual recording systems generally
do : e.g. a shopping list).
Use this method when the entities whose data appear in a tuple are identical, but there
can be many instances of them.
Slide 6
Handling a Sequence with a Relation
Although the DBMS presents a relation to the user as a set of
tuples, we can retrieve a relation in any tuple sequence which
depends on the values in the tuples.
ENo
E1
3
E3
1
E5
2
Sort sequence :
ascending order
of „Eno‟
EName M-S Sal
Robson
7
D
6 32,500
6
Smith
5
S
2 12,500
2
Mitchell
6
M
4 21,000
4
E6
4 Blake
8
M
8 54,000
8
E8 Jones
W 68,000
Although the relation has no particular sequence, when the DBMS retrieves a relation
full of data for the user, its tuples can be sorted into any order we like. Therefore the
user or application program can see a sequence of data.
All DBMSs provide this feature because it is so useful in practice.
There are many cases where it is very important, if not essential, for the tuples to
appear in the right sequence.
Example : a telephone directory. The directory is a set of entities each
consisting of a name, address and telephone number, but it would be completely
unusable if it were not sorted into alphabetic order of names !
The advantage of the relational approach is that it can provide any sequence of tuples;
different users and/or applications might want the same relation of data in different
sequences, and the relational DBMS can satisfy them all.
Slide 7
A ‘Sequenced’ Relation (1)
Same relation,
in 2 different sequences.
Sort sequence :
ascending order
of „Sal‟
Sort sequence :
descending order
of „Sal‟
ENo
E3
1
E5
2
E1
3
E6
4
EName M-S Sal
Smith
5
S
2 12,500
2
Mitchell
6
M
4 21,000
4
Robson
7
D
6 32,500
6
Blake
8
M
8 54,000
8
E8 Jones
W 68,000
ENo EName M-S Sal
E8 Jones
W 68,000
E6
4
E1
3
E5
2
E3
1
Blake
8
Robson
7
Mitchell
6
Smith
5
M
8
D
6
M
4
S
2
54,000
8
32,500
6
21,000
4
12,500
2
Slide 8
A ‘Sequenced’ Relation (2)
ENo EName M-S Sal
E1
3 Robson
7
D
6 32,500
6
E6
4 Blake
8
M
8 54,000
8
Sort sequence :
ascending order
of „EName‟
within
ascending „M-S‟
E5
2 Mitchell
6
M
4 21,000
4
E3
1 Smith
5
S
2 12,500
2
E8 Jones
W 68,000
Slide 9
Sorting a Relation into Sequences
Summary.
A relation is sorted on the values of one or more
of its attributes.
Thus any sort attribute must contain orderable data.
Examples. Numbers have a natural sequence.
Text can be sorted alphabetically.
Photos : there is no agreed way of sorting them.
Any orderable attribute‟s data can be sorted into ascending
or descending order.
If more than one attribute is used for sorting, a major to
minor order must be specified.
Where several attributes are used for sorting, then the approach is :
Sort tuples using the 1st attribute‟s values.
Where more than one tuple has the same value for the 1 st attribute, then
sort those tuples on the 2nd attribute‟s values.
Where more than one tuple has the same value for the 1 st and 2nd
attributes, then sort those tuples on the 3rd attribute‟s values,
etc.
The order of 1st, 2nd, .. nth attributes is called the major to minor order, the 1 st attribute
being the major attribute, and the last attribute being the minor attribute.
Alternatively, tuples are said to be sorted “on the 3rd attribute within the 2nd attribute
within the 1st attribute”.
Slide 10
SQL : Retrieving a Sequenced Relation
To retrieve the relation
EMPLOYEE in order of
increasing employee salary :
Keyword Select indicates :
• Do a retrieval.
Select *
• The attributes to get :
From EMPLOYEE
* means all attributes.
Order By Sal Asc ;
Keyword From indicates
the relation to use.
Keyword Order By
indicates the sequencing.
Asc indicates
ascending.
The above example retrieves the whole of the relation EMPLOYEE from the current
DB.
Asc is the default sort order, so it could be omitted if desired. If Asc had been
replaced by Desc (for descending), then the sequence would have been in order of
decreasing salaries.
The above retrieval is an example of an SQL statement, which must always be
terminated with a semi-colon; this is because some SQL statements can be very long,
so the semi-colon (rather than the end of the line) is used to indicate the end of the
statement.
Every SQL retrieval statement must include the Select and From phrases in it. All
other phrases - like Order By - are optional.
The following SQL example illustrates a sequencing that uses a major and a minor
attribute :
Select *
From EMPLOYEE
Order By M-S Asc, EName Asc ;
Slide 11
Limitations of Sequencing
This relation
contains a
plumber‟s job
list, in order
of priority :-
Job
EName
Owner
Repair Toilet 21, Byker Grove,
NE4 6TY
Mr Bloggs
Fix Dripping 100, Osborne Rd.,
Tap
Jesmond
Ms J Wareing
Install Central 10, Conaught Drive Prof Caldwell
Whitley Bay
Heating
Install new
bath
16A, Racing View,
Blaydon
Mrs Johnson
Its tuple sequence is based on the urgency of the job, followed by
the profit margin the plumber can make on the job. As this data is
not stored in the relation it cannot be used for sorting on tuple
values; so the DBMS cannot retrieve the tuples in this order.
Thus the limitation on sequencing is that it is value-based, i.e. it must be based on
values held in the relation.
Slide 12
Overcoming Sequencing Limitations
The solution is to add another attribute to the relation, on whose
values the tuples can be sorted to create the required sequence.
Example : add a priority attribute to the plumber„s relation and
sort on it :Priority
Job
EName
Repair Toilet 21, Byker Grove,
1
Owner
Mr Bloggs
NE4 6TY
Ms J Wareing
2
Fix Dripping 100, Osborne Rd.,
Tap
Jesmond
3
Install Central 10, Conaught Drive Prof Caldwell
Heating
Whitley Bay
4
Install new
bath
16A, Racing View,
Blaydon
Mrs Johnson
The only way to overcome this limitation is to add one or more attributes whose
value(s) do reflect the sequence required, and then sort on these.
Slide 13
Tuple = Set of Attributes
Definition : a tuple is a set of attributes.
Because a relation is a set of tuples, the relation has that same set of
attributes. When depicted as a table, the attributes for each tuple are
always shown in the same sequence, for simplicity & convenience.
These two relations are the same, even though their attributes
appear in a different sequence.
ENo
E3
1
E5
2
E1
3
EName M-S Sal
Smith
5
S
2 12,500
2
Mitchell
6
M
4 21,000
4
Robson
7
D
6 32,500
6
EName Sal M-S ENo
Smith
5
12,500
2
S
2 E3
1
Mitchell
6
21,000
4
M
4 E5
2
Robson
7
32,500
6
D
6 E1
3
E6
4 Blake
8
M
8 54,000
8
Blake
8
54,000
8
M
8
E6
4
E8 Jones
W 68,000
Jones
68,000 W
E8
Each tuple in the relation has the same set of named attributes. Of course the values
in those attributes can differ in different tuples.
The number of attributes in a tuple, and hence in a relation, is called the degree (or
arity) of the tuple or relation. So the example relations above both have a degree (or
arity) of 4. “Degree” is generally the preferred term in Britain, “arity” in America.
A tuple/relation of degree one is said to be unary, of degree two binary, of degree
three ternary, and so on; in general a tuple/relation of degree n is said to be n-ary.
The corresponding terms of 1-tuple, 2-tuple, 3-tuple, …, n-tuple can also be used for
tuples.
In a retrieval, such as
Select *
From EMPLOYEE ;
the order in which the attributes appear in the result is a default order, typically
determined by the relation‟s physical storage and/or by the order in which the
attributes were referenced when the relation was created.
To obtain a specific sequence of attributes in the retrieval, simply list their names in
the Select phrase in the desired order.
Thus the attribute sequences above would be produced by the following retrievals
respectively :
Select ENo, EName, M-S, Sal
From EMPLOYEE ;
Select EName, Sal, M-S, ENo
From EMPLOYEE ;
Slide 14
Relation - No Duplicate Attributes
This cannot be a relation because attribute „M-S‟ appears twice.
ENo
E3
1
E5
2
E1
3
EName M-S M-S Sal
Smith
5
S
2 S
2 12,500
2
Mitchell
6
M
4 M
4 21,000
4
Robson
7
D
6 D
6 32,500
6
E6
4 Blake
8
M
8
M
8 54,000
8
E8 Jones
W
W 68,000
Duplicate attributes are those with duplicate names, whether their
contents are duplicated or not.
If 2 attributes have the same name, we can‟t tell the computer which of these 2
attributes we want, even if they do contain different data.
The lack of a fixed attribute sequence is useful, because we can retrieve data from the
DB in any attribute sequence we like; and if we don„t care what sequence the
attributes appear in, we need not bother to specify any sequence and just get the
default.
Slide 15
A Relation - its Definition so far
A relation is a set of tuples,
each of which is a set of attributes.
Each tuple in the relation contains the same set of attributes by
name, although the same attribute in different tuples may, and
often does, hold different values.
Each named attribute holds data of one specific type (sometimes
expressed as being from a particular domain). Attributes may or
may not have data types in common with other attributes in a
tuple.
Consequently it is convenient to consider the relation as having
those named attributes, each with its particular data type/domain.
A relation‟s Heading (or Intension) is its type, which is a
composite type based on that of its attributes.
A relation‟s Body (or Extension) is its value, which is a
composite being the value of all of its attributes in all of its
tuples.
In principle, any data type can be used in an attribute, but in practice you will be
limited by the range of types available with your DBMS (although it is often possible
to buy packages of data types that are additional to the standard ones that come with
the DBMS).
A DB relation is in fact a variable. It is a named object in the DB whose value can
change over time.
Future lectures will consider relation/attribute data types in greater detail.
© Copyright 2026 Paperzz