Tải bản đầy đủ (.pdf) (6 trang)

INTRODUCTION TO COMPUTER SCIENCE - PART 3 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (272 KB, 6 trang )

INTRODUCTION TO COMPUTER SCIENCE
HANDOUT #3. THE RELATIONAL DATA MODEL
K5 & K6, Computer Science Department, Văn Lang University
Second semester Feb, 2002
Instructor: Trần Đức Quang
Major themes:
1. Binary Relations
2. Relations
3. Relational Algebra
Reading: Sections 7.7, 7.10, 8.2, and 8.7.
3.1 BINARY RELATIONS
Consider two sets of students:
MS = {Tiến, Hùng, Khoa, Việt}, and
FS = {Lan, Cúc, Huệ}.
Suppose that we have two pairs of lovers: Tiến and Cúc, Việt and Lan. Others may
love someone else not in the sets. We say that the binary relation love on the sets MS
and FS has two tuples: (Tiến, Cúc) and (Việt, Lan). The other tuples, say (Khoa, Huệ)
or (Hùng, Lan), are not in love.
We can now define binary relations formally. First, let A and B be two sets. The
product or Cartesian product of A and B, denoted A × B, is defined as the set of pairs
in which the first component is chosen from A and the second component is chosen
from B. That is,
A × B = { (a, b) | a ∈ A and b ∈ B }
A binary relation R on A and B is a subset of the product A × B.
In the previous example, we have love = { (Tiến, Cúc), (Việt, Lan) } and may write
in a predicate form:
love(Tiến, Cúc) = true
love(Việt, Lan) = true
love(Hùng, Huệ) = false
18 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #3. THE RELATIONAL DATA MODEL
The last must be false since it is not in the relation.


We can use an infix notation for binary relations, so that a binary relation like <
can be written between the componets of pairs in the relation. For example, we may
write 1<2 instead of "(1,2)∈<" or "<(1,2)=true".
Some special properties
Binary relations may have some useful properties: transitivity, reflexivity, symmetry,
and antisymmetry.
Reflexivity
A relation R on a set A is called reflexive if (a, a) ∈ R for every element a ∈ A.
Symmetry
A relation R on a set A is called symmetric if (b, a) ∈ R whenever (a, b) ∈ R
for a, b ∈ A.
Antisymmetry
A relation R on a set A such that (a, b) ∈ R and (b, a) ∈ R only if a = b,
for a, b ∈ A, is called antisymmetric.
Transitivity
A relation R on a set A is called transitive if whenever (a, b) ∈ R and (b, c) ∈ R,
then (a, c) ∈ R, for a, b, c ∈ A.
See more about binary relations in the textbook (partial orders, equivalence relations,
equivalence classes, closures of relations).
3.2 RELATIONS
In this section, we shall extend binary relations into relations of arbitrary arity. In the
relational model, relations are viewed as tables; that is, we represent information in a
table whose columns are given names, called attributes, and whose rows are called
tuples and represent basic facts. Thus, a table has two aspects:
1. The set of column names, and
2. The rows containing the information.
The set of column names (attributes) is called the scheme of the relation. For exam-
ple, the table on the next page has the scheme (Course,StudentId,Grade). If we
give it a name, say CSG, we may write CSG(Course,StudentId,Grade).
3.3 RELATIONAL ALGEBRA 19

The table CSG with three attributes.
A collection of relations (tables) is called a database. In an enterprise, databases
could contain all of the vital information for its operations. Design of a dabase and
applications that access data in the database is a big problem and is beyond the scope
of this course.
A set of schemes of various relations in a database is called the database scheme.
Notice the difference between the scheme for the database, which tells us something
about how information is organized in the database, and the set of tuples in each rela-
tion, which is the actual information stored in the database.
3.3 RELATIONAL ALGEBRA
We introduce some basic operations on relations by examples. You can see more about
those operations and others in many database textbooks.
The Selection Operation
If we want the tuples from the table CSG that have the Course component "CS101", we
can perform a selection on this table and write:
σ
Course=“CS101”
(CSG)
where σ is the selection operator; Course="CS101" is a boolean expression that can
consist of the logical operators such as AND, OR, and NOT. The result of this operation is
as follows:
Course StudentId Grade
CS101
CS101
EE200
EE200
CS101
PH100
12345
67890

12345
22222
33333
67890
A
B
C
B+
A−
C+
Course StudentId Grade
CS101
CS101
CS101
12345
67890
33333
A
B
A−
20 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #3. THE RELATIONAL DATA MODEL
The Projection Operation
Another important operation is the projection. Suppose we want the identifiers of the
students in the table CSG. That is, we must eliminate all the columns but StudentId.
This can be done using the projection operator, represented by the symbol π.
π
StudentId
(CSG)
and we can get an one-column table:
Note that this table has only four tuples, not six as in the original one. The reason is

that relations are sets, and the 1-tuples "12345" and "67890" are duplicates, thus they
are the same elements and need not to be represented twice.
The Join Operation
Unlike the previous operations that are unary ones, the join operation is a binary oper-
ation; that is, it has two operands.
The join of R and S, written R S, is formed by taking each tuple r from R and
each tuple s from S and comparing them. If the component of r for A
i
equals the com-
ponent of s for B
j
, then we form one tuple from r and s; otherwise, no tuple is created
from the pairing of r and s. We form a tuple from r and s by taking the components of
r and following them by all the components of s, but omitting the component for B
j
,
which is the same as the A
i
component of r anyway.
Suppose we have two tables CDH and CR as follows:
The table CDH.
StudentId
12345
67890
22222
33333
Course Day Hour
CS101
CS101
CS101

EE200
EE200
EE200
M
W
F
Tu
W
Th
9AM
9AM
9AM
10AM
1PM
10AM

><

A
i
=B
j
3.4 GLOSSARY 21
The table CR.
and we want to know at what times each room is occupied by some course taking a join
on the Course components. The expression defining the resulting relation is:
CR CDH
and the value of the relation produced by this expression is as shown in the following
table:
We usually perform a kind of joins that equates the attributes with the same names.

Such a join is called a natural join and simply written . The join in the example is a
natural join and can be rewritten CH CDH.
3.4 GLOSSARY
Relation: Quan hệ. See definition in text.
Arity: Ngôi (của quan hệ).
Binary relation: Quan hệ hai ngôi. See definition in text.
n-ary relation: Quan hệ n-ngôi.
Tuple: Bộ (dữ liệu). A sorted list of values, each corresponding to one component in the
relation scheme. Also called a fact.
Attribute: Thuộc tính. A component of a relation that is given a name.
Cartesian Product: Tích, tích Descartes. Also called cross product (tích chéo).
Course Room
CS101
EE200
PH100
Turing Aud.
25 Ohm Hall
Newton Lab.
Course Room Day Hour
CS101
CS101
CS101
EE200
EE200
EE200
Turing Aud.
Turing Aud.
Turing Aud.
25 Ohm Hall
25 Ohm Hall

25 Ohm Hall
M
W
F
Tu
W
Th
9AM
9AM
9AM
10AM
1PM
10AM

><

Course=Course

><


><

22 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #3. THE RELATIONAL DATA MODEL
Predicate: Vò từ. See Chapter 14 in the textbook for Predicate Logic.
Infix notation: Ký pháp trung vò. An expression notation in which operators are betweet
their operands. Related terms: prefix and postfix notation (tiền vò và hậu vò).
Reflexivity: Tính phản thân.
Symmetry: Tính đối xứng.
Antisymmetry: Tính phản xứng.

Transitivity: Tính bắc cầu.
Partial order: Thứ tự bộ phận.
Equivalence relation: Quan hệ tương đương.
Equivalence class: Lớp tương đương.
Closure: Bao đóng.
Scheme: Lược đồ.
Database: Cơ sở dữ liệu.
Relational algebra: Đại số quan hệ.
Selection: Phép chọn.
Projection: Phép chiếu.
Join: Phép nối.
Natural Join: Nối tự nhiên.

×