Lecture Note 3 - Databases - Relations
Lecture Note 3 - Databases - Relations
Discrete Mathematics II
Course Instructors:
Dr. Victor T. Odumuyiwa
Dr. Ufuoma C. Ogude
•Lectures:
– E 304, Wednesday 10:00 am to 11:00 am
– Lab 203, Thursday 10:00 am to 12:00 am
Course Outline
We study topics in such areas as:
Sets and logic
proof techniques
discrete probability,
NP-Completeness.
Course Material
Textbook
Discrete Mathematics
and Its Applications
by Kenneth H. Rosen
, , –, composite of relations
Representing relations:
0-1 matrices, directed graphs
Closure of relations:
Reflexive closure, diagonal relation, Warshall’s Algorithm Students‘ Reading
Equivalence relations:
Equivalence class, partitions
is a subset of A1A2…An.
The degree of this relation is 3. Its domains are equal to the set
of integers.
Example
Let R = {(a, b, c) | a = 2b b = 2c with a, b, cN}
Is (2, 4, 8) in R? No.
Is (4, 2, 1) in R? Yes.
Databases & Relations
Let us take a look at a type of database representation that is based
on relations, namely the relational data model. Relational database
model has been developed for information processing
fields
R: Teaching assignments
Professor Department Course-number
records Cruz Zoology 335
Cruz Zoology 412
Farber Psychology 501
Farber Psychology 617
Rosen Comp. Science 518
Rosen Mathematics 575
primary key
(all entries are different)
Selection operator: sC (R )
keeps all records that satisfy condition C
This means that no two records have the same value from the same
primary key.
• Name
• Student #
• Major
Definition1
Let R be an n-ary relation and C a condition that elements in R may satisfy.
Then the selection operator sC maps n-ary relation R to the n-ary relation
of all n-tuples from R that satisfy the condition C.
Definition2
The projection Pi1, i2, …, im maps the n-tuple (a1, a2, …, an) to the m-tuple
(ai1, ai2, …, aim), where m n.
In other words, a projection Pi1, i2, …, im keeps the m components ai1, ai2,
…, aim of an n-tuple and deletes its (n – m) other components.
Databases and Relations
In some cases, applying a projection to an entire table may not only result
in fewer columns, but also in fewer rows.
Why is that?
Some records may only have differed in those fields that were deleted, so
they become identical, and there is no need to list identical records more
than once.
Definition3
Let R be a relation of degree m and S a relation of degree n.
The join Jp(R, S), where p m and p n, is a relation of degree m + n – p that
consists of all (m + n – p)-tuples (a1, a2, …, am-p, c1, c2, …, cp, b1, b2, …, bn-p),
where the m-tuple (a1, a2, …, am-p, c1, c2, …, cp) belongs to R and the n-tuple (c1, c2,
…, cp, b1, b2, …, bn-p) belongs to S.
Example
if sC = “Major = “computer science” GPA > 3.5” then the
result of this selection consists of the 2 four-tuples:
Solution
When the projection P1,4 is used, the second and third columns
of the table are deleted, and pairs representing student
names and GPA are obtained. Table B displays the results of
this projection. TABLE B: GPAs
Students GPA
Names
Smith 3.9
Stevens 4.0
Rao 3.5
Adams 3.0
Lee 3.7
Databases and Relations
In other words, to generate Jp(R, S), we have to find all the
elements in R whose p last components match the p first
components of an element in S.
Example:
What is J1(Y, R), where Y contains the fields Student Name and
Year of Birth,
Y = {(1978, Ackermann),
(1972, Adams),
(1917, Chou),
(1984, Goodfriend),
(1982, Rao),
(1970, Stevens)},
and R contains the student records as defined before ?
Solution:
The resulting relation is:
{(1978, Ackermann, 231455, CS, 3.88),
(1972, Adams, 888323, Physics, 3.45),
(1917, Chou, 102147, CS, 3.79),
(1984, Goodfriend, 453876, Math, 3.45),
(1982, Rao, 678543, Math, 3.90),
(1970, Stevens, 786576, Psych, 2.99)}
Since Y has two fields and R has four, the relation J1(Y, R) has 2 + 4 – 1
= 5 fields.
Exercise
Definition 4
Let R be a relation of degree m and S a relation of degree n. The
join Jp(R,S), where p m and p n, is a relation of degree m + n – p
that consists of all (m + n – p)-tuples (a1, a2, …, am-p, c1, c2, …, cp, b1, b2,
…, bn-p), where the m-tuple (a1, a2, …, am-p, c1, c2, …, cp) belongs to R
and the n-tuple (c1, c2, …, cp, b1, b2, …, bn-p) belongs to S.
Example
What relation results when the operator J2 is used to combine the
relation displayed in tables C and D?
Professor Dpt Course #