Database Management System
Chapter 2:
Relational Database System
Adisak Intana
Lecturer
Table Contents
Database Model
Relational Database System
Relationship
Relation characteristics
Kinds of key
Introduction to Database System
2
Table Contents
Database Model
Relational Database System
Relationship
Relation characteristics
Kinds of key
Introduction to Database System
3
Database Model

Database Model ใช้สำหรับอธิบำยโครงกำรสร้ำง
และควำมสัมพันธ์ระหว่ำงฐำนข้อมูล เพื่อช่วยให้สำมำรถสร้ำง
ฐำนข้อมูลได้ง่ำยขึ้น

Database Model แบ่งเป็ น 3 ประเภท ดังนี้



Hierarchical Database Model
Network Database Model
Relational Database Model
Introduction to Database System
4
Hierarchical Database Model



โครงสร้ าง มีลกั ษณะเป็ นแบบ ต้ นไม้ (Tree)
ส่ วนทีอ่ ยู่ระดับบนสุ ดเรียกว่ า root ซึ่งเป็ น parent
ส่ วนทีอ่ ยู่ระดับทีต่ ่ากว่ า เรียกว่ า child
Introduction to Database System
5
Hierarchical Database Model
Introduction to Database System
6
Hierarchical Database Model
Introduction to Database System
7
Hierarchical Database Model
Root: Painter
Level 1: Painter Name
Level 2: Picture Name
Introduction to Database System
8
Hierarchical Database Model

ข้ อดี



เห็นภาพชัดเจน เข้ าใจง่ าย
โครงสร้ างเห็นได้ ชัดเจน ทาให้ ข้อมูลเป็ นอิสระจากตัว
โปรแกรมทีด่ ึงข้ อมูลขึน้ มาใช้ งาน
สามารถจัดการกับข้ อมูลทีม่ ีความสั มพันธ์ แบบ 1:M ได้
อย่ างมีประสิ ทธิภาพ
Introduction to Database System
9
Hierarchical Database Model

ข้ อเสี ย
 ความซับซ้ อนของข้ อมูล จะต้ องมีความเข้ าใจ ในลักษณะ
โครงสร้ างทางกายภาพ (Physical data storage
characteristics) ของการจัดเก็บข้ อมูล
 ยากต่ อการจัดการโครงสร้ าง
 ไม่ สามารถจัดการกับข้ อมูลทีม
่ ีความสั มพันธ์ แบบ M:M
Introduction to Database System
10
Network Database Model


ความสั มพันธ์ ของข้ อมูลจะอยุ่ในรูปแบบ Set
ในแต่ ละ set จะประกอบไปด้ วย 2 ส่ วน คือ



Owner (Parent)
Member (child)
ในแต่ ละ set จะแทนด้ วยความสั มพันธ์ 1:M ระหว่ าง
owner กับ member
Introduction to Database System
11
Network Database Model
Introduction to Database System
**INVOICE (ใบสั่ งซื้อสิ นค้ า) **INVLINE (รายละเอียดใบสั่ งซื้อสิ นค้ า)
12
Network Database Model
Introduction to Database System
13
Network Database Model
Introduction to Database System
14
Network Database Model

ข้ อดี


สามารถจัดการกับข้ อมูลทีม่ ีความสั มพันธ์ แบบ M:N
โครงสร้ างเห็นได้ ชัดเจน ทาให้ ข้อมูลเป็ นอิสระจากตัว
โปรแกรมทีด่ ึงข้ อมูลขึน้ มาใช้ งาน
Introduction to Database System
15
Network Database Model

ข้ อเสี ย
 ความซับซ้ อนของข้ อมูล จะต้ องมีความเข้ าใจ ในลักษณะ
โครงสร้ างทางกายภาพ (Physical data storage
characteristics) ของการจัดเก็บข้ อมูล มากกว่ าแบบ
Hierarchy Model
 ยากต่ อการจัดการโครงสร้ าง
Introduction to Database System
16
Table Contents
Database Model
Relational Database Management System
Relationship
Relation characteristics
Kinds of key
Introduction to Database System
17
Relational DBMS (RDBMS)

A type of DBMS that stores data in the form of related two
dimensional tables.

The structure of the database is based on the “ Relational
data model “ which was introduced by Dr. E.F. Codd in
1970.
Introduction to Database System
18
Relational DBMS (RDBMS)


Logical database is called relational
database.
Data in the logical database is represented as
a collection of relations ( the same
terminology used in mathematics ) or tables.
Introduction to Database System
19
A Sample of relational database
Student
Stud-id
Enroll
Name
Sex Age
Stud_id
Subj_id Grade
4702777 Amporn
F
18
4702777 Math102
B+
4703666 Narin
M
19
4702777 Phy101
C
4702888 Sasin
M
19
4703666 Math102
A
4702888 Phy101
B
4702888 Math101
C
Subject
Subj-id
Sjame
Credit
Phy101
Physics
3
Math102
Math
4
Introduction to Database System
20
Relational database

Domain of an attribute X is the set of all possible values
of X.
Student
Stud-id
Name
Sex
Age
4702777 Amporn
F
18
Sex = {F, M}
4703666 Narin
M
19
Age= {x  I|0 < x < 60}
4702888 Sasin
M
19
Introduction to Database System
Domain of
21
A relation in a relational database
Attributes
Relation name
Emp EmpNo
Ename
Job
Sal
Comm
DeptNo
E1
USA
SALESMAN
5000
0
D1
E2
PRANEE
SALESMAN
5000
1000
D1
E3
SUTAT
MANAGER
2000
2000
D2
E4
WATTANA
ANALYST
8000
D1
E5
BOON
CLERK
4000
D2
tuple
Introduction to Database System
atomic value
null value
22
Relational DBMS (RDBMS)


Logical database is called relational
database.
Data in the logical database is represented as
a collection of relations ( the same
terminology used in mathematics ) or tables.
Introduction to Database System
23
Relationship

ในระบบฐานข้ อมูล ข้ อมูลแต่ ละส่ วนจะมีความสั มพันธ์
เกีย่ วข้ องกัน

ทฤษฎีระบบฐานข้ อมูลได้ แบ่ งความสั มพันธ์ ของข้ อมูลออกเป็ น
3 รูปแบบคือ



one-to-one relationship
one-to-many relationship
many-to-many relationship
Introduction to Database System
24
One-to-One Relationship

ความสั มพันธ์ แบบ 1:1 เป็ นความสั มพันธ์ ทขี่ ้ อมูลชุ ด A แต่ ละ
รายการ มีความสั มพันธ์ กบั ข้ อมูลชุด B เพียงรายการเดียว

ตัวอย่ างเช่ น อาจารย์ กบั ห้ องพักอาจารย์ เป็ นความสั มพันธ์ แบบ
1:1 เพราะ
 อาจารย์ แต่ ละคนมีห้องพัก 1 ห้ อง
 ห้ องพักแต่ ละห้ องเป็ นของอาจารย์ เพียงคนเดียว
Introduction to Database System
25
One-to-One Relationship
1
Teacher
Introduction to Database System
1
Room
26
One-to-Many Relationship

ความสั มพันธ์ แบบ 1:M เป็ นความสั มพันธ์ ทขี่ ้ อมูลชุ ด A แต่ ละ
รายการ มีความสั มพันธ์ กบั ข้ อมูลชุด B มากกว่ า 1 รายการ

ตัวอย่ างเช่ น จิตรกรกับภาพวาด เป็ นความสั มพันธ์ แบบ 1:M
เพราะ
 จิตกรแต่ ละคนสามารถวาดภาพได้ มากกว่ า 1 ภาพ
 ภาพแต่ ละภาพถูกจิตรกรเพียง 1 คนเท่ านั้น
Introduction to Database System
27
One-to-Many Relationship
1
Painter
Introduction to Database System
M
Painting
28
Many-to-Many Relationship

ความสั มพันธ์ แบบ M:M เป็ นความสั มพันธ์ ทขี่ ้ อมูลชุ ด A แต่ ละรายการ
มีความสั มพันธ์ กบั ข้ อมูลชุ ด B มากกว่ า 1 รายการ

และในขณะเดียวกัน ข้ อมูลชุ ด B แต่ ละรายการ มีความสั มพันธ์ กบั
ข้ อมูลชุ ด A มากกว่ า 1 รายการ

ตัวอย่างเช่ น นักศึกษากับวิชาทีล่ งทะเบียนเรียน เป็ นความสั มพันธ์ แบบ
M:M เพราะ
 นักศึกษาแต่ ละคนสามารถลงทะเบียนเรียนได้ หลายรายวิชา
 รายวิชาแต่ ละรายวิชาถูกนักศึกษาลงทะเบียนเรียนได้ หลายคน
Introduction to Database System
29
One-to-Many Relationship
M
Student
Introduction to Database System
N
Subject
30
Table Contents
Database Model
Relational Database Management System
Relationship
Relation characteristics
Kinds of key
Introduction to Database System
31
คุณสมบัตขิ อง Relation

ข้อมูลในแต่ละ tuple ต้องไม่ซ้ ำกัน
Introduction to Database System
32
คุณสมบัตขิ อง Relation




กำรเรี ยงลำดับของ tuple ไม่มีควำมสำคัญ
กำรเรี ยงลำดับของ attribute ไม่มีควำมสำคัญ
ค่ำข้อมูลของแต่ละ attribute ต้องอยูใ่ น Domain
เดียว
ค่ำของข้อมูลในแต่ละ attribute มีเพียงค่ำเดียว
(Single Value)
Introduction to Database System
33
Table Contents
Database Model
Relational Database Management System
Relationship
Relation characteristics
Kinds of key
Introduction to Database System
34
Candidate key and primary key

An attribute (or set of attributes) of a relation whose
values can be used to identified each tuple in the
relation is called a candidate key.

If a relation has only one candidate key, it will be the
primary key of that relation.
Introduction to Database System
35
Primary key and secondary key

If there is more than one candidate key in a relation,
one candidate key must be selected to be the primary
key of that relation and the rest will become the
secondary key(s) or alternate key(s).
Introduction to Database System
36
Primary key and secondary key
Dept
DeptNo
Dname
D1
MARKETING
D2
PERSONNEL
D3
COMPUTER

Both DeptNo and Dname are candidate keys

If DeptNo is chosen to be the primary key, thus Dname
must be the secondary key
Introduction to Database System
37
Primary key and secondary key
Dept

Dname
DeptNo
D1
MARKETING
D2
PERSONNEL
D3
COMPUTER
Relation “Dept” structure (intension part) is represented as :
Dept(DeptNo, Dname)
Primary key
Introduction to Database System
Secondary key
38
Primary key

Each relation, there is always an attribute (or a set of
attributes) has been set to be the primary key.
–
–
–
student(std-id, name, sex, age)
subject(sj-id, sjname, credit)
enroll(std-id, sj-id, grade)
Introduction to Database System
39
Primary key


Each tuple of a relation, the attribute value of the primary
key (primary key value) can be used to identify that tuple.
Primary key value of each tuple of a relation must satisfy
the following two time-independent properties
– Not null
– Unique
Introduction to Database System
40
Compound primary key

A key which consists of more than one attribute is called
a compound key.
Enroll
Stud_id
Compound key
Subj_id Grade
4702777 Math102
B+
4702777 Phy101
C
4703666 Math102
A
4702888 Phy101
B
4702888 Math101
C
Introduction to Database System
41
Compound primary key

In each tuple, the compound value of attributes of the
compound keys must be unique and can be used to be
the representation of that tuple.
Enroll
Stud_id
Subj_id
Grade
4702777 Math102
B+
4702777 Phy101
C
4703666 Math102
A
4702888 Phy101
B
4702888 Math101
C
Introduction to Database System
42
Foreign keys

An attribute (or set of attributes) X of a relation R2 is
a foreign key if it is the primary key of some other
relation R1 in the same database.
– For all time, each current value of X in the relation R2
must be a current value of X in the relation R1.
– R2 is called referencing relation
– R1 is called referenced relation
Introduction to Database System
43
Foreign key
Emp
EmpNo
Dept
Ename
Job
Sal
Comm
DeptNo
E1
USA
SALESMAN
5000
0
D1
E2
PRANEE
SALESMAN
5000
1000
D1
E3
SUTAT
MANAGER
2000
2000
D2
E4
WATTANA
ANALYST
8000
D1
E5
BOON
CLERK
4000
D2
DeptNo
[Dname]
D1
MARKETING
D2
ACCOUNTING
D3
COMPUTER
Introduction to Database System
DeptNo is a foreign key of
relation Emp
44
Foreign key
Emp
EmpNo
E1
Ename
USA
Job
Sal
Comm
DeptNo
SALESMAN
5000
0
D1
Dept
DeptNo
D1
[Dname]
MARKETING
Each DeptNo value in relation Emp must refer to an existed DeptNo
value in relation Emp
Introduction to Database System
45
กฎเกีย่ วกับ Key
 The Entity integrity rule
– PK จะต้อง unique และ not NULL
 The Referential integrity rule
– foreign key จะต้องอ้ำงถึง primary key ของอีก
relation ได้
Introduction to Database System
46
กฎเกีย่ วกับ Key

ข้ อกาหนดเกีย่ วกับ foreign key ดังนี้
– FK สำมำรถเป็ น NULL ได้หรื อไม่
– ข้อบังคับในกำรแก้ไข PK ซึ่ งเป็ น FK ของ relation อื่น
 Restrict - ถ้ำจะลบ DeptId 01 จะต้องไม่มี StdId อ้ำงถึง
 Cascade - ถ้ำจะลบ DeptId 02 จะต้องลบ StdId 4001 ด้วย
หรื อถ้ำจะแก้ไข DeptId 02 เป็ น 200 จะ
 ต้องแก้ไข StdId 4001 ให้ DeptId เป็ น 200 ด้วย
Nullify - ถ้ำจะแก้ไขหรื อลบ DeptId 02 จะต้องแก้ไข StdId
4001 ให้ DeptId เป็ น NULL ก่อน
Introduction to Database System
47