Vous êtes sur la page 1sur 135

1

MO HNH QUAN HE
HE THONG THONG TIN THEO LOI CU
1. D lieu trung lap
2. D lieu khong nhat quan
3. Kho khan trong trao oi d lieu
4. Kho khan trong khai thac d lieu
5. NDC khong khai thac trc tiep d lieu
2
HE TAP TIN THEO LOI CU

1. Co s lien ket chat che gia cau truc luan ly va cau
truc vat ly cua cac tap tin va chng trnh ng dung khai
thac chung
2. Co s d tha d lieu rat ln qua viec trung lap cac tap
tin trong cac ng dung khac nhau.
3. Ngi s dung co t kha nang khai thac trc tiep d lieu

3
C S D LIEU LA G?
Mot cho cha co to chc tap hp cac tap tin d lieu co
tng quan, cac mau tin va cac cot.
Ngay nay CSDL a ton tai:
o He at cho may bay
o He nguon nhan lc.
o He kho va kiem ke.
o he dch vu cong cong nh cap nc, ien...
o ieu khien qua trnh che tao va san xuat

4
DataBase Management System (DBMS)
Software 1
Software 2
Software 3
P
h
y
s
i
c
a
l
d
a
t
a
b
a
s
e
USER
DBMS
The DBMS provides an interface between the user and the data
Database
administrator
Application
developer
A database can be accessed by:
a) programmers.
b) data entry personnel.
c) the DBA.
d) all of the above.
5
CAC LOAI HQTCSDL (DBMS)
1. loai phan cap (Hierarchical) nh he IMS cua IBM
2. loai mang (network) nh IDMS cua Cullinet Software
3. Loai tap tin ao (inverted file) nh ADABAS cua Software AG
4. Loai quan he (relational) nh nh ORACLE cua Oracle, DB2 cua
IBM, ACCESS cua Microsoft Access, SQL Server cua Microsoft
Access.
5. Loai oi tng (Object oriented)
6
NGI S DUNG (User)
1. Ngi quan tr CSDL (Databse administrator):
s chnh xac, toan ven cua d lieu va ng dung
lu phong h va phuc hoi CSDL
gi lien lac vi NPTVLTD va NDC.
s hoat ong troi chay va hieu qua cua CSDL va HQTCSDL
2. NPTVLTD (application delveloper, programer) thiet ke, tao dng va
bao tr he thong tin cho ngi dung cuoi.
3. Ngi dung cuoi (end user) khai thac CSDL thong qua he c
phat trien bi NPTVLTD hay cac cong cu truy van cua
HQTCSDL.
7
SO SANH HAI KIEU TIEP CAN
chng trnh
ng dung
Hequan trc sdlieu
CSDL Logic
T
a
p
t
i
n

v
a
t
l
y
,
m
a
u
t
i
n

v
a

c
o
t
NSD
Hnh 1.2.2 - Tiep can theo csdl tao ra mot lp phan mem gia dlieu vat lyva
chng trnh ng dung
chng trnh
ng dung
NSD
T
a
p
t
i
n

v
a
t
l
y
,
m
a
u
t
i
n

v
a

c
o
t
Tiep can theo loi cu Tiep can theo csdl
8
Mo hnh quan he (Relational Model)
Mo hnh C s d lieu Quan he (goi tat la Mo hnh Quan he)
do E.F Codd e xuat nam 1971. Mo hnh nay bao gom:
cac khai niem lien quan en cau truc d lieu di dang dong
va cot nh quan he, bo, thuoc tnh, khoa chnh, khoa ngoai, ...
phep toan tap hp va phep toan quan he.
rang buoc toan ven quan he.
Cac HQTCSDLQH c xay dng da theo ly thuyet mo hnh quan he
9
MO HNH QUAN HE
MA
SV
HOSV TEN
SV
MONHOC TEN
KHOA
DIEM
THI
99001 TRAN DAN THU CO SO DU LIEU CNTT 3.0
99002 NGUYEN HA DA THAO CO SO DU LIEU CNTT 8.0
99001 TRAN DAN THU TIN HOC VAN P. CNTT 6.0
99005 LE THANH TRUNG TIN HOC VAN P. ANH VAN 5.0
Bai toan quan ly iem thi c lu tr khong ung vi mo
hnh quan he se gay ra d tha d lieu. D tha d lieu dan
en cap nhat d thng
10
MO HNH QUAN HE
MASV MAMH MAKHOA DIEMTHI
99001 CSDL CNTT 3.0
99002 CSDL CNTT 8.0
99001 THVP CNTT 6.0
99005 THVP AV 5.0
MAMH TENMH SOTIET
CSDL CO SO DU LIEU 90
THVP TIN HOC VAN PHONG 90
MAKHOA TENKHOA
CNTT CONG NGHE THONG TIN
AV ANH VAN
MASV HOSV TENSV
99001 TRAN DAN THU
99002 NGUYEN HA DA THAO
99005 LE THANH TRUNG
11
THUOC TNH (Atribute, arity)
Thong tin chi tiet s vat, s viec hay oi tng
Thuoc tnh cua s vat c phan biet qua ten va khong
c co hai thuoc tnh cung ten
Thuoc tnh phai thuoc mot kieu d lieu nhat nh nh
so, chuoi, ngay ...
Thuoc tnh c ky hieu bang ch cai hoa au danh
sach nh A, B, C hay A
1
,A
2,
...

12
LC O QUAN HE (Relation schema)
La tap tat ca cac thuoc tnh cua mot oi tng, s vat
hay s viec
Lc o quan he Q vi tap thuoc tnh A
1
,A
2,
...An c viet
la Q(A
1
,A
2,
... An)
Moi lc o quan he luon kem mot tan t e dien ta
y ngha cua no. Chang han LQH
Kh(MAKHOA,TENKHOA,SOCBGD) co tan t: Moi
khoa co ma khoa e phan biet khoa nay vi khoa kia, ten
khoa va so giang vien cua khoa
13
QUAN HE (Relation, instances of relation)
S the hien cua lc o quan he Q mot thi iem nao o
c goi la quan he.
Lc o quan he Q co quan he r.
V du quan he khoa

rKh
MAKHOA TENKHOA SOCBGD
CNTT CONG NGHE THONG TIN 60
AV ANH VAN 60
HOA HOA CHAT
20
MT MOI TRUONG 10
14
BO (Tuple)
Bo la tap moi gia tr lien quan cua tat ca cac thuoc
tnh cua mot lc o quan he.
V du quan he sau co 2 bo
MA
SV HOTEN NU
NGAY
SINH
MA
LOP TINH
HOC
BONG
99001 TRAN AN TRUE 15-03-1977 CTH2B T GIANG 120000
99002 NGUYEN HA TRUE 25-04-1986 TCTH29C TPHCM 120000
15
Khoa (Key, candidate key)
S la tap cac thuoc tnh
S c goi la mot sieu khoa (superkey) cua lc o quan he R neu vi hai bo tuy y trong
quan he R th gia tr cua cac thuoc tnh trong S la khac nhau .
Sieu khoa co t thuoc tnh nhat c goi la khoa (key)
khoa c chon e cai at goi la khoa chnh (primary key)
khoa ngoai (foreign key) la thuoc tnh cua LQH nay nhng lai la khoa chnh cua LQH
khac
Khoa hp (composite key) la khoa co tren mot thuoc tnh

A primary key in a table
a) can be the same value as other primary key values in the same table
b) must be different from foreign keys in another table
c) must not contain NULL.
d) must be a number e.must be different from a primary key in another table
16
V DU
Cho lc o quan he sau:
Xe(SODANGBO,QUICACH,HINHDANG,MAUSAC,SOSUON,SOMAY,MAXE,QUOCGIA)
Sieu khoa: (SOSUON,QUICACH),...
Khoa ch nh: (SODANGBO,QUOCGIA), (SOSUON), (SOMAY), (MAXE)
Khoa chnh: MAXE
Thuoc tnh khoa: SODANGBO,QUOCGIA,SOSUON,SOMAY,MAXE
Thuoc tnh khong khoa: QUICACH, HINHDANG, MAUSAC
17
Relational Algebra Operation (phep toan ai so quan he)
SET OPERATION (Phep toan tap hp)
---------------------------------------------------------------------
Neu Q
1
, Q
2
co:
Q
1
+
= Q
2
+
= {A
1
, A
2
,..., A
n
}
r
1
la quan he tren Q
1
r
2
la quan he tren Q
2
Th Q
3
co quan he r
3
c xac nh nh sau:
Q
3
+
= {A
1
, A
2
,..., A
n
}
r
3
= r
1
+ r
2
={t/ ter
1
hay te r
2
}
NH NGHA TOAN T HP (UNION,UNION JOIN)
r
1
r
2
r
3
= r
1
+ r
2

MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
99001 CSDL 5.0 99002 CTDL 2.0 99001 CSDL 5.0
99002 CTDL 2.0 99001 TTNT 5.0 99002 CTDL 2.0
99003 MANG 8.0 99003 CSDL 6.0 99003 MANG 8.0
99001 TTNT 5.0
99003 CSDL 6.0
18
Neu Q
1
, Q
2
co:
Q
1
+
= Q
2
+
=

{A
1
, A
2
,..., An}
r
1
la quan he tren Q
1
r
2
la quan he tren Q
2

Th Q
3
co quan he r
3
c xac nh nh sau:
Q
3
+
= {A
1
, A
2
,..., A
n
}
r
3
= r
1
* r
2
= {t/ ter
1
va te r
2
}
NH NGHA PHEP GIAO (INTERSECTION)
r
1
r
2
r
3
= r
1
* r
2

MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
99001 CSDL 5.0 99002 CTDL 2.0 99002 CTDL 2.0
99002 CTDL 2.0 99001 TTNT 5.0
99003 MANG 8.0 99003 CSDL 6.0
19
Neu Q
1
, Q
2
co:
Q
1
+
= Q
2
+
= {A
1
, A
2
,..., A
n
}
r
1
la quan he tren Q
1
r
2
la quan he tren Q
2
Th Q
3
co quan he r
3
c xac nh nh sau:
Q
3
+
= {A
1
, A
2
,..., A
n
}
r
3
= r
1
- r
2
={t/ ter
1
va te r
2
}
NH NGHA PHEP TR (MINUS,DIFFERENCE)
r
1
r
2
r
3
=

r
1
-

r
2

MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
99001 CSDL 5.0 99002 CTDL 2.0 99001 CSDL 5.0
99002 CTDL 2.0 99001 TTNT 5.0 99003 MANG 8.0
99003 MANG 8.0 99003 CSDL 6.0
20
Neu Q
1
, Q
2
co:
Q
1
+
= {A
1
, A
2
,..., A
n
}
Q
2
+
=

{B
1
, B
2
,..., B
m
}
r
1
la quan he tren Q
1
r
2
la quan he tren Q
2

Th Q
3
co quan he r
3
c xac nh nh sau:
Q
3
+
= Q
1
+

U Q
2
+

={A
1
,...,B
1
,...}
r
3
= r
1
x r
2
= {(t
1
, t
2
)/ t
1
er
1
va t
2
er
2
}
TCH DESCARTES (Cartesian Product, Product)
r
1
r
3
=

r
1
x r
2

MA
SV
MA
MH
DIEM
THI
MA
SV
MA
MH
DIEM
THI
MA
MH TENMH
99001 CSDL 5.0 99001 CSDL 5.0 CSDL CO SO DU LIEU
99002 CTDL 2.0 99001 CSDL 5.0 FOX FOXPRO
99003 MANG 8.0 99002 CTDL 2.0 CSDL CO SO DU LIEU
r
2
99002 CTDL 2.0 FOX FOXPRO
MAMH TENMH 99003 MANG 8.0 CSDL CO SO DU LIEU
CSDL CO SODULIEU 99003 MANG 8.0 FOX FOXPRO
FOX FOXPRO
21
Neu Q co:
Q
+
= {A
1
, A
2
,..., A
n
}
r la quan he tren Q

X c Q
+
Th Q co quan he rc xac nh nh sau:
Q
+
= X
r=r.X=r[X]={t/-ter va t=t.X}
PHEP CHIEU (PROJECTION)
r
r = r.{MAMH}
MASV MAMH DIEMTHI MAMH
99001 CSDL 5.0 CSDL
99002 CTDL 2.0 CTDL
99003 MANG 8.0
MANG
22
Neu Q co: Q
+
= {A
1
, A
2
,..., A
n
}
r la quan he tren Q, Xc Q
+
E la mot menh e logic c phat bieu
tren X.
ter thoa ieu kien E ky hieu la t(E)
Th Qco quan he rc xac nh nh sau:
Q
+
= Q
+

r= r(E)={t/ te r va t(E)}
PHEP CHON (SELECTION)
r r= r(DIEMTHI >= 5)
MASV MAMH DIEMTHI MASV MAMH DIEMTHI
99001 CSDL 5.0 99001 CSDL 5.0
99002 CTDL 2.0 99003 MANG 8.0
99003 MANG 8.0
23
Neu Q
1
, Q
2
co
Q
1
+
= {A
1
, A
2
,..., A
n
}
Q
2
+
=

{B
1
, B
2
,..., B
m
}
r
1
,r
2
lan lt la quan he tren Q
1
,Q
2
MGT(A
i
) = MGT(B
j
).
u la phep so sanh tren MGT(A
i
)
Th Q
3
co quan he r
3
c xac nh nh sau:

Q
3
+
=

Q
1
+

U Q
2
+

={A
1
,...,B
1
,...}
r
3
= r
1
|><| r
2
={t
12
/ -t
1
e r
1
, -t
2
e r
2
sao cho
t
12
.Q
1
+
= t
1
, t
12
.Q
2
+
= t
2
, t
1
.A
i
u t
2
.B
j
}
PHEP KET (JOIN,EQUI-JOIN)
r
1
r
2
r
3
= r
1
|><| r
2
Vi A
i
=B
j
=MAMH
MA
SV
MA
MH
DIEM
THI
MA
MH
TEN
MH
MA
SV
MA
MH
DIEM
THI TENMH
99001 CSDL 5.0 CSDL CO SO DU LIEU 99001 CSDL 5.0 CO SO DU LIEU
99002 CTDL 2.0 CTDL CAU TRUC DLIEU 99002 CTDL 2.0 CAU TRUC DLIEU
99003 MANG 8.0
24
Neu Q
1
, Q
2
co
Q
1
+
= {A
1
, A
2
,..., A
n
}
Q
2
+
=

{B
1
, B
2
,..., B
m
}
r
1
,r
2
lan lt la quan he tren Q
1
,Q
2

Th Q
3
co quan he r
3
c xac nh nh sau:

Q
3
+
=

{A
1
,...,A
n-m
}
r
3
= r
1
r
2
={t
3
|t
2
er
2
, -t
1
er
1
t
3
=t
1
.{A
1
,...,A
n-m
}
t
2
=t
1
.{A
n-m+1
,...,A
n
} }
PHEP CHIA (DIVISION)
r
1
r
2
r
3
= r
1
r
2

A
1
A
2
A
3
A
4
A
5
B
1
B
2
A
1
A
2
A
3

a b d c g c g a b d
a b d e f e f e g c
b c e e f
e g c c g
e g c e f
a b e g e
25
MO HNH THC THE KET HP
ac tavan e
Phan tch ac taexac nh dlieu yeu
cau vamoi lien quan gia chung exay
dng mohnh thc theket hp
Mohnh thc theket hp
Ap dung qui tac bien oi mohnh thc
theket hp thanh lc oc sdlieu
Lc oc sdlieu
Lc oCSDL xay dng theo hng Phan tch thiet ke
26
MO HNH THC THE KET HP
Nhng ngi phu trach ao tao cua Trng cao ang cong ong nui
Ayers mong muon tao lap mot CSDL ve cac mon ao tao cua trng
(nh: chng ch leo nui, cong nghe bay) va hoc vien ghi danh vao nhng
mon hoc nay. Trng cung co qui nh la cung mot luc, hoc vien ch
co the ghi danh vao mot mon hoc. Ho ch quan tam ve d lieu cua
t ghi danh hien tai. Mot khi hoc vien ket thuc mon hoc th nha
trng se khong con quan tam en ho va nhng hoc vien nay phai
c xoa khoi CSDL. Thong tin can lu tr ve mot hoc vien bao gom:
ma hoc vien, ten hoc vien, a ch, ngay sinh, so ien thoai, ngay nhap
hoc
Thong tin ve mon hoc gom ma mon hoc, ten mon hoc, thi lng
27
Phan tch:
Phan ac ta van e cha ng cac qui tac quan ly va d lieu yeu cau cua
van e.
d lieu cua van e la: chi tiet ve hoc vien co ma hoc vien, ten hoc vien, a ch, ngay
sinh, so ien thoai va ngay nhap hoc chi tiet ve mon hoc co ma mon hoc, ten mon hoc
va thi lng.
qui tac quan ly gom:
Cung mot luc, mot hoc vien ch co the ghi danh vao mot mon hoc.
Nhieu hoc vien co the ghi danh vao mot mon hoc.
Nha trng ch quan tam en nhng hoc vien cua mon hoc hien tai
HOC
VIEN
MON
HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
ngay nhap hoc
mamon hoc
ten mon hoc
thi lng
Mohnh ER cua van ehoc vien/mon hoc Trng cao ang cong ong nui Ayers
ghi danh vao
c ghi danh bi
cothe
phai
mot
mot hay nhieu
28
Moi NGI LAI XE phai s hu mot BANG LAI
Moi BANG LAI phai c s hu bi NGI LAI XE
ac ta van e
Phong canh sat mong muon quan ly ly lch ca nhan nhng ngi lai xe
va bang lai cua ho. Mot ngi ch lay c mot bang lai va mot bang
lai ch thuoc ve mot ngi. Thong tin ve lai xe ma phong canh sat quan
tam la: ma ngi lai xe, ten, a ch, ngay sinh
Thong tin ve bang lai can lu tr la: ma bang lai, loai bang lai, ngay
het han
Mo hnh thc the ket hp

NGI
LAI XE
BANG
LAI
mangi lai xe
ten
a ch
ngay sinh
mabang lai
loai bang lai
ngay het han
Mohnh ER cua van engi lai xe vabang lai
shu
c shu bi
29
V DU VE MOI QUAN HE NHIEU NHIEU
ac ta van e
Ngi phu trach ao tao Trng cao ang cong ong nui xanh mong muon
thiet lap mot csdl ve cac mon hoc ma ho cung cap (nh chng ch leo nui,
c nhan cong nghe bay) va cac hoc vien ghi danh vao cac mon hoc nay.
Nha trng qui nh la mot hoc vien c ghi danh hoc toi a ba mon hoc
trong cung mot luc. Ho ch quan tam en d lieu cua mon hoc hien tai.
Mot khi hoc vien ket thuc mon hoc, ho se khong con thuoc dien quan ly
cua nha trng va phai c xoa khoi csdl tr khi hoc vien nay ghi danh
hoc tiep mon mi. Thong tin ve mot hoc vien gom: ma hoc vien, ten hoc
vien, a ch, ngay sinh, so ien thoai, ngay nhap hoc
Thong tin ve mon hoc gom: ma mon hoc, ten mon hoc, thi lng
30
MOI QUAN HE NHIEU NHIEU CHUYEN THANH 2 MOI QUAN HE MOT NHIEU
HOC
VIEN
MON
HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
mamon hoc
ten mon hoc
thi lng
Mohnh ER cua van eTrng cao ang cong ong nui xanh
ghi danh vao
c ghi danh bi
?ngay nhap hoc
HOC VIEN MON HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
mamon hoc
ten mon hoc
thi lng
ghi nhan ao tao ve
thuoc ve
PHIEU
GHI DANH
mahoc vien
mamon hoc
ngay nhap hoc
co
c ghi nhan ao tao bi
31
BIEN OI MO HNH ER THANH MO HNH QUAN HE
Khi bien oi mo hnh ER thanh cac lc o c s d lieu ta ap dung
cac qui tac sau:
Moi tap thc the trong mo hnh ER c chuyen thanh mot lc
o quan he.
Moi thuoc tnh trong mo hnh ER c chuyen thanh thuoc tnh
trong lc o quan he tng ng
Moi thuoc tnh nhan dien trong mo hnh ER c chuyen thanh
khoa chnh trong lc o quan he tng ng.
Moi moi quan he trong ER c chuyen thanh khoa ngoai theo qui
tac sau:
32
CHUYEN MOI QUAN HE MOT MOT THANH KHOA NGOAI
Mohnh ER valc oCSDL cua van eNgi lai xe vabang lai
Mohnh ER
Lc oCSDL
NGI LAI XE (mangi lai xe, ten, a ch, ngay sinh)
BANG LAI(mabang lai , loai bang lai, ngay het han, mangi lai xe)
NGI
LAI XE
BANG
LAI
mangi lai xe
ten
a ch
ngay sinh
mabang lai
loai bang lai
ngay het han
shu
c shu bi
NGI LAI XE (mangi lai xe, ten, a ch, ngay sinh, mabang lai)
BANG LAI(mabang lai , loai bang lai, ngay het han)
hay
Chuyen khoa chnh t quan he 1 sang quan he 2 hay ngc lai
33
CHUYEN MOI QUAN HE MOT NHIEU THANH KHOA NGOAI
Chuyen khoa chnh t ben 1 sang ben nhieu
HOC
VIEN
MON
HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
ngay nhap hoc
mamon hoc
ten mon hoc
thi lng
Mohnh ER vamolc oCSDL cua van eTrng cao ang cong ong nui Ayers
ghi danh vao
c ghi danh bi
Mohnh ER
Lc oCSDL
HOC VIEN (mahoc vien, ten hoc vien, a ch, ngay sinh, soien thoai, ngay nhap hoc, mamon hoc)
MON HOC (mamon hoc, ten mon hoc, thi lng)
khoa ngoai
34
CHUYEN MOI QUAN HE CO TAP KET HP THANH KHOA NGOAI
HOC VIEN MON HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
mamon hoc
ten mon hoc
thi lng
Mohnh ER vamohnh quan hecua van eTrng Cao ang Cong ong Nui Xanh
ghi nhan ao tao ve
thuoc ve
PHIEU GHI DANH
mahoc vien
mamon hoc
ngay nhap hoc
co
c ghi nhan ao tao bi
Mohnh ER
Lc oCSDL
HOC VIEN (mahoc vien, ten hoc vien, a ch, ngay sinh, soien thoai)
MON HOC (mamon hoc, ten mon hoc, thi lng)
PHIEU GHI DANH(mahoc vien, mamon hoc, ngay nhap hoc)
35
CHUYEN MOI QUAN HE NHIEU NHIEU THANH KHOA NGOAI
HOC
VIEN
MON HOC
mahoc vien
ten hoc vien
a ch
ngay sinh
soien thoai
mamon hoc
ten mon hoc
thi lng
Mohnh ER vamohnh quan hecua van e
Trng cao ang cong ong nui xanh khong cothuoc tnh ngay nhap hoc
ghi danh vao
c ghi danh bi
Mohnh ER
Mohnh quan he
HOC VIEN(mahoc vien, ten hoc vien, a ch, ngay sinh, soien thoai)
MON HOC(mamon hoc, ten mon hoc, thi lng)
HOC VIEN MON HOC(mahoc vien, mamon hoc)
36
BAI TAP AI SO QUAN HE
Cho lc o c s d lieu dung e quan ly ho s sinh vien bao gom cac lc o quan he Sv(sinh
vien), Lop(Lp), kh(khoa), Mh(mon hoc), Kq(ket qua) c mo ta bi cac lc o quan he nh sau:
Sv(MASV, HOTEN,NU, NGAYSINH, MALOP, TINH, HOCBONG)
Tan t:Moi sinh vien co moi MASV duy nhat. Moi MASV xac nh tat ca cac thuoc tnh con lai
cua sinh vien o.
Lop(MALOP,TENLOP,SISO,MAKHOA)
Tan t:Moi lp co mot ma lp duy nhat, moi lp ch thuoc ve mot khoa nao o.
Kh(MAKHOA,TENKHOA, SOCBGD)
Tan t:Moi khoa co moi MAKHOA duy nhat. Moi MAKHOA xac nh tat ca cac thuoc tnh con lai
cua khoa o.
Mh(MAMH, TENMH, SOTIET)
Tan t:Moi Mon hoc co mot MAMH duy nhat. Moi MAMH xac nh tat ca cac thuoc tnh con lai
cua mon hoc o.
Kq(MASV, MAMH, DIEMTHI)
Tan t:Moi sinh vien cung vi mot mon hoc xac dnh duy nhat mot iem thi
37
BAI TAP AI SO QUAN HE
Xac nh khoa quan he:
Sv(MASV, HOTEN,NU, NGAYSINH, MALOP, TINH, HOCBONG)
Lop(MALOP,TENLOP,SISO,MAKHOA)
Kh(MAKHOA,TENKHOA, SOCBGD)
Mh(MAMH, TENMH, SOTIET)
Kq(MASV, MAMH, DIEMTHI)
Xac nh cac quan he
a) at rSv la quan he cua Sv:
rSv.{MASV,HOTEN,HOCBONG}
b) at rLop la quan he cua Lop
((rSv rLop): NU va MAKHOA=CNTT).{MASV,HOTEN,HOCBONG}
c) at rKq la quan he cua kQ, rMh la quan he cua Mh
((((rKq I><I rMh) I><I rSv) I><I rLop):MAKHOA=CNTT).{MASV,HOTEN,TENMH,DIEMTHI}
d) at rBangDiem la quan he cua cau c)
rPhieuDiem = rBangDiem:MASV=99001
MALOP
I >< I
38
QUAN LY LAO ONG
e quan ly viec phan cong cac nhan vien tham gia vao xay dng cac cong trnh. Cong ty
xay dng ABC to chc quan ly nh sau:
Cung luc cong ty co the tham gia xay dng nhieu cong trnh, moi cong trnh co mot ma
so cong trnh duy nhat (MACT), moi ma so cong trnh xac nh cac thong tin nh: Ten goi
cong trnh (TENCT), a iem(IAIEM), ngay cong trnh c cap giay phep xay dng
(NGAYCAPGP), ngay khi cong (NGAYKC), ngay hoan thanh (NGAYHT)
Moi nhan vien cua cong ty ABC co mot ma so nhan vien(MANV) duy nhat, mot ma so
nhan vien xac nh cac thong tin nh: Ho ten (HOTEN), ngay sinh(NGAYSINH), phai (PHAI),
a ch (IACHI),phong ban,
Cong ty phan cong cac nhan vien tham gia vao cac cong trnh, moi cong trnh co the c
phan cho nhieu nhan vien va moi nhan vien cung luc cung co the tham gia vao nhieu
cong trnh. Vi moi cong trnh mot nhan vien co mot so lng ngay cong (SLNGAYCONG)
a tham gia vao cong trnh o.
Cong ty co nhieu phong ban(Phong ke toan, phong kinh doanh, phong ky thuat, phong to
chc, phong chuyen mon, Phong phuc vu,). Moi phong ban co mot ma so phong
ban(MAPB) duy nhat, mot phong ban ng vi mot ten phong ban(TENPB)
39
CONG
TRNH
NHAN
VIEN
maCT
tenCT
diaDiem
ngayCapGP
ngayKhoiCong
ngayHT
maNV
hoTen
ngaySinh
phai
diaChi
Mohnh ER valc oCSDL
c tham gia bi
c phan cong vao
Mohnh ER
Lc oCSDL
NHANVIEN(maNV,hoTen,ngaySinh,phai,diaChi,maPB)
PHONGBAN(maPB,tenPB)
CONG(maNV,maCT,sLNgayCong)
PHONG
BAN
maPB
tenPB
thuoc ve
co
CONG
NHAN
VIEN
maCT
tenCT
diaDiem
ngayCapGP
ngayKhoiCong
ngayHT
maNV
hoTen
ngaySinh
phai
diaChi
thuoc ve
co
PHONG
BAN
maPB
tenPB
thuoc ve
co
CONG
TRNH
co
thuoc ve
maNV
maCT
sLNgayCong
CONGTRINH(maCT,tenCT,diaDiem,ngayCapGP,ngayKhoiCong,ngayHT)
40
QUAN LY TH VIEN
Mot th vien to chc viec cho mn sach nh sau:
Moi quyen sach c anh mot ma sach (MASH) dung e phan biet vi cac
quyen sach khac (gia s neu mot tac pham co nhieu ban giong nhau hoac co
nhieu tap th cung xem la co ma sach khac nhau), moi ma sach xac nh cac
thong tin khac nh : ten sach (TENSACH), ten tac gia (TACGIA), nha xuat ban
(NHAXB), nam xuat ban (NAMXB).
Moi oc gia c th vien cap cho mot the th vien, trong o co ghi ro ma oc
gia (MAG), cung vi cac thong tin khac nh : ho ten (HOTEN), ngay sinh
(NGAYSINH), a ch (IACHI), nghe nghiep(NGHENGHIEP).
C moi lt mn sach, oc gia phai ang ky cac quyen sach can mn vao
mot phieu mn, moi phieu mn co mot so phieu mn (SOPM) khac nhau, moi
phieu mn xac nh cac thong tin nh: ngay mn sach (NGAYMUON), ma oc
gia. Cac cac quyen sach trong cung mot phieu mn khong nhat thiet phai tra
trong mot lan.
41
AU SACH
OC GIA
maDauSach
ban
tap
maDG
hoTen
ngaySinh
diaChi
ngheNghiep
Mohnh ER valc oCSDL
lachuthe
thuoc ve
Mohnh ER
Lc oCSDL
DOCGIA(maDG,hoTen,ngaySinh,diaChi,ngheNghiep)
PHIEUMUON(soPM,ngayMuon,maDG)
CHITIETMUON(soPM,maDauSach,ngayTra,maDauSach)
PHIEU
MN
soPM
ngayMuon
thuoc ve
cha
DAUSACH(maDauSach,ban,tap,maSH)
CHI TIET
MN
ke
c kebi
soPM
maDauSach
ngayTra
TA SACH
maSH
tenSach
tacGia
nhaXB
namXB
co
lata cua
TUASACH(maSH,tenSach,tacGia,nhaXB,namXB)
42
QUAN LY BAN HANG
Moi khach hang co mot ma khach hang (MAKH) duy nhat, moi
MAKH xac nh c cac thong tin ve khach hang nh : ho ten khach
hang (HOTEN), a ch (IACHI), so ien thoai (IENTHOAI). Cac mat
hang c phan loai theo tng nhom hang, moi nhom hang co mot
ma nhom (MANHOM) duy nhat, moi ma nhom hang xac nh ten
nhom hang (TENNHOM), tat nhien mot nhom hang co the co nhieu
mat hang. Moi mat hang c anh mot ma so (MAHANG) duy nhat,
moi ma so nay xac nh cac thong tin ve mat hang o nh : ten
hang (TENHANG), n gia ban (ONGIA), n v tnh (VT). Moi hoa
n ban hang co mot so hoa n (SOH) duy nhat, moi hoa n xac
nh c khach hang va ngay lap hoa n (NGAYLAPH), ngay ban
hang (NGAYBAN). Vi moi mat hang trong mot hoa n cho biet so
lng ban (SLBAN) cua mat hang o.
43
HOA
N
KHACH
HANG
soH
ngayLapH
ngayBan
maKH
hoTen
diaChi
dienThoai
Mohnh ER valc oCSDL
co
thuoc ve
Mohnh ER
Lc oCSDL
KHACH HANG(maKH,hoTen,diaChi,dienThoai)
HOADON(soHD,ngayLapHD,ngayBan,maKH)
DONGHOADON(soHD,maHang,sLBan)
NHOM
HANG
maNhom
tenNhom
thuoc ve
bao gom
MATHANG(maHang,tenHang,donGia,DVT,maNhom)
MAT
HANG
ke
cha maHang
tenHang
donGia
VT
DONG
HOA N
c kebi thuoc ve
soH
maHang
sLBan
HOA N BAN HANG
Ngay 1 thang 3 nam 2003
Ngi mua hang: Tran Thai Hung
a ch: 125 Nguyen ThMinh Khai
HANG MS N GIA SOLNG
Giay A4 VP001 35.000 1
Bang keo VP005 20.000 2
44
QUAN LY DAY HOC
e quan ly lch day cua cac giao vien va lch hoc cua cac lp, mot trng to chc nh sau:
Moi giao vien co mot ma so giao vien (MAGV) duy nhat, moi MAGV xac nh cac thong tin nh: ho va ten
giao vien (HOTEN), so ien thoai (DTGV). Moi giao vien co the day nhieu mon cho nhieu khoa nhng ch thuoc
s quan ly hanh chanh cua mot khoa nao o.
Moi mon hoc co mot ma so mon hoc (MAMH) duy nhat, moi mon hoc xac nh ten mon hoc(TENMH). ng
vi moi lp th moi mon hoc ch c phan cho mot giao vien.
Moi phong hoc co mot so phong hoc (PHONG) duy nhat, moi phong co mot chc nang (CHUCNANG); chang
han nh phong ly thuyet, phong thc hanh may tnh, phong nghe nhn, xng thc tap c kh,
Moi khoa co mot ma khoa (MAKHOA) duy nhat, moi khoa xac nh cac thong tin nh: ten khoa (TENKHOA), ien
thoai khoa(DTKHOA).
Moi lp co mot ma lp (MALOP) duy nhat, moi lp co mot ten lp (TENLOP), s so lp (SISO). Moi lp co
the hoc nhieu mon cua nhieu khoa nhng ch thuoc s quan ly hanh chnh cua mot khoa nao o.
Hang tuan, moi giao vien phai lap lch bao giang cho biet giao vien o se day nhng lp nao, ngay nao
(NGAYDAY), mon g?, tai phong nao, t tiet nao (TUTIET) en tiet nao (ENTIET),ta e bai day (BAIDAY),
nhng ghi chu (GHICHU) ve cac tiet day nay, ay la gi day ly thuyet (LYTHUYET) hay thc hanh - gia s
neu LYTHUYET=1 th o la gi day thc hanh va neu LYTHUYET=2 th o la gi ly thuyet, mot ngay co 16
tiet, sang t tiet 1 en tiet 6, chieu t tiet 7 en tiet 12, toi t tiet 13 en 16.
45
MON
HOC
GIAO
VIEN
maMH
tenMH
maGV
hoTen
dTGV
thuoc ve
Mohnh ER
PHONG
soPhong
chucNang
KHOA
quan ly
maKhoa
tenKhoa
dTKhoa
bao
bao
LOP
maLop
tenLop
siSo
DONG BAO
GIANG
maLich
tuTiet
denTiet
baiDay
ghiChu
lyThuyet
c bao bi
LCH BAO
GIANG
maLich
ngayDay
bao
bao
c bao bi
c bao bi
c bao bi
quan ly
thuoc ve
cha
thuoc ve
46
CAC TNH CHAT CUA PHEP TOAN TAP HP
(r:E1):E2 = (r:E2):E1
Chng minh:
(r:E1):E2 = {t|te(r:E1) va t(E2)}
= {t|te{t|ter va t(E1)} va t(E2)}
= {ter|t(E1) va t(E2)}
= {t|te{t|ter va t(E2)} va t(E1)}
= {t|te(r:E2) va t(E1)}
= (r:E2):E1
(r+s):E = (r:E)+(s:E)
Chng minh:
(r+s):E = {t|te(r+s) va t(E)}
= {t|te{t|ter hoac tes} va t(E)}
= {t|(ter hoac tes) va t(E)}
= {t|(ter va t(E)) hoac (tes va t(E))}
= {t|te{t|ter va t(E)} hoac te{t|tes va t(E)}}
= {t|te(r:E) hoac te(s:E)}
= (r:E)+(s:E)
47
CAC TNH CHAT CUA PHEP TOAN TAP HP
(r*s):E = (r:E)*(s:E)
Chng minh:
(r*s):E = {t|te(r*s) va t(E)}
= {t|te{t|ter va tes} va t(E)}
= {t|ter va tes va t(E)}
= {t|(ter va t(E)) va (tes va t(E))}
= {t|te{t|ter va t(E)} va te{t|tes va t(E)}}
= {t|te(r:E) va te(s:E)}
= (r:E)*(s:E)
(r-s):E = (r:E)-(s:E)
Chng minh:
(r-s):E = {t|te(r-s) va t(E)}
= {t|te{t|ter va tes} va t(E)}
= {t|ter va tes va t(E)}
= {t|(ter va t(E)) va (tes va t(E))}
= {t|te{t|ter va t(E)} va te{t|tes va t(E)}}
= {t|te(r:E) va te(s:E)}
= (r:E)*(s:E)
48
CAC TNH CHAT CUA PHEP TOAN TAP HP
Vi X
2
_ X
1


(r.X
2
).X
1
= r.X
1
Chng minh:
(r.X
2
).X
1
= {t.X
1
|te(r.X
2
)}
= {t.X
1
|te{t.X
2
|ter}}
= {(t.X
2
).X
1
|ter }


= {t.X
1
|t er} v X
1
_ X
2

= r.X
1
E phat bieu tren X (r:E).X = (r.X):E
Chng minh:
(r:E).X = {t.X|te(r:E)}
= {t.X|te{t|ter va t(E)}}
= {t.X|ter va t(E)}
= {t|te{t.X|ter} va t(E)}
= {t|te(r.X) va t(E)}
= (r.X):E
49
CAC TNH CHAT CUA PHEP TOAN TAP HP
(q|><|r)|><|s = q|><|(r|><|s)
Chng minh:
(q|><|r)|><|s ={t
12
|-t
1
e(q|><|r),-t
2
es t
12
.Q
+
R
+
= t
1

t
12
.S
+
=t
2
t
12
.A
i
u
1
t
12
.B
j
}
={t
12
|-t
1
e{u
12
|-u
1
eq,-u
2
er u
12
.Q
+
=u
1
u
12
.R
+
=u
2
u
1
.C
k
u
2
u
2
.D
l
},
-t
2
es t
12
.Q
+
R
+
= t
1
,t
12
.S
+
=t
2
t
1
.A
i
u
1
t
2
.B
j
}
={t
123
|-t
1
eq,-t
2
er,-t
3
es t
123
.Q
+
=t
1
,t
123
.R
+
=t
2
t
123
.S
+
=t
3

t
123
.A
i
u
1
t
123
.B
j
, t
123
.C
k
u
2
t
123
.D
l
}
={t
12
|-t
1
eq,-t
2
e{u
12
|-u
1
er,-u
2
es u
12
.R
+
=u
1

u
12
.S
+
=u
2
u
1
.C
k
u
2
u
2
.D
l
},t
12
.Q
+
=t
1
t
12
.R
+
S
+
=t
2
t
12
.A
i
u
1
t
12
.B
j
}
={t
12
|-t
1
eq,-t
2
e(r|><|s),t
12
.Q
+
=t
1
t
12
.R
+
S
+
=t
2
t
12
.A
i
u
1
t
12
.B
j
}
=q|><|(r|><|s)
50
NGON NG TRUY VAN SQL
kieu dlieu
ten thuoc tnh
kch thc dlieu
Tao khoa chnh:
nhap chuot vao letrai
thuoc tnh roi nhap chuot
len bieu tng cha khoa
Start-> Programs-> Microsoft Access (khi ong Access)
Blank Database->OK->qLSV->Create (tao tap tin csdl qLSV)
Tables->New->Design View->OK (tao bang)
51
Literal value New York, 1056.25, #12/2/2001#
Constant la mot ten ai dien cho mot gia tr khong thay oi nh :True, False,Null
Toan t so hoc +,-,*,/,\,^,Mod (ALGEBRA OPERATORS)
Toan t luan ly NOT,AND,OR (STANDART LOGICAL OPERATORS, BOOLEAN OPERATOR)
Toan t so sanh <,<=,>,>=,=,<>
Toan t khac Between...And...,Like,&, IN
Wildcard character
Ky t Y ngha V du
? ai dien cho mot ky t bat ky A? -> AN, AM, AC
* ai dien cho mot chuoi ky t bat ky. A* -> AI, ANH,
# ai dien cho mot ky so A## ->A13, A24, A35
[] ai dien cho cac ky t nam trong A[IN] -> AI, AN
- ai dien cho cac ky t nam trong khoang A[M-O] -> AM, AN, AO
! ai dien cho ky t khong phai ky t nam sau ! A[!C] -> AA, AB, AD, ...
Ham IIf(ieu kien, tr 1, tr 2),Date(),Now(bieu thc ngay), Time(bieu thc ngay),Day(bieu thc ngay), Month(bieu thc ngay),Year(bieu thc
ngay),Len( bieu thc chuoi),Chr(ma Ascii), InStr(Start, s1, s2),LCase(s), UCase(s),Left(s, n), Right(s, n),Mid(s, i, n), Nz(v1, v2)
For which task in SQL would you use an IN clause
a) To query the database for unknown values
b) To query the database for a range of values
c) To query the database for a character pattern
d) To query the database for values in a specified list
52
Ngon ng truy van c chia thanh ba loai:
Data definition query
Dynaset query (data retrieval query)
Action query (data modification query)
Data definition query
Tao lc o quan he rSV:
Create Table rSV (MASV Text (10) CONSTRAINT khoaChinh PRIMARY KEY,HOTEN Text (30), NU YesNo, NGAYSINH
Date, MALOP Text (10), TINH Text (50), HOCBONG Double)
Tao lc o quan he rKQ:
Create Table rKQ (MASV text (10),MAMH Text (10), DIEMTHI Double,CONSTRAINT khoaChinh PRIMARY KEY
(MASV,MAMH) )
MASV HOTEN NU NGAYSINH MALOP TINH HOCBONG
MASV MAMH DIEMTHI
53
Them cot DTHOAI vao lc o quan he rSV:
Alter Table rSV Add Column DTHOAI Text (20)
Sa kieu kieu d lieu cua mot cot:
Alter Table rSV Alter Column DTHOAI double
Xoa cot cua mot lc o a co:
Alter Table rSv Drop Column DTHOAI
Xoa rang buoc khoa chnh:
Alter Table rSv Drop Constraint khoaChinh
Them rang buoc khoa chnh:
Alter Table rSv Add Constraint khoaChinh Primary Key (MASV)
Them rang buoc mien gia tr:
ALTER TABLE rSV WITH NOCHECK ADD CONSTRAINT mgthb CHECK (hocBong <= 500000 )
Them khoa ngoai:
ALTER TABLE rSV ADD FOREIGN KEY REFERENCES rLop (maLop)
Which of the following will actually describe the layout of a table to the DBMS?
a) SELECT TABLE
b) DROP TABLE
c) CREATE TABLE
d) DELETE FROM TABLE
54
Dynaset query (data retrieval query)
Use the following tables and data. The primary keys are underlined. All of the fields are
strings, except STATUS, WEIGHT, and QUANTITY, which are Integers, and Shipment
Date, which is Date. The table names are Shipper, Parts, Shipment, respectively.
PARTS
PNUM PNAME COLOR WEIGHT
P1 Nut Red 12
2 P2 Bolt Green 17
1 2 P3 Screw Blue 17
2 P4 Screw Red 14
1 P5 Cam Blue 12
2 P6 Cog Red 19
How many records does the following SQL example return?

SELECT color
FROM parts
WHERE color = 'Blue' OR weight > 12;

1: color=Blue
2: weight > 12
COLOR
Green
Blue
Red
Blue
Red
SELECT DISTINCT color
FROM parts WHERE
color = 'Blue'
OR weight > 12;
COLOR
Green
Blue
Red
55
PARTS
PNUM PNAME COLOR WEIGHT
P1 Nut Red 12
P2 Bolt Green 17
P3 Screw Blue 17
P4 Screw Red 14
P5 Cam Blue 12
P6 Cog Red 19
SHIPMENT
SNUM PNUM Shipment Date Quantity
S1 P1 9/23/2001 300
S1 P2 3/4/2000 200
S1 P6 8/30/1999 100
S2 P1 4/12/1999 300
S2 P2 5/23/2000 400
S3 P2 7/8/2001 200
S4 P2 9/21/2000 200
S4 P4 10/1/2001 300
How many records does the following SQL example return?
SELECT parts.COLOR, Shipment.Quantity
FROM parts INNER JOIN Shipment ON parts.PNUM = Shipment.PNUM
WHERE (((parts.COLOR)='red') AND ((parts.WEIGHT)>10));
The result of the SQL
COLOR QUANTITY
RED 300
RED 100
RED 300
RED 300
Select Distinct ..........
COLOR QUANTITY
RED 300
RED 100
56
FROM parts INNER JOIN Shipment ON parts.PNUM = Shipment.PNUM
SNUM PNUM Shipment Date Quantity PNUM PNAME COLOR WEIGHT
1 2 S1 P1 9/23/2001 300 P1 Nut Red 12
2 S1 P2 3/4/2000 200 P2 Bolt Green 17
1 2 S1 P6 8/30/1999 100 P6 Cog Red 19
1 2 S2 P1 4/12/1999 300 P1 Nut Red 12
2 S2 P2 5/23/2000 400 P2 Bolt Green 17
2 S3 P2 7/8/2001 200 P2 Bolt Green 17
2 S4 P2 9/21/2000 200 P2 Bolt Green 17
1 2 S4 P4 10/1/2001 300 P4 Screw Red 14
PARTS
PNUM PNAME COLOR W..
P1 Nut Red 12
P2 Bolt Green 17
P3 Screw Blue 17
P4 Screw Red 14
P5 Cam Blue 12
P6 Cog Red 19
SHIPMENT
SNUM PNUM Shipment Date Quantity
S1 P1 9/23/2001 300
S1 P2 3/4/2000 200
S1 P6 8/30/1999 100
S2 P1 4/12/1999 300
S2 P2 5/23/2000 400
S3 P2 7/8/2001 200
S4 P2 9/21/2000 200
S4 P4 10/1/2001 300 WHERE (((parts.COLOR)='red') AND ((parts.WEIGHT)>10))
57
The following database contains weather measurements for a number of stations around the UK.. Each station is in a
region, each station records a value for rainfall in cm and sunshine in hours.
region(regionid, name)
station(stationid, rainfall, sunshine, region)
Select the SQL statement which will list the station identity and rainfall for any station with more rainfall than station 6.
a) SELECT stationid, rainfall
FROM station
WHERE rainfall > (SELECT stationid, rainfall FROM station WHERE stationid=6)
REGION
REGIONID NAME
1 BORDERS
2 FIFE
3 LOTHIAN
STATION
STATIONID RAINFALL SUNSHINE REGION
1 10 2 1
2 11 4 1
3 55 0 3
4 23 1 3
5 17 6 2
6 11 4 2
7 41 3 2
58
FROM station a, station b
STATIONID RAINFALL SUNSHINE REGION STATIONID RAINFALL SUNSHINE REGION
1 10 2 1 1 10 2 1
...... ... .. .. .. ... ... ...
1 10 2 1 6 11 4 2
2 11 4 1 6 11 4 2
x 3 55 0 3 6 11 4 2
x 4 23 1 3 6 11 4 2
x 5 17 6 2 6 11 4 2
6 11 4 2 6 11 4 2
x 7 41 3 2 6 11 4 2
1 10 2 1 7 .... ... ...
... ... .. ... .. .. ... ..
b) SELECT a.stationid, a.rainfall
FROM station a, station b
WHERE a.rainfall > b.rainfall AND b.stationid = 6;
c) SELECT a.stationid, a.rainfall
FROM station a, station b
WHERE a.rainfall < b.rainfall AND b.stationid = 6
d) SELECT stationid, rainfall
FROM station
WHERE stationid=6
e) SELECT b.stationid, a.rainfall
FROM station a, station b
WHERE a.rainfall < b.rainfall
AND a.stationid = 6
59
SHIPPER
SNUM SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
PARTS
PNUM PNAME COLOR WEIGHT
P1 Nut Red 12
P2 Bolt Green 17
P3 Screw Blue 17
P4 Screw Red 14
P5 Cam Blue 12
P6 Cog Red 19
SHIPMENT
SNUM PNUM Shipment Date Quantity
S1 P1 9/23/2001 300
S1 P2 3/4/2000 200
S1 P6 8/30/1999 100
S2 P1 4/12/1999 300
S2 P2 5/23/2000 400
S3 P2 7/8/2001 200
S4 P2 9/21/2000 200
S4 P4 10/1/2001 300
SELECT Sum(parts.WEIGHT) AS [SUM]
FROM (parts INNER JOIN Shipment ON parts.PNUM = Shipment.PNUM) INNER
JOIN Shipper ON Shipment.SNUM = Shipper.SNUM
GROUP BY Shipper.CITY, parts.COLOR
HAVING (((Shipper.CITY)='London') AND ((parts.COLOR)='Red'));
Aggregate function, group function
SUM () MAX()
MIN() AVG()
COUNT()
60
FROM (parts INNER JOIN Shipment ON parts.PNUM = Shipment.PNUM) INNER JOIN Shipper ON Shipment.SNUM =
Shipper.SNUM
SNUM PNUM .... Quantity PNUM PNAME COLOR WEIGHT SNUM SNAME STATUS CITY
S1 P1 300 P1 Nut Red 12 S1 Smith 20 London
S1 P2 200 P2 Bolt Green 17 S1 Smith 20 London
S1 P6 100 P6 Cog Red 19 S1 Smith 20 London
S2 P1 300 P1 Nut Red 12 S2 Jones 10 Paris
S2 P2 400 P2 Bolt Green 17 S2 Jones 10 Paris
S3 P2 200 P2 Bolt Green 17 S3 Blake 30 Paris
S4 P2 200 P2 Bolt Green 17 S4 Clark 20 London
S4 P4 300 P4 Screw Red 14 S4 Clark 20 London
GROUP BY Shipper.CITY, parts.COLOR
SNUM PNUM .... Quantity PNUM PNAME COLOR WEIGHT SNUM SNAME STATUS CITY
S1 P2 200 P2 Bolt Green 17 S1 Smith 20 London
S4 P2 200 P2 Bolt Green 17 S4 Clark 20 London
S1 P1 300 P1 Nut Red 12 S1 Smith 20 London
S1 P6 100 P6 Cog Red 19 S1 Smith 20 London
S4 P4 300 P4 Screw Red 14 S4 Clark 20 London
S2 P2 400 P2 Bolt Green 17 S2 Jones 10 Paris
S3 P2 200 P2 Bolt Green 17 S3 Blake 30 Paris
S2 P1 300 P1 Nut Red 12 S2 Jones 10 Paris
WEIGHT
45
HAVING (((Shipper.CITY)='London') AND ((parts.COLOR)='Red'));
COLOR WEIGHT CITY
Green 34 London
Red 45 London
Green 34 Paris
Red 12 Paris
Which of the following is not a Group function?
a) AVG
b) SQRT
c) SUM
d) MAX
e) MIN
61
Truy van long nhau (nested query, sub query)
La nhng cau lenh truy van ma trong thanh phan WHERE hay HAVING co cha them mot cau lenh Select khac. Cau lenh
select khac nay goi la subquery. Ta long cau Select vao phan Where hay Having theo cu phap sau:
bieuthuc toanTuSoSanh [ANY | ALL | SOME] (cauLenhSQL)
ANY, SOME la bat ky, ALL la tat ca
Cac mau tin cua query chnh thoa man toan t so sanh vi bat ky/ tat ca mau tin nao cua subquery
bieuThuc [NOT] IN (cauLenhSQL)
Cac mau tin cua query chnh co gia tr bang vi mot gia tr trong subquery
[NOT] EXISTS (cauLenhSQL).
Cac mau tin cua query chnh thoa man khi subquery khong/co mau tin
Which of the following statements will return the names of the products with Product ID 10, 11, or 42?
a) SELECT ProductName FROM products WHERE ProductID IN (10,11,42)
b) SELECT ProductName FROM products WHERE ProductID IN 10 OR 11 OR 42
c) SELECT ProductName FROM products WHERE ProductID = (10,11,42)
d) SELECT ProductName FROM products WHERE ProductID IS (10,11,42)
e) None of the above
Which line in the following SQL statement contains an error?
1. SELECT title
2. FROM books
3. WHERE pubid EXISTS IN
4. (SELECT pubid
5. FROM books
6. WHERE retail > 41.95);
a) Line 1
b) Line 3
c) Line 4
d) Line 6
62
student(student_id, student_name, address, tutor_id)
enrolment(student_id, subject_id, mark)
subject(subject_id, subject_name, department)
SELECT subject_name, Count(*) FROM subject, enrolment
WHERE subject.subject_id = enrolment.subject_id AND mark > 40 AND subject_id IN
(SELECT subject_id FROM enrolment GROUP BY subject_id HAVING Count(*) > 4 )
GROUP BY subject_name
SELECT subject_name, Count(*) FROM subject INNER JOIN enrolment ON subject.subject_id = enrolment.subject_id
WHERE mark > 40 AND subject_id IN
(SELECT subject_id FROM enrolment GROUP BY subject_id HAVING Count(*) > 4 )
GROUP BY subject_name
ENROLMENT
student_id subject_id mark
9901 CP1500 45
9902 CP1500 60
9901 CP1600 50
9902 CP1600 30
9903 CP1600 20
9904 CP1600 45
9905 CP1600 40
STUDENT
student_id student_name address tutor_id
9901 Jenny 45 Hay street T01
9902 Mun chan 7 Blacktown Road T01
9903 Alexander 281 Pilbara Way T02
9904 Sarah 25 Hay street T03
9905 Helga 30 Hay street T03
SUBJECT
subject_id subject_name department
CP1500 Database IT
CP1600 Visual Basic IT
63
FROM subject, enrolment
student_id subject_id mark subject_id subject_name department
1 2 9901 CP1500 45 CP1500 Database IT
1 2 9902 CP1500 60 CP1500 Database IT
9901 CP1600 50 CP1500 Database IT
9902 CP1600 30 CP1500 Database IT
9903 CP1600 20 CP1500 Database IT
9904 CP1600 45 CP1500 Database IT
9905 CP1600 40 CP1500 Database IT
9901 CP1500 45 CP1600 Visual Basic IT
9902 CP1500 60 CP1600 Visual Basic IT
1 2 3 9901 CP1600 50 CP1600 Visual Basic IT
1 9902 CP1600 30 CP1600 Visual Basic IT
1 9903 CP1600 20 CP1600 Visual Basic IT
1 2 3 9904 CP1600 45 CP1600 Visual Basic IT
1 9905 CP1600 40 CP1600 Visual Basic IT
1: subject.subject_id = enrolment.subject_id
2: mark > 40
3: subject_id IN
(SELECT subject_id FROM enrolment GROUP BY subject_id HAVING Count(*) > 4 )
64
student_id subject_id mark subject_id subject_name department
9901 CP1600 50 CP1600 Visual Basic IT
9904 CP1600 45 CP1600 Visual Basic IT
The result
subject_id
CP1600
ENROLMENT
student_id subject_id mark
9901 CP1500 45
9902 CP1500 60
9901 CP1600 50
9902 CP1600 30
9903 CP1600 20
9904 CP1600 45
9905 CP1600 40
subject_id Count(*)
CP1500 2
CP1600 5
SELECT subject_id FROM enrolment GROUP BY subject_id HAVING Count(*) > 4
SELECT subject_name, Count(*)
....
GROUP BY subject_name
subject_name Count(*)
Visual Basic 2
65
SELECT student_id
FROM student
WHERE student_id NOT IN
(SELECT student_id FROM enrolment
WHERE subject_id = 'CP1500')
SELECT student_id
FROM enrolment
WHERE NOT EXISTS
(SELECT student_id FROM enrolment
WHERE subject_id = 'CP1500')
ENROLMENT
student_id subject_id mark
x 9901 CP1500 45
x 9902 CP1500 60
9901 CP1600 50
9902 CP1600 30
9903 CP1600 20
9904 CP1600 45
9905 CP1600 40
STUDENT
student_id student_name address tutor_id
9901 Jenny 45 Hay T01
9902 Mun chan 7 ... T01
x 9903 Alexander 281 T02
x 9904 Sarah 25.. T03
x 9905 Helga 30 ... T03
student_id
9903
9904
9905
student_id
In a nested query, which query is evaluated first?
a) The innerrmost query
b) the outermost query
c) both are evaluated simultaneously
d) whichever query is selected to execute first by the user
66
Truy van cap nhat d lieu (action query, data modification query)
Update table Set field1 = bieuThc1, field2 = bieuThc2 [Where ieuKien]
Delete From table [Where ieuKien]
Customer (cid, cname, city, discnt),
Products (pid, pname, city, quantity, price),
Agents (aid, aname, city, percent) and
Oders (ordno, month, cid, aid, pid, qty, dollars).
Raise the price of each product in the PRODUCTS table by 10% for all PRODUCTS that were ordered from customer c01
a) Update products set price = price * 1.1 where orders.cid = c01;
b) Update products p, Orders o set Products.price = Products.price * 1.1 where orders.cid = c01;
c) Update products set price = .10 * price where pid in (select pid from orders where cid = c01);
d) update products set price = 1.1 * price where pid in (select pid from orders where cid = c01) ;
Delete all customers that did not order product p01
a) Delete ALL Customers where cid not in (selest cid from orders where pid = p01);
b) Delete from Customers where products.pid <> p01;
c) Delete from Customers where cid in (select cid from orders where pid <> p01);
d) delete from Customers where cid not in (select cid from orders where pid = p01);
ordno month cid aid pid qty dollars
H0001 5 C01 A01 P01 10 100
H0002 6 C01 A02 P02 20 150
H0003 5 C02 A01 P01 15 200
H004 6 C03 A01 P02 20 200
67
Truy van chon
Select [Distinct|Top n[%]] field1 [As alias1][,field2 [As alias2][,...]]
From table1 [Inner Join table2 On table1.field1 u table2.field2] ...
[Where dieuKien]
[Order By field1[Asc|Desc][,field2[Asc|Desc]][,...]]];
V du
SELECT MASV,HOTEN,HOCBONG
FROM Sv;

SELECT MASV,HOTEN,HOCBONG
FROM Sv
WHERE NU And MALOP='CTH2B' And HOCBONG>0;

SELECT MASV,HOTEN,HOCBONG
FROM Sv
ORDER BY HOCBONG DESC;

SELECT MASV,HOTEN,TENLOP
FROM Sv Inner Join Lop On Sv.MALOP = Kh.LOP;
68
iem >= 5
MAHV MAMH DIEM MAHV MAMH DIEM
99001 DB 5.0 99001 DB 5.0
99002 DS 2.0 99003 NW 8.0
99003 NW 8.0
INNER JOIN
MAHV MAMH DIEM MAMH TENMH
99001 DB 5.0 DB C S D LIEU
99002 NW 2.0 DS CAU TRUC D LIEU
99002 DS 8.0 NW MANG MAY TNH
MAHV MAMH DIEM TENMH
99001 DB 5.0 C S D LIEU
99002 NW 2.0 MANG MAY TNH
99002 DS 8.0 CAU TRUC D LIEU
69
Truy van nhom d lieu
Select [Distinct|Top n[%]] field1 [As alias1][,field2 [As alias2][,...]]
From table1 [Inner Join table2 On table1.field1 u table2.field2] ...
[Where dieuKienLocMauTinNguon]
[Group By fieldGroupBy[,fieldGroupBy[,...]]
[Having dieuKienLocMauTinTongHop]
[Order By field1 [Asc|Desc][,field2[Asc|Desc]][,...]]]

Group functions (aggregate functions) COUNT,MIN,MAX,SUM, AVG
V du
Lap danh sach sinh vien co ang ky t nhat la ba mon hoc

SELECT Kq.MASV,HOTEN,NU,NGAYSINH,COUNT(MAMH) As SLMH
FROM Sv Inner Join Kq On Sv.MASV = Kq.MASV
GROUP BY Kq.MASV,HOTEN,NU,NGAYSINH
HAVING COUNT(MAMH)>=3;

em so lng sinh vien n cua moi khoa
SELECT Kh.MAKHOA,TENKHOA,COUNT(Kh.MAKHOA) AS SOLUONG
FROM (Sv Inner Join Lop On Sv.MALOP = LOP.MALOP)
INNER JOIN Kh On Lop.MAKHOA=Kh.MAKHOA
WHERE NU
GROUP BY Kh.MAKHOA,TENKHOA;
70
Truy van long nhau (nested query, subquery)
toanTuSoSanh [ANY | ALL | SOME] (cauLenhSQL)
bieuThuc [NOT] IN (cauLenhSQL)
[NOT] EXISTS (cauLenhSQL).
V du:
Lap danh sach sinh vien co hoc bong cao nhat
SELECT *
FROM Sv
WHERE HOCBONG>=ALL(SELECT HOCBONG FROM SV)

Lap danh sach sinh vien co iem thi mon CSDL cao nhat
SELECT SV.MASV,HOTEN,NU,NGAYSINH,DIEMTHI
FROM sv Inner Join kq On Sv.MASV = Kq.MASV
WHERE MAMH='CSDL' AND DIEMTHI >= ALL
(SELECT DIEMTHI FROM KQ WHERE MAMH='CSDL')
Truy van cap nhat d lieu (action query, data modification query)
Update table Set field1 = bieuThc1, field2 = bieuThc2 Where ieuKien
Delete From table Where ieuKien
Truy van hp
Select .....Union Select ....
V du:
Lenh sau ay gap oi danh sach sinh vien
SELECT MASV,HOTEN,NGAYSINH UNION SELECT MASV,HOTEN,NGAYSINH
71
Bai tap 2
Nhanvien(MANV,HOTEN,NGAYSINH,PHAI,DIACHI,MAPB)
Phongban(MAPB,TENPB)
Cong(MACT,MANV,SLNGAYCONG)
Congtrinh(MACT,TENCT,DIADIEM,NGAYCAPGP,NGAYKHOICONG,NGAYHT)
Bai tap 3:
Monhoc(MSMH ,TENMH,SOTINCHI ,TINHCHAT)
Sinhvien(MSSV,HOTEN,NGAYSINH,LOP)
Diem(MSSV,MSMH,DIEMTHI)
Bai tap 4:
Docgia(MADG,HOTEN,NGAYSINH,DIACHI,NGHENGHIEP)
Phieumuon(SOPM,NGAYMUON,MADG)
Chitietmuon(SOPM,MADAUSACH,NGAYTRA)
Dausach(MADAUSACH,BAN,TAP,MASH)
Sach(MASH,TENSACH,TACGIA,NHAXB,NAMXB)
Bai tap 5:
Khach(MAKH,HOTEN,DIACHI,DIENTHOAI)
Hoadon(SOHD,NGAYLAPHD,NGAYBAN,MAKH)
DongHoaDon(SOHD,MAHANG,SLBAN)
Hang(MAHANG,TENHANG,DONGIA,DVT,MANHOM)
Nhom(MANHOM,TENNHOM)
72
Bai tap 6:
Giaovien(MAGV,HOTEN,DTGV,MAKHOA)
Khoa(MAKHOA,TENKHOA,DTKHOA)
Lop(MALOP,TENLOP,SISO,MAKHOA)
Monhoc(MAMH,TENMH)
Phonghoc(SOPHONG,CHUCNANG)
Lichbaogiang(MALICH,NGAYDAY,MAGV)
Dongbaogiang(MALICH,TUTIET,DENTIET,BAIDAY,GHICHU,LYTHUYET,
MAMH,MALOP,SOPHONG)
Bai tap quan ly thi
Q1:HD(MAHDT,TENHDT, TENCT, DCHDT,DTHDT)
Q2:PT(SOPT,DCPT,MAHDT)
Q3:TS(SOBD, TENTS,NGSINH,PHAI,CCNGHE, MATR,SOPT)
Q4:MT(MAMT,TENMT,BUOI,NGAY)
Q5:GV(MAGV,TENGV,CHUYENMON,CHUCDANH,MAHDT,MATR)
Q6:TR(MATR,TENTR,DCTR,LHDT)
Q7:KQ(SOBD,MAMT,DIEMTHI)
73
MASV HOTEN NU NGAYSINH MALOP TINH HOCBONG
99001 TRAN DAN THU TRUE 15-03-1977 CTH2B TIEN GIANG 120000
99002 NGUYEN H DA THAO TRUE 25-04-1986 CTH2B TPHCM 120000
99003 PHAM ANH HUY FALSE 16-08-1977 CTH2B BAC LIEU
99004 NGUYEN NGOC THUAN FALSE 24-12-1980 CTH2B CA MAU
99005 LE THANH TRUNG FALSE 20-11-1978 CAV1 CA MAU 120000
99006 NGUYEN HONG VAN FALSE 19-09-1979 CAV1 SOC TRANG
99007 VU THI LOAN TRUE 15-03-1975 CAV1 CA MAU
99008 TRUONG KIM QUANG FALSE 15-05-1975 CTH2B HA NOI
99009 TON THAT QUYEN FALSE 26-06-1976 CTH2B VUNG TAU 60000
99010 HA VAN LONG FALSE 14-04-1973 CAV1 BAC LIEU
99011 BUI VAN ANH FALSE 22-12-1972 CAV1 AN GIANG
99012 LE HUU CHI FALSE 28-08-1977 CT2 CAN THO 60000
99013 VU THANH CONG FALSE 29-03-1979 CTH2B KIEN GIANG 60000
99014 TRAN QUANG CUONG FALSE 30-05-1981 CT2 DONG THAP 120000
99015 PHAM VAN HAI FALSE 30-06-1976 CT2 CA MAU
99016 HUYNH THANH HOANG FALSE 29-07-1982 CT2 TPHCM 80000
99017 TRAN MINH LAM FALSE 21-08-1977 CTH2B TRA VINH
99018 PHAN VAN SANG FALSE 19-05-1979 CDL1 DONG THAP 120000
99019 PHAM THI HUYEN FALSE 16-06-1982 CDL1 CAN THO 120000
99020 NGUYEN THI NGAN TRUE 11-11-1981 CTH2B CA MAU 120000
99021 PHAM TAN QUANG FALSE 01-01-1980 CDL1 CA MAU
99022 TRAN PHUOC QUYEN FALSE 12-12-1979 CTH2B BAC LIEU 60000
99023 LE THI THANH VAN TRUE 11-11-1980 CDL1 TPHCM 120000
74
RANG BUOC TOAN VEN
Rang buoc toan ven la g?
la cac moi lien he gia cac thuoc tnh, gia cac bo
la cac ieu kien ma cac quan he phai thoa.
trong thc te ta co the hieu rang buoc toan ven la cac qui tac quan ly.
Cac yeu to cua rang buoc toan ven:
ieu kien
t
1
,t
2
e r
t
1
.MASV = t
2
.MASV
cuoi
boi canh la cac quan he ma ieu kien rang buoc co hieu lc
tam anh hng
(Ten RBTV) Them(T) Sa(S) Xoa(X)
r
1

+ - -
r
2

...
... .. ..
...
... ... ...
r
n

75
The type of integrity constraint
entity integrity constraint
derived integrity constraint
referential integrity constraint
domain integrity constraint
data type integrity constraint
MASV MAMH DIEM MAMH TENMH
99001 DB 5.0 DB C S D LIEU
99002 DS hai DS CAU TRUC D LIEU
99003 NW 8.0
NW MANG MAY TNH
99002 SQL 11.0
Entity
integrity
Referential
integrity
Domain
integrity
Data type
integrity
SOLUONG DONGIA THANHTIEN
4 50 250
derived
integrity
76
Cac loai rang buoc:
RBTV lien bo
R
1
:t
1
,t
2
e rKhach
t
1
. MAKH = t
2
. MAKH
Cuoi
RBTV ve s phu thuoc ton tai
R
2
: rDatHang[MAKH] _
rKhach[MAKH]
RBTV ve mien gia tr
R
3
: t e rHoaDon
t.TRIGIAHD > 0
Cuoi
RBTV lien thuoc tnh
R
4
: t e rHoaDon
t.NGAYLAP <= t.NGAYXUAT
Cuoi
R
1
Them Sa Xoa
rKhach
+ + -
R
2
Them Sa Xoa
rDatHang + + -
rKhach
- + +
R
3
Them Sa Xoa
rHoaDon
+ + -
R
3
Them Sa Xoa
rHoaDon
+ + -
77
Cac loai rang buoc:
RBTV lien thuoc tnh lien quan he
R
5
: t
1
e rDatHang, t
2
e
rHoaDon
Neu t
1
.SODH = t
2
.SODH th
t
1
.NGAYDH <= t
2
.NGAYXUAT
Cuoi
RBTV ve thuoc tnh tong hp
la gia tr mot thuoc tnh c tnh toan
t cac gia tr khac nh
THANHTIEN=SOLUONG*DONGIA
R
5
Them Sa Xoa
rDatHang + + +
rHoaDon
+ + +
78
PHU THUOC HAM
phanCong (PHICONG, MAYBAY, NGAYKH, GIOKH)
Cushing 83 9/8 10:15a
Cushing 116 10/8 1:25p
Clark 281 8/8 5:50a
Clark 301 12/8 6:35p
Clark 83 11/8 10:15a
Chin 83 13/8 10:15a
Chin 116 12/8 1:25p
Copely 281 9/8 5:50a
Copely 281 13/8 5:50a
Copely 412 15/8 1:25p
Moi may bay co mot gi khi hanh duy nhat.
Neu biet phi cong, biet ngay gi khi hanh th biet c may bay do phi cong ay lai.
Neu biet may bay, biet ngay khi hanh th biet phi cong lai chuyen bay ay.
79
o MAYBAY xac nh GIOKH
o {PHICONG,NGAYKH,GIOKH} xac nh MABAY
o {MAYBAY,NGAYKH} xac nh PHICONG
hay
o GIOKH phu thuoc ham vao MAYBAY
o MABAY phu thuoc ham vao {PHICONG,NGAYKH,GIOKH}
o PHICONG phu thuoc ham vao {MAYBAY,NGAYKH}
va c ky hieu nh sau:
o {MAYBAY} GIOKH
o {PHICONG,NGAYKH,GIOKH} MABAY
o {MAYBAY,NGAYKH} PHICONG
NH NGHA:
Q(A
1
,A
2
,,A
n
) la lc o quan he.
X, Y la hai tap con cua Q
+
={A
1
,A
2
,,A
n
}.
r la quan he tren Q.
t
1
,t
2
la hai bo bat ky cua r.
X Y (t
1
.X = t
2
.X t
1
.Y = t
2
.Y)
80
He qua: Neu X _ Y th X Y.
Chng minh:
Gia s t
1
.X = t
2
.X do X _ Y nen t
1
.Y = t
2
.Y theo nh ngha suy ra X Y
Trong trng hp nay X Y c goi la phu thuoc ham hien nhien.
V du phu thuoc ham X X la phu thuoc ham hien nhien.
Vay vi r la quan he bat ky, F la tap phu thuoc ham thoa tren r, ta luon co
F _ {cac phu thuoc ham hien nhien}
phanCong (PHICONG, MAYBAY, NGAYKH, GIOKH)
Cushing 83 9/8 10:15a
Clark 83 11/8 10:15a
Chin 83 13/8 10:15a
Cushing 116 10/8 1:25p
Chin 116 12/8 1:25p
Clark 281 8/8 5:50a
Copely 281 9/8 5:50a
Copely 281 13/8 5:50a
Clark 301 12/8 6:35p
Copely 412 15/8 1:25p
MAYBAY->GIOKH ?
81
phanCong (PHICONG, MAYBAY, NGAYKH, GIOKH)
Clark 281 8/8 5:50a
Copely 281 9/8 5:50a
Copely 281 13/8 5:50a
Cushing 83 9/8 10:15a
Clark 83 11/8 10:15a
Chin 83 13/8 10:15a
Cushing 116 10/8 1:25p
Chin 116 12/8 1:25p
Copely 412 15/8 1:25p
Clark 301 12/8 6:35p
SATIFIES(phanCong,GIOKH,MAYBAY)
GIOKHMAYBAY cho ket qua false
82
A B C D
C
A B C D
AB AC AD
BC BD
ABC ABD
CD
ACD
BCD
ABCD
Q
+
= {A,B,C,D}
co bao nhieu tap con?
- co 2
n
=2
4
=16
co bao nhieu phu thuoc ham co the co?
- co 2
n
x2
n
=2
4
x2
4
=256
C C
CA
CB
CAB
......
AC
AA,
AB,
.....
ABCDC
ABCDA
.....
ABCDABCD
In the instance of the relation R(A,B,C,D,E) shown below,
which of the following functional dependencies (FD's) hold?
Briefly justify your answer.
A B C D E
1 2 3 4 5
1 4 3 4 5
1 2 4 4 1
I. AB C
II. B D (T)
III. DE A (T)
Phu thuoc ham (functional dependency, FD)
Q(A1,A2,,An) la lc o quan he.
Q
+
={A1,A2,,An}
X, Y la hai tap con cua Q
+
r la quan he tren Q.
t
1
,t
2
la hai bo bat ky cua r.
X Y (t
1
.X = t
2
.X t
1
.Y = t
2
.Y)
Ky hieu: X Y oc la
- X xac nh Y (X functional determines Y)
-Y phu thuoc vao X (Y functional dependent on X )
-Neu X _ Y th X Y la PTH hien nhien (trivial dependence)
83
Phu thuoc ham c suy dien logic t F
Noi rang phu thuoc ham X Y c suy dien logic t F neu mot quan he r bat ky thoa man tat ca cac
phu thuoc ham cua F th cung thoa phu thuoc ham X Y. Ky hieu F|= X Y.
Bao ong cua F
Bao ong cua F ky hieu F
+
la tap tat ca cac phu thuoc ham c suy dien logic t F.
Cac tnh chat cua tap F
+
1.Tnh phan xa: F _ F
+
2.Tnh n ieu: Neu F _ G th F
+
_ G
+
3.Tnh luy ang: (F
+
)
+
= F
+
.
Goi G la tap tat ca cac phu thuoc ham co the co cua r, phan phu cua F ky hieu F
-
= G - F
+
Chng minh
1.X Y e F r thoa X Y X Y e F
+
2.Neu X Y la phu thuoc ham thuoc F
+
ta phai chng minh X Y thuoc G
+
Gia s r thoa tat ca cac phu thuoc ham cua G (1)
r thoa tat ca phu thuoc ham cua F v F _ G
r thoa phu thuoc ham X Y (2) v X YeF
+

(1) va (2) X Y e G
+
F
+
_ G
+

84
F _ F
+
(tnh phan xa) F
+
_ (F
+
)
+
(1)
Neu X Y e (F
+
)
+
(2) X Y e F
+
that vay: (3)
Gia s r thoa tat ca cac phu thuoc ham cua F (4)
r thoa tat ca cac phu thuoc ham cua F
+
(theo nh ngha)
r thoa tat ca cac phu thuoc ham cua (F
+
)
+
(theo nh ngha)
r thoa X Y (v (2)) X Y e F
+

(1) va (3) (F
+
)
+
= F
+
85
Chng minh:
Luat them vao: gia s co t1.XZ = t2.XZ (1)
t1.X = t2.X
t1.Y = t2.Y (do X Y) (2)
XZ Y (do (1) (2))
Luat hp: gia s co t1.X = t2.X (1)
t1.X = t2.X va t1.Z = t2.Z
t1.XZ = t2.XZ (2)
X YZ (do (1) (2))
Luat phan ra: ga s co t1.X = t2.X (1)
t1.YZ = t2.YZ (do X YZ)
t1.Y = t2.Y (2)
X Y (do (1) (2))
Luat bac cau: gia s co t1.X = t2.X (1)
t1.Y = t2.Y
t1.Z = t2.Z (2)
X Z (do (1) (2))
Luat bac cau gia: gia s co: t1.XZ = t2.XZ (1)
t1.X = t2.X va t1.Z = t2.Z (2)
t1.Y = t2.Y (do X Y) (3)
t1.YZ = t2.YZ (Ket hp (2) va (3))
t1.W = t2.W (do YZ W) (4)
XZ W
86
Noi rang X Y la phu thuoc ham c suy dien nh vao luat dan Armstrong neu ton tai cac tap
phu thuoc ham F
0
c F
1
c... c F
n
sao cho X Y e F
n
vi F
0
,F
1
,...,F
n
lan lt c hnh thanh
thoa phng phap sau:
Bc 1: F
0
= F
Bc 2:chon mot so phu thuoc ham trong F
i
ap dung he luat dan Armstrong e thu c mot so
phu thuoc ham mi. at F
i+1
= F
i
{cac phu thuoc ham mi}
V du:
Cho F = {AB C,C B,BC A} th co F
0
c F
1
c F
2
sao cho C A e F
2
F
0
= {AB C,C B, BC A} ap dung luat hp cho C B va C C
F
1
= {AB C,C B, BC A, C BC} ap dung luat bac cau.
F
2
= {AB C,C B, BC A, C BC, C A}
He qua: He luat dan Armstrong la ung ngha la neu F la tap cac phu thuoc ham ung tren
quan he r va X Y la mot phu thuoc ham c suy dien t F nh he luat dan Armstrong th
X Y ung tren quan he r. Vay X Y la phu thuoc ham c suy dien logic t F
Ta se chng minh he luat dan Armstrong la u ngha la moi phu thuoc ham
X Y c suy dien logic t F se c suy dien t F nh he luat dan Armstrong
87
Bao ong cua tap thuoc tnh X oi vi F ky hieu la X
+
c nh ngha nh sau:
X
+
= A
i
vi X A
i
la phu thuoc ham c suy dien t F nh he tien e Armstrong
Tnh chat:
bao ong cua Q la Q
+
Cac tnh chat cua bao ong
Neu X,Y la cac tap con cua tap thuoc tnh Q
+
th ta co cac tnh chat sau ay:
1.Tnh phan xa: X _ X
+
2.Tnh n ieu: Neu X _ Y th X
+
_ Y
+
3.Tnh luy ang: X
++
= X
+
4. (XY)
+
_ X
+
Y
+
5. (X
+
Y)
+
= (XY
+
)
+
= (X
+
Y
+
)
+
6. X Y Y
+
_ X
+
7. X X
+
va X
+
X
8. X
+
= Y
+
X Y va Y X
Chng minh:
1. X X X
+
_ X
2. A e X
+
X A Y A A e Y
+
3. A e X
++
X
+
A va X X
+
(ap dung 8) X A AeX
+
X
++
_ X
+
. Ap dung 1 X
++
_ X
+

7. X A
1
va X A
2
X A
1
A
2
.... XA
i
= X
+
X
+
_ X X
+
X (Phu thuoc ham hien nhien)
88
He luat dan Armstrong (Armstrong inference rule)
He luat dan la mot phat bieu cho biet neu mot quan he r thoa man mot vai phu thuoc ham th
no phai thoa man phu thuoc ham khac.
Vi X,Y,Z,W la tap con cua Q
+
. r la quan he bat ky cua Q. Ta co 6 luat dan sau:
** Reflexive rule: X X
** Augmentation rule : Cho X Y XZ Y
** Transitive rule : Cho X Y, Y Z X Z
ba luat dan tren la he tien e Armstrong (Armstrongs Axioms)
Union rule : Cho X Y, X Z X YZ
Decomposition rule : Cho X YZ X Y
Pseudo transitive rule : Cho X Y, YZ W XZ W
Bao ong ca tap PTH F (closures of functional dependences sets)
Bao ong cua F ky hieu F
+
la tap tat ca cac phu thuoc ham c suy dien logic t F.
Bao ong cua tap thuoc tnh (closures of attributes sets)
Bao ong cua tap thuoc tnh X oi vi F ky hieu la X
+
la tap cac thuoc tnh phu thuoc vao X
Given the function dependencies RHJ; JM for the relation schema E(RHJM). The dependency RM can be inferred by:
a) The transitive rule c) The reflexive rule
b) None of the above d) The augmentation rule
89
Tnh lien tiep tap cac tap thuoc tnh X
0
,X
1
,X
2
,... theo phng phap sau:
Bc 1: X
0
= X
Bc 2: lan lt xet cac phu thuoc ham cua F
Neu YZ co Y _ X
i
th X
i+1
= X
i
Z
Loai phu thuoc ham Y Z khoi F
Bc 3: Neu bc 2 khong tnh c X
i+1
th X
i
chnh la bao ong cua X
Ngc lai lap lai bc 2
V du 1: Cho lc o quan he Q(ABCDEGH) va tap phu thuoc ham F
F={f
1
: BA; f
2
: DACE; f
3
: DH; f
4
: GH C; f
5
: ACD}
Tm bao ong cua cac tap X = {AC} da tren F.
Giai:
Bc 1: X
+
= AC
Bc 2: Do f
1
, f
2
, f
3
, f
4
khong thoa. f
5
thoa v X
+
_ AC
X
+
= AC D = ACD
Lap lai bc 2: f
1
khong thoa, f
2
thoa v X
+
_ AD:
X
+
= ACD CE = ACDE
f
3
thoa v X
+
_ D
X
+
= ACDE H = ACDEH
f
4
khong thoa, f
5
khong xet v a thoa
Lap lai bc 2: f
1
,f
2
,f
3
khong xet v a thoa, f
4
khong thoa,f
5
khong xet v a thoa.Trong bc nay
X
+
khong thay oi => X
+
={ACDEH} la bao ong cua X
90
Let Q be a relation with attributes (A,B,C,D,E,F) and let the following functional dependencies hold
F={f
1
: BA;
f
2
: DACE;
f
3
: DH;
f
4
: GH C;
f
5
: ACD}
Find the closure of X = {AC}.
Let R be a relation with attributes (A,B,C,D,E,F) and let the following functional dependencies hold.
A B
A C
CD E
CD F
B E
Given the above functional dependencies, which of the following functional dependencies does not hold:
a) A -> E
b) CD -> EF
c) AD -> F
d) B -> CD
Giai:
X
0
= AC
X
1
= AC D = ACD do ve trai f5 thoa X
0
_ AC
X
2
= ACD CE = ACDE do ve trai f2 thoa X
1
_ AD
X
3
= ACDE H = ACDEH do ve trai f3 thoa X
2
_ D

X
3
khong thay oi => X
+
={ACDEH} la bao ong cua X
91
nh ly Thuat toan tm bao ong cho ket qua X
i
= X
+
Chng minh
1. Ta chng minh X
i
_ X
+
bang phng phap qui nap.
Bc c s chng minh X X
0

Theo tnh phan xa cua he luat dan th X X theo thuat toan th X
0
= X
X X
0
. Vay X
0
_ X
+
Bc qui nap gia s co X X
i-1
(1) ta phai chng minh X X
i

Theo thuat toan tm bao ong th co f
j
= X
j
Y
j
e X
i-1
_ X
j
va X
i
= X
i-1
Y
j
X
i-1
Y
j
(2). (1) va (2) X Y
j
(3)
(1) va (3) X X
i-1
Y
j
= X
i
X X
i
Vay X
i
_ X
+
2. Ta chng minh A _ X
+
A _ X
i

A _ X
+
nen co mot phu thuoc ham X A c suy dien nh vao luat dan Armstrong, ma X _
X
i
A _ X
i
92
He qua
Q la lc o quan he. F la tap phu thuoc ham, A la thuoc tnh ch xuat hien ve phai cua cac phu
thuoc ham trong F th X
+
= (X A)
+
A
Q la lc o quan he. F la tap phu thuoc ham, X la tap con cua Q
+
va Y = {cac thuoc tnh xuat hien
ve phai cua cac phu thuoc ham trong F} th X
+
_ X Y.
Chng minh
Theo thuat toan tm bao ong th bao ong X
+
hay (X-A)
+
c hnh thanh qua mot so bc. Ta chng
minh bieu thc X
+
= (X A)
+
A theo qui nap.
Bc c s: X
0
= X, (X-A)
0
= X - A X
0
=(X - A)
0
A ung
Bc qui nap: gia s ta co X
i-1
=(X - A)
i-1
A. Bao ong X
i
c hnh thanh do co f
j
=
X
j
Y
j
e:
X
i-1
_ X
j
va X
i
= X
i-1
Y
j
= (X - A)
i-1
A Y
j
(1).
S hnh thanh X
i
luon keo theo s hnh thanh (X-A)
i
v:
X
i-1
= (X-A)
i-1
A _ X
j
do X
j
khong cha A nen:
(X - A)
i-1
_ X
j
vay (X - A)
i
= (X - A)
i-1
Y
j
(2)
(1) va (2) cho:
X
i
= (X - A)
i
A la ieu phai chng minh
Bc c s: X
0
= X X
0
_ X Y
Bc qui nap: gia s co X
i-1
_ X Y ta chng minh X
i
_ X Y.
Bao ong Xi c hnh thanh do co f
j
= X
j
Y
j
e:
X
i-1
_ X
j
va X
i
= X
i-1
Y
j
_ X Y Y
j
do Y
j
la ve phai cua phu thuoc ham
nen Y Y
j
= Y vay X
i
_ X Y
93
nh ly
He luat dan Armstrong la ay u ngha la moi phu thuoc ham X Y c suy dien logic t F se
c suy dien t F nh he luat dan Armstrong.
Chng minh:
e chng minh XY c suy dien t F nh he luat dan Armstrong ta chng minh bang phng
phap phan chng ngha la neu XY khong suy dien c t he luat dan Armstrong th co quan
he r thoa cac phu thuoc ham F nhng khong thoa phu thuoc ham XY (ieu nay nghch ly vi
gia thuyet la moi quan he r thoa cac phu thuoc ham trong F th r cung thoa phu thuoc ham
XY).
That vay gia s Q(A
1
,A
2
,...,A
n
) la lc o quan he, a
i
,b
i
la cac gia tr khac nhau tren
mien gia tr Ai. r la quan he tren Q co hai bo t va tc xac nh nh sau:
t=(a
1
,a
2
,...,a
n
)
t.A
i
= a
i
neu A
i
e X
+
t.Ai= b
i
neu A
i
e X
+
Vay quan he r co t.X = t.X nhng t.Y = t.Y (t.Y gom cac gia tr ai con t.Y phai co t nhat mot bi
neu khong Y _ X+ X Y c suy dan t he luat dan Armstrong ). Nh vay r khong thoa phu
thuoc ham X Y.
Bay gi ta chng minh quan he r thoa moi phu thuoc ham trong F. Goi W Z la phu thuoc ham
trong F.
Neu W . X+ t.W = t.W menh e (t.W = t.W t.Z = t.Z)ung
Neu W _ X+ t.Z = t.Z = bo cac ai
menh e (t.W = t.W t.Z = t.Z)ung
94
He qua:
Bao ong cua tap thuoc tnh X oi vi F la:
X
+
= Ai vi X Ai la phu thuoc ham c suy dien logic t F
Tnh chat
X Y e F
+
Y _ X
+

Chng minh
X Y co k sao cho Y = A
k
_ A
i
= X
+
Y _ X
+
X
+
=Y (X
+
- Y)X Y (X
+
- Y) X Y

Thuat toan xac nh f = XY co la thanh vien cua F hay khong
Bc 1: tnh X
+

Bc 2: so sanh X
+
vi Y neu X
+
_ Y th ta khang nh X Y la thanh vien cua F
Thuat toan tm F
+

Bc 1: Tm tat ca tap con cua Q
+

Bc 2: Tm tat ca cac phu thuoc ham co the co cua Q.
Bc 3: Tm bao ong cua tat ca tap con cua Q.
Bc 4: Da vao bao ong cua tat ca cac tap con a tm e xac nh phu thuoc ham nao thuoc F
+
95
Q(A,B,C) F = {AB C,C B} F
+
?
Tat ca cac tap con cua tap thuoc tnh






Tat ca cac phu thuoc ham co the co:
C A B C
C {A} {B} {C}
{A,B} {A,C}
{B,C}
{A,B,C}
AB ABC BC ABCeF CA CBCeF
+
ACBCeF
+
BCAC
AAB AABC BAC ABACeF
+
CBeF CABC ACABCeF
+
BCABC
AC BA BBC ABBCeF
+
CAB ACBeF
+
BCA
AAC BAB BABC ABABCeF
+
CAC ACABeF
+
BCAB
Bao ong cua tat ca tap con

A
+
= A C
+
= BC
B+ = B AC
+
= ABC
{AB}
+
= ABC BC
+
= BC
F
+
= { ABC, ABAC,
ABBC,ABABC,
CB, CBC,
ACB, ACAB,
ACBC,ACABC}
96
PHU CUA TAP PHU THUOC HAM (cover of dependencies)
Phu thuoc ham tng ng (equivalences among sets of dependencies)
Noi rang hai tap phu thuoc ham F va G la tng ng (equivalent) neu F
+
=G
+

ky hieu F G.
Ta noi F phu G neu F
+
_ G
+

V du: Cho lc o quan he Q(ABCDE) hai tap phu thuoc ham:
F={ABC,AD,CDE} va G={ABCE,AABD,CDE}
a) F co tng ng vi G khong?
b) F co tng ng vi G={ABCDE} khong?
Phu thuoc ham co ve trai d tha
Noi rang phu thuoc ham Z Y co ve trai d tha neu co mot AeZ sao cho F
F-{Z Y}{(Z-A) Y}
V du: cho tap phu thuoc ham F={ABC, BC, ABD} th phu thuoc ham ABD co
ve trai d tha B v:
F F {AB D}{A D}
{A BC,B C,A D}
Tap phu thuoc ham co ve trai khong d tha la tap phu thuoc ham khong co phu thuoc ham co
ve trai d tha
97
PHU CUA TAP PHU THUOC HAM (cover of dependencies)
Phu thuoc ham tng ng (equivalences among sets of dependencies)
Noi rang hai tap phu thuoc ham F va G la tng ng (equivalent) neu F
+
=G
+

ky hieu F G.
Ta noi F phu G neu F
+
_ G
+

e chng minh F va G tng ng ta chng minh:
F
+
_ G

Bang cach: XYeG XY eF
+

G
+
_ F

Bang cach: XYeF XY eG
+

V du: Cho lc o quan he Q(ABCDE) hai tap phu thuoc ham:
F={ABC,AD,CDE} va G={ABCE,AABD,CDE}
F co tng ng vi G khong?

Ta co A
F
+
=ABCDE ABCE eF
+
, AABD eF
+
CD
F
+
= CDE CDE eF
+
F
+
_ G

Ta co A
G
+
=ABCED ABCeG
+
, ADeG
+
,
CD
G
+
=CDE CDEeG
+
G
+
_ F
Vay F
+
=G
+
98
Given the following schema R(Emp #,Dept #,City) functional dependencies, F={Emp#City, Emp#Dept#, Dept#City;
Emp#,Dept#City}. Which set of functions dependencies is a minimal cover for F ?
a) {Emp#City; Dept#City} =A
b) {Emp#City; Emp#Dept#} =B
c) {Emp#Dept#; Dept#City} =C
d) {Empt#,Dept#City; Emp#Dept#} =D
Gii:
a) Emp#
+
=Emp#,City Emp#Dept# e A
+
b) Emp#
+
=Emp#,City,Dept#; Dept#
+
=Dept# Dept#City e B
+

c) Emp#
+
=Emp#,Dept#,City; Dept#
+
=Dept#,City; Emp#,Dept#
+
= Emp#,Dept#,City
d) Emp#
+
=Emp#,Dept#,City; Dept#
+
=Dept# Dept#City e D
+
Minimal cover
F c goi la mot phu toi thieu neu F thoa ong thi ba ieu kien sau:
1) F la tap phu thuoc ham co ve trai khong d tha
2) F la tap phu thuoc ham co ve phai mot thuoc tnh.
3) F la tap phu thuoc ham khong d tha
99
Tap phu thuoc ham co ve phai la mot thuoc tnh
Moi tap phu thuoc ham F eu tng ng vi mot tap phu thuoc ham G ma ve phai
cua cac phu thuoc ham trong G ch gom mot thuoc tnh.
V du: cho F = {ABC, BC, ABD} ta suy ra
F {AB, AC ,BC, ABD} = G
Tap phu thuoc ham khong d tha
Noi rang F la tap phu thuoc ham khong d tha neu khong ton tai Fc F sao cho
F F.Ngc lai F la tap phu thuoc ham d tha.
V du: cho F = {ABC, BD, ABD} th F d tha v
F F= {ABC, BD}
Tap phu thuoc ham toi thieu
F c goi la mot tap phu thuoc ham toi thieu neu F thoa ong thi ba ieu kien
sau:
F la tap phu thuoc ham co ve trai khong d tha
F la tap phu thuoc ham co ve phai mot thuoc tnh.
F la tap phu thuoc ham khong d tha
100
V du: Cho lc o quan he Q(A,B,C,D) va tap phu thuoc F nh sau:
F={ABCD, BC, CD}
Hay tnh phu toi thieu cua F.
V du: Cho lc o quan he Q(MSCD,MSSV,CD,HG) va tap phu thuoc:
F = { MSCD CD;
CD MSCD;
CD,MSSV HG;
MSCD,HG MSSV;
CD,HG MSSV;
MSCD,MSSV HG}
Hay tm phu toi thieu cua F
Khoa cua Q:
Q(A
1
,A
2
,,A
n
)la lc o quan he.
Q
+
la tap thuoc tnh cua Q.
F la tap phu thuoc ham tren Q.
K la tap con cua Q
+
.
Noi rang K la mot khoa cua Q neu:
K
+
= Q
+
va
Khong ton tai K'c K sao cho K
+
= Q
+

Tap thuoc tnh S c goi la sieu khoa neu S _ K
Thuoc tnh A c goi la thuoc tnh khoa neu AeK vi K la khoa bat ky cua Q. Ngc lai A c
goi la thuoc tnh khong khoa.
101
V du: Cho Q(A,B,C,D,E,G,H,I)
F={ACB; BI ACD; ABCD; HI; ACEBCG; CGAE}
Tm mot khoa cua Q.
Thuat toan tm mot khoa cua mot lc o quan he Q
Bc 1: gan K = Q
+
Bc 2: A la mot thuoc tnh cua K, at K=K A. Neu K
+
=Q
+
th gan
K = K' thc hien lai bc 2
V du: Tm tat ca cac khoa cua lc o quan he va tap phu thuoc ham nh sau: Q(C,S,Z);
F={CSZ; ZC}
X
i
X
i
+ Sieu khoa khoa
C C
S S
CS CSZ CS CS
Z ZC
CZ CZ
SZ SZC SZ SZ
CSZ CSZ CSZ
102
He qua: Neu K la khoa cua Q th TN _ K va TD K = C
Chng minh TN _ K
Theo he qua 2 cua thuat toan tm bao ong ta co:
K
+
_ KTDTG
Ta chng minh A e TN A e K. That vay:
Neu AeK K
+
_KTDTG _ Q
+
-A K khong la khoa
mau thuan
Chng minh TD K = C
Gia s co thuoc tnh A e TD K ta se dan en ieu mau thuan.
That vay:
Theo he qua 1 cua thuat toan tm bao ong th K
+
=(K-A)
+
A
A e TD co X la ve trai cua mot phu thuoc ham trong F sao cho
XA (1) va AeX X_K
+
=(K-A)
+
A v AeX
X_(K-A)
+
(K-A) X (2)
(1) va (2) cho (K-A)A Ae(K-A)
+
(K-A)
+
A =
(K-A)
+
K
+
=(K-A)
+
mau thuan vi ieu K la khoa.
103
Tm tat ca cac khoa cua lc o quan he va tap phu thuoc
ham nh sau: Q(C,S,Z); F={CSZ; ZC}
Giai:
TN = {S}; TG = {C,Z}
Goi X
i
la cac tap con cua tap TG:
X
i
(TN X
i
) (TN X
i
)
+

Sieu khoa khoa
| S S
C SC Q
+
SC SC
Z SZ Q
+
SZ SZ
CZ SCZ Q
+
SCZ
Let a relation R have three candidate keys A, B, and (C,D). Which of the
following must not be correct.
a) AB
b) BA
c) AC
d) CAB
Ghi chu:
Tap thuoc tnh nguon (TN) cha tat
ca cac thuoc tnh co xuat hien
ve trai va khong xuat hien ve
phai cua cac phu thuoc ham va
cac thuoc tnh khong xuat hien
ca ve trai lan ve phai cua cac
phu thuoc ham.
Tap thuoc tnh trung gian (TG) cha
tat ca cac thuoc tnh xuat hien
ca ve trai lan ve phai cua cac
phu thuoc ham.
Noi rang K la mot khoa cua Q neu:
K
+
= Q
+
va
Khong ton tai K'c K sao cho K
+
= Q
+

Consider a relation R(A,B,C,D,E) with the
following functional dependencies:
ABC DE and D AB.
The number of superkeys of R is:
a) 2
b) 7
c) 10
d) 12
104
MASV
HOVATEN KHOA TENMONHOC DIEMTHI
99023
99023
99023
NGUYEN THI THU
NGUYEN THI THU
NGUYEN THI THU
CNTT
CNTT
CNTT
KY THUAT LAP TRINH
TOAN ROI RAC
CO SO DU LIEU
6.0
8.0
4.0
99030 LE VAN THANH DIEN TU VI XULY 4..0
MASV
HOVATEN KHOA TENMONHOC DIEMTHI
99023 NGUYEN THI THU CNTT KY THUAT LAP TRINH 6.0
99023 NGUYEN THI THU CNTT TOAN ROI RAC 8.0
99023 NGUYEN THI THU CNTT CO SO DU LIEU 4.0
99030 LE VAN THANH DIEN TU VI XULY 4..0
Quan he khong at chuan 1:
Quan he at chuan 1:
For a table to be in first normal form, it must
a) have a composite primary key.
b) have at least one foreign key.
c) not have a composite primary key.
d) not have any foreign keys
e) not have any repeating groups.
MASV, HOVATEN:repeating group; TENMONHOC, DIEMTHI: multi values
105
nh ngha:
Mot lc o quan he Q dang chuan 2 neu Q at chuan 1 va moi thuoc tnh khong khoa cua
Q eu phu thuoc ay u vao khoa.
Thuat toan kiem tra dang chuan 2
Vao: lc o quan he Q, tap phu thuoc ham F
Ra: khang nh Q at chuan 2 hay khong at chuan 2.
Bc 1: Tm tat ca khoa cua Q
Bc 2: Vi moi khoa K, tm bao ong cua tat ca tap con that s S cua K.
Bc 3: Neu co bao ong S
+
cha thuoc tnh khong khoa th Q khong at chuan 2 . Ngc lai
Q at chuan 2
V du:
Cho lc o quan he Q(A,B,C,D) va tap phu thuoc ham
F={ABC; BD; BCA}. Hoi Q co at chuan 2 khong?
V du 3: Quan he sau at chuan 2.
Q(G,M,V,N,H,P) F={GM;GN;GH;GP;MV;NHPM}
He qua:
Neu Q at chuan 1 va tap thuoc tnh khong khoa cua Q bang rong th Q at chuan 2
Neu tat ca khoa cua quan he ch gom mot thuoc tnh th quan he o t nhat at chuan 2.
106
Thuoc tnh phu thuoc bac cau
Q la lc o quan he, X,Y la hai tap con cua Q
+
, A la mot thuoc tnh.
Noi rang A phu thuoc bac cau vao X neu ca ba ieu sau thoa:
X Y,Y A
X khong phu thuoc ham vao Y
A e XY
nh ngha 1:
Lc o quan he Q dang chuan 3 neu moi phu thuoc ham X A e F
+
vi
A e X eu co X la sieu khoa hay A la thuoc tnh khoa
nh ngha 2:
Lc o quan he Q dang chuan 3 neu moi thuoc tnh khong khoa cua Q eu khong phu thuoc
bac cau vao mot khoa bat ky cua Q
T nh ngha 1 khong co phu thuoc bac cau vao mot khoa bat ky cua Q.
Gia s co phu thuoc bac cau vao khoa ngha la co K Y,Y A,Y K va
A e KY. Y A la mot phu thuoc ham nen theo nh ngha 1 co hai trng hp xay ra cho Y:
Y la sieu khoa YK ieu nay mau thuan vi Y K.
Y khong la sieu khoa A la thuoc tnh khoa mau thuan vi gia thiet AeKY
107
He qua 1: Neu Q at chuan 3 th Q at chuan 2
He qua 2: Neu Q khong co thuoc tnh khong khoa th Q at chuan 3.
nh ly:
Q la lc o quan he
F la tap cac phu thuoc ham co ve phai mot thuoc tnh.
Q at chuan 3 neu va ch neu moi pth XAeF vi AeX eu co
X la sieu khoa
hay A la thuoc tnh khoa
Thuat toan kiem tra dang chuan 3
Vao: lc o quan he Q, tap phu thuoc ham F
Ra: khang nh Q at chuan 3 hay khong at chuan 3.
Bc 1: Tm tat ca khoa cua Q
Bc 2: T F tao tap phu thuoc ham tng ng F
1tt
co ve phai mot thuoc tnh.
Bc 3: Neu moi phu thuoc ham X A e F
1tt
vi AeX eu co X la sieu khoa hoac A la
thuoc tnh khoa th Q at chuan 3 ngc lai Q khong at chuan 3
V du 5: Cho lc o quan he Q(A,B,C,D) F={ABC; DB; CABD}. Hoi Q co
at chuan 3 khong?
108
Dang Chuan BC (Boyce-Codd Normal Form)
Mot quan he Q dang chuan BC neu moi phu thuoc ham XA e F
+
vi AeX eu co X la
sieu khoa.
He qua 1: Neu Q at chuan BC th Q at chuan 3
He qua 2: Moi lc o co hai thuoc tnh eu chuan BC
nh ly:
Q la lc o quan he
F la tap cac phu thuoc ham co ve phai mot thuoc tnh.
Q at chuan BC neu va ch neu moi phu thuoc ham XAeF vi AeX eu co X la sieu khoa
Chng minh:
Q at dang chuan BC theo nh ngha ta suy ra moi phu thuoc ham XAeF vi AeX co X la
sieu khoa.
Ngc lai ta phai chng minh neu moi phu thuoc ham XAeF vi AeX co X la sieu khoa th
moi phu thuoc ham XAeF
+
vi AeX cung co X la sieu khoa. That vay, do XA khong la
phu thuoc ham hien nhien nen phai co ZBeF sao cho X_Z (Z la sieu khoa) X la sieu
khoa.
V du 7: Q(A,B,C,D,E,I) F={ACDEBI;CEAD}. Hoi Q co at chuan BC
khong?
109
Thuat toan kiem tra dang chuan cua mot lc o quan he.
Vao: lc o quan he Q, tap phu thuoc ham F
Ra: khang nh Q at chuan g?
Bc 1:Tm tat ca khoa cua Q
Bc 2:Kiem tra chuan BC neu ung th Q at chuan BC, ket thuc
thuat toan ngc lai qua bc 3
Bc 3:Kiem tra chuan 3 neu ung th Q at chuan 3, ket thuc thuat toan
ngc lai qua bc 4
Bc 4: Kiem tra chuan 2 neu ung th Q at chuan 2, ket thuc thuat toan
ngc lai Q at chuan 1
nh ngha: Dang chuan cua mot lc o c s d lieu la dang chuan thap
nhat trong cac dang chuan cua cac lc o quan he con.
110
Chuan 2:
Mot lc o quan he Q dang chuan 2 neu Q at chuan 1 va moi thuoc tnh khong khoa cua
Q eu phu thuoc ay u vao khoa.
He qua:
Neu Q at chuan 1 va tap thuoc tnh khong khoa cua Q bang rong th Q at chuan 2
Neu tat ca khoa cua quan he ch gom mot thuoc tnh th quan he o t nhat at chuan 2.
Chuan 3:
** Lc o quan he Q dang chuan 3 neu moi thuoc tnh khong khoa cua Q eu khong phu
thuoc bac cau vao mot khoa bat ky cua Q
** Q at chuan 3 neu va ch neu moi pth XAeF vi AeX eu co X la sieu khoa hay A la
thuoc tnh khoa
**Neu Q khong co thuoc tnh khong khoa th Q at chuan 3.
Chuan BC:
** Q at chuan BC neu va ch neu moi phu thuoc ham XAeF vi AeX eu co X la sieu
khoa
**Q at chuan 3 va khoa cua Q ch gom mot thuoc tnh th Q at chuan BC
111
Consider the relation student(sno, sname, cname, cno) where (sno,
cno) or (sname, cname) are candidate keys. There are
functional dependencies within the keys.
The highest normal form whose requirements this relation satisfies
is:
a) 1NF
b) 2NF
c) 3NF
d) BCNF
The following table and functional dependencies
exhibits what type of dependency?
Table(A, B, C)
A C
A B
B C
a) Partial Dependence
b) Transitive Dependence
c) Full Dependence
d) A and B
e) None of the above
Consider the following functional dependencies
a,bc,d
a,c b,d
e,f,g h,i
f,g j
g,h i
e,g,h f,j
p,q r,s
s t
q u
Which of the following relational schemas might be the result of
normalising R(a,b,c,d)?
a. The schema R(a,b,c,d)
b. The schema R1(a,b) R2(a,c) R3(a,d)
c. The schema R1(a,b) R2(b,c) R3(c,d)
d. The schema R1(a,b,c) R2(a,b,d)
e. The schema R1(a,b) R2(a,c) R3(b,d)
112
Phep tach ket noi bao toan thong tin (decomposition with lossless join)
Cho lc o quan he Q(TENNCC,DIACHI,SANPHAM,DONGIA) co quan he la r
r
TENNCC DIACHI SANPHAM DONGIA
Hung 12 Nguyen Kiem Gach ong 200
Hung 12 Nguyen Kiem Gach the 250
Hung 40 Nguyen Oanh Gach ong 200
r
2
= r.Q
2
+
r
1
= r.Q
1
+
TENNCC DIACHI TENNCC SANPHAM DONGIA
Hung 12 Nguyen Kiem Hung Gach ong 200
Hung 40 Nguyen Oanh Hung Gach the 250
r = r
1
|><|r
2

TENNCC DIACHI SANPHAM DONGIA
Hung 12 Nguyen Kiem Gach ong 200
Hung 12 Nguyen Kiem Gach the 250
Hung 40 Nguyen Oanh Gach ong 200
Hung 40 Nguyen Oanh Gach the 250
Neu Q la mot lc o quan he, Q
1
,Q
2

la hai lc o quan he con co
Q
1
+
Q
2
+
= X
Q
1
+
Q
2
+
= Q
+
X Q
2
+

Th r = r.Q
1
|><|r.Q
2
If the follwing functional dependencies
AB;BC hold for database schema R(AB)
and S(BC) then the join of R and S will be
a) non lossless c) lossy
b) none of above d) lossless
113
nh ngha phep tach Q thanh 2 lc o con
Q la lc o quan he, Q
1
, Q
2
hai lc o con co:
Q
1
+
Q
2
+
= X va Q
1
+
Q
2
+
= Q
+
Noi rang lc o quan he Q c tach thanh hai lc o con Q
1
, Q
2
theo phep tach (Q
1
,Q
2
)
la phep tach ket noi khong mat (hay phep tach bao toan thong tin) neu vi r la quan he bat ky
cua Q ta co:
r = r.Q
1
|><|r.Q
2
Tc la r c tao nen t phep ket noi t nhien cua cac hnh chieu cua no tren cac Q
1
,Q
2
.
Tnh chat
Neu Q la mot lc o quan he, Q
1
,Q
2
la hai lc o quan he con co
Q
1
+
Q
2
+
= X
Q
1
+
Q
2
+
= Q
+
X Q
2
+

Th r = r.Q
1
|><|r.Q
2
V du: cho Q(S,A,I,P), Q
1
=(S,A) , Q
2
=(S,I,P) F={SA,SIP}. Hoi viec
tach Q thanh Q
1
va Q
2
co gay ra mat mat thong tin khong?
Ap dung tnh chat tren, ta co
Q
1
+
Q
2
+
= S
Q
1
+
Q
2
+
= SAIP = Q
+
S SA = Q
1
+

r = r.Q
1
|><|r.Q
2
. phep tach ket noi bao toan thong tin.
114
Q la mot lc o quan he, F la tap phu thuoc ham. Q c
tach thanh cac lc o con Q1, Q2, Q3...,Qn theo tng bc
ma moi bc mot lc o c tach thanh hai lc o
con va thoa man ieu kien cua tnh chat bao toan thong tin
th vi r la quan he bat ky cua Q ta luon co:
r = r.Q
1
|><|r.Q
2
... |><|r.Q
n

Q
Q
1
Q
12
Q
21
Q
2
Q
3
Q
4
S A I P
SA a
1
a
2
b
1
b
2

SIP a
1
b
3
a
3
a
4

S A I P
SA a
1
a
2
b
1
b
2

SIP a
1
a
2
a
3
a
4

Vi Q(S,A,I,P) Q
1
(SA) Q
2
(SIP) F = {S A,SI P}
Hoi Q c tach thanh Q
1
va Q
2
co bao toan thong tin khong?
Bc 1: lap bang 3 dong 5 cot Bc 2:lam bang theo phu thuoc ham
phep tach bao toan thong tin
115
Phep tach bao toan phu thuoc ham
Q(TENNCC,DIACHI,QUAN)
F={TENNCC->DIACHI; DIACHI->QUAN; QUAN->TENNCC}
Q
1
(TENNCC,DIACHI) Q
2
(DIACHI,QUAN)
r
TENNCC DIACHI QUAN
Hung 12 Nguyen Kiem
Go vap
Hong 12 Nguyen Hue Quan 1
Hanh 11 Nguyen Van Troi Phu Nhuan
F
1
={TENNCC->DIACHI,
DIACHI->TENNCC}
r
1
=r.Q
1
+
F
2
={DIACHI->QUAN,
QUAN->DIACHI}
r
2
=r.Q
2
+
TENNCC DIACHI DIACHI QUAN
Hung 12 Nguyen Kiem 12 Nguyen Kiem Go vap
Hong 12 Nguyen Hue 12 Nguyen Hue Quan 1
Hanh 11 Nguyen Van Troi 11 Nguyen Van Troi Phu Nhuan
116
nh ngha phep phan ra bao toan phu thuoc ham:
HQ
i
(F)=F
i
+
={ XY|X Y e F
+
va XY _ Q
i
}
Ta noi phan ra bao toan tap phu thuoc ham F neu
F HQ
i
(F) F
+
= ( HQ
i
(F))
+
vi i=1..k
V du:
Q(A,B,C) F={A->B; B->C; C->A}
=(Q
1
,Q
2
) Q
1
(A,B) Q
2
(B,C)
F
+
AB AABC BBC CAC ABBC ACABC
AAB BA BABC CBC ABABC BCA
AC BAB CA CABC ACB BCAB
AAC BC CB ABABC ACAB BCAC
ABC BAC CAB ABC ACBC BCABC
HQ
1
(F)= F
1
+
={AB,AAB,BA,BAB}{AB,BA}=F
1

HQ
2
(F)= F
2
+
={BC,BBC,CB,CBC}{BC,CB}=F
2
e tnh F
1
+
,F
1
ta khong can tnh F
+
ma ch tnh bao ong cua tat ca tap con cua Q
1
+
roi
suy ra F
1
+
,F
1

Tng t cho F
2
+
,F
2
117
H
Q1
(F)={PTHHN} H
Q2
(F)={ZC, ZZC} F={CSZ,ZC}
Q
1
(S Z) Q
2
(C Z) Q (C S Z)
s
1
z
1
c
1
z
1
c
1
s
1
z
1

s
1
z
2
c
1
z
2
c
1
s
1
z
2

Y ngha phan ra bao toan phu thuoc ham
Q(C,S,Z) F={CS->Z; Z->C}
=(Q
1
,Q
2
) Q
1
(S,Z) Q
2
(C,Z)
Thuat toan kiem tra phep phan ra bao toan phu thuoc ham:
Vao: Q(A,B,C),F={AB,BC,CA},Q
1
(A,B) va Q
2
(B,C)
Hien nhien G = HQ
1
(F)HQ
2
(F) _ {AB,BC}
Ta xac nh CA co thuoc (HQ
1
(F) HQ
2
(F))
+
.Bang cach tnh bao ong cua C
G
+
Bc 1: Z=C
Bc 2: gan Z= Z((ZQ
1
+
)
+
Q
1
+
): Z=C(CAB)=C
Bc 1 va 2 co Z khong thay oi, ta sang lc o Q
2
va tnh tiep Z
Bc 3: gan Z= Z((ZQ
2
+
)
+
Q
2
+
): Z=C(ABCBC)=BC
Zthay oi tnh tiep Zbat au t lc o Q
1
Bc 4: gan Z= Z((ZQ
1
+
)
+
Q
1
+
): Z=BC(ABCAB)=ABC
do Z=Q
+
Z se khong bao gi thay oi.
vay C
G
+
=ABC CAe(HQ
1
(F)HQ
2
(F))
+
phep phan ra bao toan pth.
118
V du: cho Q(S,D,I,M) F={SID;SDM} hay phan ra Q thanh cac lc o con at
chuan BC bao toan thong tin.
Giai:
TN={SI} TG={D}
X
i
TNX
i
(TNX
i
)
+

Sieu khoa Khoa
C SI SDIM SI SI
D SID SDIM SID
F={SI->D;SD->M}
Q(S,D,I,M)
K = SI
F
1
={SD->M}
Q
1
(SDM)
K
1
= SD
F
2
={SI->D}
Q
2
(SDI)
K
2
= SI
S
+
=S D
+
=D M
+
=M S
+
=S D
+
=D I
+
=I
SD
+
=SDM SM
+
=SM SD
+
=SDM SI
+
=SDIM
DM
+
=DM DI
+
=DI
SDM
+
=SDM SDI
+
=SDIM
F
1
+
=HQ
1
(F)={SDM,SDSM,SDDM,SDSDM}{SDM}= F
1

F
2
+
=HQ
2
(F)={SID,SISD,SIDI,SISDI}{SID}= F
2
119
Tnh chat: Theo thuat toan tren, khi phan ra Q thanh Q
1
va Q
2
th tap khoa S
Q
cua Q luon luon
bang vi tap khoa S
Q2
cua Q
2
.
Chng minh
That vay, K la mot khoa cua Q K la mot sieu khoa cua Q
2
. Gia s co Kc K va K
la khoa cua Q
2
K(Q
+
-Y) ma XY KQ
+
. ieu nay mau thuan vi K la
khoa cua Q K la khoa cua Q
2
. Ngc lai cung ung.
F={C->T;HR->C;HT->R;CS->G;HS->R}
Q(CTHRSG)
K = HS
F
1
={C->T}
Q
1
(CT)
K
1
= C
F
12
={HR->C;CS->G;HS->R;...}
Q
12
(CHRSG)
K
12
= HS
F
2
={HR->C;CH->R}
Q
2
(CHR)
K
21
=HR; K
22
= CH
F
3
={HS->RG}
Q
3
(HRSG)
K
3
= HS
Tnh F
1
, K
1
Tnh F
2
,K
Tnh F
3
,K
3
120
F={C->T;HR->C;HT->R;CS->G;HS->R}
Q(CTHRSG)
K = HS
F
1
={C->T}
Q
1
(CT)
K
1
= C
F
12
={HR->C;CS->G;HS->R;...}
Q
12
(CHRSG)
K
12
= HS
F
2
={HR->C;CH->R}
Q
2
(CHR)
K
21
=HR; K
22
= CH
F
3
={HS->RG}
Q
3
(HRSG)
K
3
= HS
Tnh F
1
, K
1
Tnh F
2
,K
Tnh F
3
,K
3
Consider the following functional dependencies:
a,b c,d
e c
b e,f
Given the same functional dependencies as shown above, which option
shows the relations normalised to third normal form of: R(a, b, c, d, e, f)
a. R(a,b,c,d,e,f)
R(e,c)
R(b,e,f)
c. R(a,b,c,d)
R(c,e)
R(b,e,f)
b. R(a,b,c,d)
R(c,e)
R(e,f,b)
d. R(a,b,d)
R(e,c)
R(b,e,f)
e. R(a,b,c,d,e,f)
121
Bo e: Neu Q khong dang chuan BC th co thuoc tnh A,B thuoc Q
+
sao cho (Q
+
-AB)A
Chng minh:
Q khong dang chuan BC co XA sao cho X khong la sieu khoa co thuoc tnh B e
XA (Q
+
-AB) _ X (Q
+
-AB)A
Nhan xet:
Mot lc o Q dang chuan BC van co the co AB sao cho (Q
+
-AB)A
Mot lc o Q khong co AB sao cho (Q
+
-AB)A th Q dang chuan BC

V du: Cho quan he Q(B,O,S,Q,I,D) va tap phu thuoc ham F
F = { S D,
I B
IS Q
B O}
Hay phan ra Q thanh cac lc o con at dang chuan BC va bao toan thong tin.

122
Giai
***at Z=Q
+
=BOSQID
Thc hien thuat toan chi tiet
Y= BOSQID
Chon BO:(SQID)
+
B
Giam O ta c Y=BSQID
Chon BS:(QID)
+
B
Giam S ta c Y=BQID
Chon BQ:(ID)
+
B
Giam Q ta c Y=BID
Chon BD: I
+
B
Giam D ta c Y= BI
Q
1
=(BI) va F
1
={IB}
***Giam B ta c Z=OSQID
at Y=OSQID
Chon OQ: (SID)
+
Q;
Giam O ta c Y= SQID
chon QD: (SI)
+
D
giam Q ta c Y= SID
chon ID: S
+
D;
giam I ta c Y= SD
Q
2
=(SD) va F
2
={SD}
*** Giam D ta c Z= OSQI
at Y=OSQI
chon OQ: (SI)
+
Q
giam O ta c Y= SQI
Q
3
=(SQI) va F
3
={SIQ}
*** Giam Q ta c Z= OSI
at Y=OSI
Chon OS: I
+
=IBO O
giam S ta c Y= OI
Q
4
=(OI) va F
4
={IO}
*** Giam O ta c Z= SI
Q
5
=(SI)va F
5
={PTHHN}
Loai Q
5
v Q
5
+
_ Q
3
+
Vay ket qua phan ra la:
1:Q
1
(BI) F
1
={IB}
2:Q
2
(SD) F
2
={SD}
3:Q
3
(SQI) F
3
={SIQ}
4:Q
4
(OI) F
4
={IO}

123
Tnh chat
Khong co thuat toan phan ra lc o Q thanh cac lc o con dang chuan BC va bao
toan thong tin va bao toan phu thuoc ham.
Van co lc o Q c phan ra thanh cac lc o con dang chuan BC va bao toan thong
tin va bao toan phu thuoc ham.
V du: cho lc o Q(CSZ) co F={CSZ,ZC}. Q khong the phan ra thanh cac lc o
con dang chuan BC va bao toan thong tin va bao toan phu thuoc ham. That vay:
Tat ca khoa cua Q la:






Vay Q at dang chuan 3 nhng khong dang chuan BC v co ZC co ve trai khong la sieu
khoa. Nhng neu ta phan ra Q thanh cac lc o con co t hn 3 thuoc tnh th phu thuoc
CSZ khong suy ra c t cac phu thuoc hnh chieu.
Xi TNXi (TNXi)
+

sieu khoa khoa
C S S
Z SZ SZC SZ SZ
C SC SZC SC SC
ZC SZC SZC SZC
124
Thuat Toan phan ra Q, F thanh dang chuan 3, bao toan thong tin, bao toan phu thuoc ham
Tm phu toi thieu Ftt cua F
Neu co nhng thuoc tnh cua Q khong nam trong mot phu thuoc nao cua Ftt - du ve
phai hay ve trai cua F th chung tao thanh mot lc o.
Neu co mot phu thuoc ham nao cua Ftt ma lien quan en tat ca cac thuoc tnh cua Q th
ket qua phan ra chnh la Q ( Q khong the phan ra)
C moi phu thuoc ham X A e Ftt th XA la mot lc o can tm
Neu co mot lc o con cha khoa K cua Q th ket thuc thuat toan .Ngc lai tao mot
lc o con K
V du: cho lc o Q(CTHRSG),F={CT,HRC,THR,CSG,HSR}.Hay phan
ra Q thanh cac lc o con at dang chuan 3 va bao toan thong tin va bao toan phu thuoc
ham.
Gai:
F=Ftt={CT,HRC,THR,CSG,HSR} la phu toi thieu.
Ap dung thuat toan tren Q c phan ra thanh cac lc o con
Q
1
(CT)
Q
2
(HRC)
Q
3
(THR)
Q
4
(CSG)
Q
5
(HSR)
Khoa cua Q la HSQ
1
,Q
2
,Q
3
,Q
4
,Q
5
chnh la ket qua phan ra
125
nh ly: Thuat toan tren tao ra mot phan ra dang chuan 3 va bao toan thong tin va bao
toan phu thuoc ham
Chng minh:
Ta chng minh moi lc o con dang chuan 3. That vay:
Theo thuat toan th moi lc o con Q
i
co dang YB vi YB Y la sieu khoa. Gia s
trong Q
i
co phu thuoc ham XA co ve trai khong la sieu khoa va ve phai khong la thuoc tnh
khoa. Ta phan lam hai trng hp:
Trng hp 1: A=B XB X c Y YB la phu thuoc co ve trai d tha,
ieu nay trai vi YB la phu thuoc ham trong phu toi thieu.
Trng hp 2: A=B AeY (1). Goi K la khoa cua Q
i
K _ Y (2). A la thuoc tnh
khong khoa nen A e K (3).(1)(2)(3) K c Y (4).K la khoa nen KB YB la phu
thuoc ham co ve trai d tha. ieu nay trai vi ieu phu thuoc ham YB la phu thuoc ham
cua phu toi thieu Ftt
Ta chng minh phep phan ra bao toan phu thuoc ham. That vay:
Hien nhien F
tt
_ G = HQ
i
(F
tt
) F
tt
+
_ G
+
(1)
Hn na F
tt
+
_ G = HQ
i
(F
tt
) F
tt
++
_ G
+
F
tt
+
_ G
+
(2)
(1)va (2) F
tt
+
= G
+
Ta chng minh phep phan ra bao toan thong tin. That vay:
Lap bang kiem tra bao toan thong tin. Ta lan lt ong nhat cac gia tr theo cac phu thuoc ham
c phat hien do thuat toan tm bao ong (co ve trai la tap con cua Q
i
+
cha khoa). Do Q
i
+

cha khoa nen hang cua lc o Q
i
se cha toan a la ieu phai chng minh
126
C T H R S G
Q
1
a
1
a
2

Q
2
a
1
a
3
a
4

Q
3
a
2
a
3
a
4

Q
4
a
1
a
5
a
6

Q
5
a
3
a
4
a
5

Tm bao ong cua X={HSR} _ K; F={CT,HRC,THR,CSG,HSR}
X
0
=HSR
X
1
=HSRC do HRC ong nhat cac gia tr theo phu thuoc ham nay.
X
2
=HSRCT do CT ong nhat cac gia tr theo phu thuoc ham nay.
X
3
=HSRCTG do CSG ong nhat cac gia tr theo phu thuoc ham nay.
Do X
+
=Q
+
nen se co mot hang cha toan gia tr a
C T H R S G
Q
1
a
1
a
2

Q
2
a
1
a
2
a
3
a
4

Q
3
a
1
a
2
a
3
a
4

Q
4
a
1
a
2
a
5
a
6

Q
5
a
1
a
2
a
3
a
4
a
5
a
6

127
Which charater function would you use a specified portion of a character string?
a) Length c) substr
b) Concat d) initcap
A modification anomaly occurs when:
a) Relations are normalized
b) none of them
c) functional dependencies must be modified
d) Undesirable consequences result from changing data in a table
e) All of them
The consequence of the reflexivity rule is that:
a) A set of attributes determines an attribute from that set
b) A superkey determines all other attribute
c) Every attribute determines itself
d) A candidate key determines all other attributes
Indexing is a feature applied to a database:
a) All of the others
b) To place permit restriction on access
c) To assist in good user respone times
d) To improve access times and to enforce some integrity conrols
128
Relation J is a projection of relation G. Which of the following statements must
be true in all cases where relation J is different from relation G?
a) The arity of J is less than the arity of G
b) The cardinality of J is less than the cardinality of G
c) None of the others
d) The arity of J is greater than the arlity of G
e) The cardinality of J is greater than the cardinality of G
The resulting relation from an equijoin operation which is none emty, will
a) Have the same number of attribute as a natural join between the same operand
relations
b) None of them
c) Always have one or more pairs of attributes in each tuple that have identical values
d) have fewer attributes than the sum of the attributes of the two eperand relations
The following is an example of a composide attribute
a) list of skills b) Product code
c) Address d) Employee
e) Total price
Which of the following best describes the source of the function dependencies.
a) The mathematical formalization of constraints from the mini-world
b) The cardinalities of foreign key constraints
c) The intuition of the schema designer
d) The value of a mathematical function on the keys of a relation
129
A third normal form is violation can occur for which the following tables
a) A table which a single field primary and two non key attributes and A table with
a composite primary key and two non key attribute
b) A table with a composite primary key and two non key attributes
c) A table with a composite primary key and one non key attributes
d) A table with a single field primary key and two non key attributes
e) All of the others
The following is an example of a multivalued attribute
a) Address
b) Product code
Database technologry offers
a) Multi user access (in advanced DBMSs)
c) Imporved progrmmer and user productivity
b) Data security facilities
c) List of skill
d) Total price
e) Employee
d) All of the others
e) High integrity control over data
A functional dependency violates 3NF if it:
a) Has a key on its lefthandside, a prime attrite on its righthand-side
b) Has a part of a candidate key on its lefthand side
c) Has a super key on its left-hand-side,but there is no prime attribute on the right-hand-side
d) Has a prime attribute on its righthand side
e) Does not have a candidate key on its lefthand side
f) Neither has a super key on its lefthand side, nor a prime attribute on the righthand side
130
Going from first to second normal form removes
a) Transitive dependencies b) Functional dependencies
c) Full functional dependencies d) Data redundancy
This operator can only operate on relations with the same schema
a) cartesian product b) All of them
c) Intersection d) Equi-join
Database-Processing programs
a) Call the DBMS to access the stored data
b) Require at least one dedicated workstation
c) Can not be used by more than one person
d) Present problems with storage space
Which of the following best describes why we study relational algebra
a) It allows for more efficient operator than SQL
b) It provides a form mathematical foundation
c) It is more concise than drawing query trees
d) It allows the expression of operations not possible in conventional programming
language
131
If we use the algorithm for producing a lossless decomposition into 3rd normal
form with the relation schema of X(K, A, C) and the set of FDS as (KAC, CK)
then the algorithm would produce as output
a) A relation schema with (K, A, C)
b) A relation schema with (X, A, C)
c) A relation schema with (K, A, X)
d) A relation schema with (C, K) and a relation schema with (C)
e) A relation schema with (C, K) and a relation schema with (K, A)
f) A relation schema with (K) and a relation schema with (A) and a relation schema
with (C)
Which of the following is not a relations ?
a) Y (D, E, F) b) W (A) c) X (B, C) d) Z (J, K(L, M) )
If ML JQ is one of the functional dependencies, which hold for relation
schema F (M,L,J,Q) then
a) No two tuples in F can have the same values for M and L
b) ML is a key for F
c) All of above
d) ML is a primary key for F
132
The term row (or record) used in relational database
a) Define the entityrelationship model used.
b) Refers to the primary key nomination
c) Refers to the method of matrix organisation of data
d) Refers to the denormalised data
Normalization is a process associated with
a) Avoiding update anomalies
b) Naming of primary and foreign keys in a database
c) Database concurrency procedures
d) Ensuring ease of access to the database
Given two relation R (A,B,C) and S (A, B, C) which of the following is always true
a) R S yields the same result as S R
b) R INTERSECT S yields the same result as S INTERSECT R
c) PROJECT(R) yields the same result as PROJECT(S)
d) none of the above
133
Which of the following is correct
a) All FDs are aretoare relationships
b) All FDs are many toare relationships
c) None of them are true
d) All FDs are manytomany relationships
The following table and the functional dependencies exhibits whay type of
dependency table (Q,O,I)
Q I
Q O
O I
a) Transitive dependence b) Q and O
c) Full dependence d) partial dependence
e) None of the others
A prime attribute is an attribute that
a) a member of a foreign b) is single valued
c) is a member of the primary key d) is a member of all keys
e) is minimal f) is a member of any candidate key
134
Which of the following is (are) a capability (capabilities) that should be provided
by a database management system ?
a) all of the others b) restrict unauthorized access
c) represent relationships among data d) cantrol redundancy
Which of the follwing is not correct?
a) SQL may be used for data definition as well as data retrieval
b) SQL SELECT operation may be used for data retrieval as well as for data
modification
c) SQL may be used to defining base tables as well as view tables
d) SQL data definitions may be used for defining primary keys as well as foreign keys
e) SQL may be invoked either interactively or from an application program
A table is in 3NF if
Each functional dependency has a superkey on its left hand side
Each functional dependency either has a superkey on its left hand side, or has a
prime attribute on right hand side
There are no partial functional dependencies
There is more than one key
There are no transitive functional dependencies
A table in third normal form:
a) Cannot have any repeating groups
b) Has no non-keys fields depending on only a part of the composite primary key
c) Has no transitive dependencies
d) All of the others

135
If a relation schema has 5 attributes and 3 of those attributes make up the only
key, the how many different superkeys does the relation have
a) 5 b) 4 c) 3 d) 2
Table TT (YMBKGFD) and a set of functional dependencies
YM FD
G YMB
BF KD
BKD F
YMD G
Table TT is
2NF b) 3NF c) 1NF
Table A Table B
Col1 Col2 Col3 Col4
I 1 I 1
G 3 W 4
W 4 S 5
C 3
Consider the tables A and B shown above and select the result of
A LEFT OUTER JOIN col1=col3 B

Which of the attributes are extraneous in the functional dependency UIZE,
considering the relational schema R(UIZEOD) and FDs {IO;ZD
If a relation R is decomposed into {R1,R2,...,Rn} and
the decoposition is lossless then
a) none of them
b) The relations R
1
,R
2
,...,R
n
are each in 3
rd
normal
form
c) The relations R
1
,R
2
,...,R
n
can have more tuples than
the original relation
d) The natural join of R1,R2,...,Rn will have the same
number of tuples as the original