Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Lecture Note 3 - Databases - Relations

This document describes a Discrete Mathematics II course offered by the Department of Computer Sciences at the University of Lagos. The course covers topics such as sets, logic, proofs, relations, functions, counting, combinatorics, probability, graphs, and NP-completeness. Lectures are on Wednesdays and labs are on Thursdays. The textbook is Discrete Mathematics and Its Applications by Kenneth H. Rosen. The course also uses lecture notes. Presentation outlines cover topics like relations, equivalence relations, n-ary relations, and database representation using the relational model with operations like selection, projection, and join.

Uploaded by

Oriola Kolawole
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lecture Note 3 - Databases - Relations

This document describes a Discrete Mathematics II course offered by the Department of Computer Sciences at the University of Lagos. The course covers topics such as sets, logic, proofs, relations, functions, counting, combinatorics, probability, graphs, and NP-completeness. Lectures are on Wednesdays and labs are on Thursdays. The textbook is Discrete Mathematics and Its Applications by Kenneth H. Rosen. The course also uses lecture notes. Presentation outlines cover topics like relations, equivalence relations, n-ary relations, and database representation using the relational model with operations like selection, projection, and join.

Uploaded by

Oriola Kolawole
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

CSC 413

Discrete Mathematics II
Course Instructors:
Dr. Victor T. Odumuyiwa
Dr. Ufuoma C. Ogude

Department of Computer Sciences


University of Lagos

•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

relations and functions,

counting and combinatorics,

discrete probability,

graphs and trees and

NP-Completeness.
Course Material
Textbook

Discrete Mathematics
and Its Applications
by Kenneth H. Rosen

Use lecture notes as study guide.


Presentation Outlines
Relation and Functions:

 Definition, representation, relation on a set


Properties:

 Reflexivity, symmetry, antisymmetric, irreflexive, asymmetric


Combining relations:

 , , –, 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

n-ary Relations & their Applications


n-ary Relations
In order to study an interesting application of relations,
namely databases, we first need to generalize the concept
of binary relations to n-ary relations.

Relationship among elements of more than 2 sets often arise: n-ary


relations.
E.g. Airline, flight number, starting point, destination, departure time,
arrival time
n-ary relations
Definition
An n-ary relation on sets A1 , A2 ,  , An
is a subset of Cartesian product A1  A2    An

Let A1, A2, …, An be sets. An n-ary relation on these sets

is a subset of A1A2…An.

Where A1, A2, …, An are called the domains of the relation,


and n is called its degree.
Example
A relation on NNN
All triples of numbers ( a, b, c ) with a bc
R  {(1,2,3), (1,2,4), (1,2,5), }

Let R be the relation on N * N * N consisting of triples (a, b, c)


where a, b, and c are integers with a<b<c.

Then (1,2,3)  R, but (2,4,3)  R.

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, cN}

What is the degree of R?

The degree of R is 3, so its elements are triples.

What are its domains?

Its domains are all equal to the set of integers.

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

A database consists of n-tuples called records, which are made up of


fields. These fields are the entries of the n-tuples.

The relational database model represents a database of records as


an n-ary relation, that is, a set of records.
Relational database model
n-ary relation R is represented with table

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

Example: C : Department  Psychology

Result of selection operator sC (R )


Professor Department Course-number
Farber Psychology 501
Farber Psychology 617
Projection operator: Pi1 ,i2 ,,im ( R )
Keeps only the fields i1 , i2 ,  , im of R

Example: PProfessor, Department ( R )


Professor Department
Cruz Zoology
Farber Psychology
Rosen Comp. Science
Rosen Mathematics
Join operator: J k ( R, S )
Concatenates the records of R and S
where the last k fields of R
are the same with the first k fields of S
S: Class schedule
Department Course-number Room Time
Comp. Science 518 N521 2:00pm
Mathematics 575 N502 3:00pm
Mathematics 611 N521 4:00pm
Psychology 501 A100 3:00pm
Psychology 617 A110 11:00am
Zoology 335 A100 9:00am
Zoology 412 A100 8:00am
J2(R,S)
Professor Department Course Room Time
Number

Cruz Zoology 335 A100 9:00am


Cruz Zoology 412 A100 8:00am
Farber Psychology 501 A100 3:00pm
Farber Psychology 617 A110 11:00am
Rosen Comp. Science 518 N521 2:00pm
Rosen Mathematics 575 N502 3:00pm
Databases & Relations
A domain of an n-ary relation is called a primary key if the n-tuples
are uniquely determined by their values from this domain.

This means that no two records have the same value from the same
primary key.

In our example, which of the fields Student Name, ID Number,


Major, and GPA are primary keys?
Student Name and ID Number are primary keys, because no two
students have identical values in these fields.

In a real student database, only ID Number would be a primary key.


Databases & Relations
In a database, a primary key should remain one even if new records
are added.

Therefore, we should use a primary key of the intension of the


database, containing all the n-tuples that can ever be included in our
database.

Combinations of domains can also uniquely identify n-tuples in an n-


ary relation.

When the values of a set of domains determine an n-tuple in a


relation, the Cartesian product of these domains is called a
composite key.
Example:
Relational database model has been developed for information processing

A database consists of records, which are n-tuples made up of fields

 Consider a database of students, whose records are represented as 4-


tuples with the fields Student Name, ID Number, Major, and GPA:

 The fields contains information such as:

• Name

• Student #

• Major

• Grade point average of the student


Example:
The relational database model represents a database of
records or n-ary relation
The relation is R(Student-Name, Id-number, Major, GPA)
Example of records

(Smith, 3214, Mathematics, 3.9)

(Stevens, 1412, Computer Science, 4.0)


(Rao, 6633, Physics, 3.5)
(Adams, 1320, Biology, 3.0)
(Lee, 1030, Computer Science, 3.7)

Relations that represent databases are also called tables,


since they are often displayed as tables.
TABLE A: Students
Students ID # Major GPA
Names
Smith 3214 Mathematics 3.9
Stevens 1412 Computer Science 4.0
Rao 6633 Physics 3.5
Adams 1320 Biology 3.0
Lee 1030 Computer Science 3.7
Operations on n-ary relations
There are varieties of operations that are applied on n-ary relations in
order to form or create new relations that answer eventual queries of a
database.

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.

Databases and Relations


We can use the join operation to combine two tables into one if they share
some identical fields.

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:

(Stevens, 1412, Computer Science, 4.0)


(Lee, 1030, Computer Science, 3.7)
Example
What relation results when the projection P1,4 is applied to the
relation in Table A?

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.

The new relation contains exactly these matches, which are


combined to tuples that contain each matching field only once.

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 #

Cruz Zoology 335


Cruz Zoology 412
TABLE C: Farber Psychology 501
Teaching Farber Psychology 617
Assignments Grammer Physics 544
Grammer Physics 551
Rosen Computer Science 518
Rosen Mathematics 575

Dpt Course # Room Time


Computer Science 518 N521 2:00 PM
Mathematics 575 N502 3:00 PM
TABLE D: Mathematics 611 N521 4:00 PM
Class Physics 544 B505 4:00 PM
Schedule Psychology 501 A100 3:00 PM
Psychology 617 A110 11:00 AM
Zoology 335 A100 9:00 AM
Zoology 412 A100 8:00 AM
Example:
The relation is R(Student-Name, Id-number, Major, GPA)

R = {(Ackermann, 231455, CS, 3.88),


(Adams, 888323, Physics, 3.45),
(Chou, 102147, CS, 3.79),
(Goodfriend, 453876, Math, 3.45),
(Rao, 678543, Math, 3.90),
(Stevens, 786576, Psych, 2.99)}
What is the result when we apply the projection P2,4 to the
student record (Stevens, 786576, Psych, 2.99) ?
It is the pair (786576, 2.99).
Next Lectures Outlines
Introduction

Basic Counting Principles


Sum Principle
Product Principle
Pigeonhole Principle

Combinatorial (ways to combine things)


Combinatorial analysis deals with
permutations
combinations
•binomial coefficients
•binomial Theorem

Distinguish permutations vs combinations

You might also like