You are on page 1of 78

Khoa HTTT-i hc CNTT 1

i hc Cng Ngh Thng Tin


Mn: C S D Liu
Thi lng: 45LT+30TH
Khoa HTTT-i hc CNTT 2
Ni dung
Bi 1: Cc khi nim ca mt h CSDL
Bi 2: Cc m hnh CSDL
Bi 3: M hnh d liu quan h (ca Codd)
Bi 4: Ngn ng i s quan h
Bi 5: Ngn ng SQL
Bi 6: Ngn ng tn t
Bi 7: Rng buc ton vn trong mt CSDL
Bi 8: Ti u ha cu hi bng i s quan h
Khoa HTTT-i hc CNTT 3
Bi 1: Cc khi nim ca mt h
CSDL
Khoa HTTT-i hc CNTT 4
Bi 1: Cc khi nim ca mt
h CSDL
1. Gii thiu
2. H thng tp tin (File System)
3. nh ngha mt CSDL
4. Cc i tng s dng CSDL
5. H qun tr CSDL
6. Cc mc ca mt CSDL
Khoa HTTT-i hc CNTT 5
1. Gii thiu
Ti sao cn phi c mt c s d liu?
Khoa HTTT-i hc CNTT 6
2. H thng tp tin
L tp hp cc tp tin ring l phc v cho mt mc
ch ca n v s dng.
u im:
Trin khai ng dng nhanh
Kh nng p ng nhanh chng, kp thi (v ch phc v
cho mc ch hn hp)
Khuyt im:
Trng lp d liu lng ph, d liu khng nht qun
Chi ph cao
Chia s d liu km
Khoa HTTT-i hc CNTT 7
3. C s d liu (1)
nh ngha:
C s d liu l mt h thng cc thng tin c
cu trc, c lu tr trn cc thit b lu tr
nhm tha mn yu cu khai thc thng tin ng
thi ca nhiu ngi s dng hay nhiu chng
trnh ng dng vi nhng mc ch khc nhau
Khoa HTTT-i hc CNTT 8
3. C s d liu (2)
u im:
Gim trng lp thng tin xung mc thp nht,
m bo tnh nht qun v ton vn d liu.
m bo d liu c truy xut theo nhiu cch
khc nhau.
Kh nng chia s thng tin cho nhiu ngi,
nhiu ng dng khc nhau.
Khoa HTTT-i hc CNTT 9
3. C s d liu
Nhng vn cn gii quyt:
Tnh ch quyn d liu.
Tnh bo mt v quyn khai thc thng tin ca
ngi s dng
Tranh chp d liu
m bo d liu khi c s c
Khoa HTTT-i hc CNTT 10
4. Cc i tng s dng
Ngi s dng CSDL khng chuyn v lnh vc tin
hc v CSDL -> cn cng c h c th khai thc
CSDL khi cn.
Chuyn vin tin hc xy dng cc ng dng phc
v cho cc mc ch qun l
Qun tr CSDL: t chc CSDL, bo mt, cp quyn,
sao lu, phc hi d liu, gii quyt cc tranh chp
d liu
Khoa HTTT-i hc CNTT 11
5. H qun tr c s d liu (1)
H qun tr c s d liu (DBMS DataBase
Management System) l h thng cc phn mm h
tr tch cc cho cc nh phn tch, thit k v khai
thc CSDL.
Cc DBMS thng dng: Visual FoxPro, Microsoft
Access, SQL Server, DB2, Oracle hu ht cc
DBMS hin nay u da trn m hnh quan h.
Khoa HTTT-i hc CNTT 12
5. H qun tr c s d liu (2)
Mt DBMS phi c:
Ngn ng giao tip gia ngi s dng v CSDL
T in d liu (Data Dictionary)
C bin php bo mt khi c yu cu
C ch gii quyt tranh chp d liu
C c ch sao lu (backup), phc hi (restore)
m bo tnh c lp gia d liu v chng trnh
Khoa HTTT-i hc CNTT 13
5. H qun tr c s d liu (3)
Ngn ng giao tip:
Ngn ng m t d liu (DDL Data Definition Language):
cho php khai bo cu trc CSDL, cc mi lin h ca d liu,
cc quy nh, rng buc d liu.
Ngn ng thao tc d liu (DML Data Manipulation
Language): cho php thc hin thao tc thm, xa, sa d liu.
Ngn ng truy vn c cu trc (SQL Structured Query
Language): cho php ngi khai thc s dng truy vn
thng tin cn thit.
Ngn ng qun l d liu (DCL Data Control Language) cho
php thay i cu trc bng, khai bo bo mt, cp quyn cho
ngi s dng.
Khoa HTTT-i hc CNTT 14
6. Cc mc biu din 1 CSDL
Mc trong: (mc vt l Physical) l mc lu tr CSDL
(cn gii quyt vn g? D liu g? Lu tr nh th
no? u? Cn cc ch mc g? Truy xut tun t hay
ngu nhin. Dnh cho ngi qun tr v ngi s dng
chuyn mn.
Mc quan nim: (Conception hay Logical) cn phi lu
tr bao nhiu loi d liu? l d liu g? mi quan h
Mc ngoi: ca ngi s dng v cc chng trnh ng
dng
Khoa HTTT-i hc CNTT 15
6. Cc mc biu din 1 CSDL
Cu trc
ngoi 1
Cu trc
ngoi 2
Cu trc
ngoi n
Chng trnh
ng dng n
Mc
quan
nim
hoc
mc
logic
Mc vt l
Cu trc vt l
CSDL
Mi trng
thc th gii
thc
NSD1
NSD2
Khoa HTTT-i hc CNTT 16
Bi 2: Cc m hnh d liu
Khoa HTTT-i hc CNTT 17
Ni dung
1. Gii thiu
2. M hnh d liu mng
3. M hnh thc th mi kt hp
Khoa HTTT-i hc CNTT 18
1. Gii thiu
M hnh d liu l s tru tng ha ca mi
trng thc, biu din d liu mc quan nim.
Gii thiu mt s m hnh nh:
M hnh d liu mng
M hnh d liu phn cp
M hnh d liu thc th mi kt hp
M hnh d liu quan h
M hnh d liu hng i tng
Khoa HTTT-i hc CNTT 19
2. M hnh d liu mng (1)
M hnh d liu mng (Network Data Model) cn
gi tt l m hnh mng hoc m hnh li l m
hnh c biu din bi mt th c hng.
Mu tin (record)
Loi mu tin
Loi lin h (set type)
Bn s
Khoa HTTT-i hc CNTT 20
2. M hnh d liu mng (2)
Mu tin: m t 1 i tng trong th gii thc.
(NV001,Nguyen Lam,Nam,10/10/1970,Dong Nai)
Loi mu tin: l 1 tp cc mu tin c cng tnh
cht. V d: NHANVIEN
K hiu:

Loi lin h: m t s lin kt gia 1 loi mu tin
ch v 1 loi mu tin thnh vin
K hiu:

NHANVIEN
Tham gia
CONGVIEC
Khoa HTTT-i hc CNTT 21
2. M hnh d liu mng (3)
Bn s: ch ra s lng cc mu tin tham gia trong
mi lin h
(1:1) (one-to-one): mi mu tin ca loi mu tin ch kt
hp vi ng 1 mu tin ca loi mu tin thnh vin.
(1:n) (one-to-many): mi mu tin ca loi mu tin ch
kt hp vi 1 hay nhiu mu tin thnh vin.
(n:1) (many-to-one): nhiu mu tin ca loi mu tin ch
kt hp vi ng 1 mu tin ca loi mu tin thnh vin.
(Recursive): mt loi mu tin ch cng c th ng thi
l loi mu tin thnh vin vi chnh n. Loi lin h ny
l quy
Khoa HTTT-i hc CNTT 22
2. M hnh d liu mng (4)
PHONG
CONGVIEC
NHANVIEN
LYLICH
gm c
qun l
trc tip
cng lm
n:1
1:1
1:n
1:n
Khoa HTTT-i hc CNTT 23
2. M hnh d liu mng (5)
M hnh d liu mng:
Tng i n gin
D s dng
Khng thch hp biu din CSDL c quy m ln
Kh nng din t ng ngha km
Khoa HTTT-i hc CNTT 24
2. M hnh d liu mng (6)
Bi tp:
Xy dng m hnh d liu mng cho c s
d liu qun l bn hng trong mt siu th
(gio vin m t hin trng, xc nh cc yu
cu)

Khoa HTTT-i hc CNTT 1
3. M hnh thc th mi kt hp
3.1 Gii thiu
3.2 Loi thc th, thc th
3.3 Thuc tnh ca loi thc th
3.4 Kho ca loi thc th
3.5 Loi mi kt hp, mi kt hp
3.6 Thuc tnh ca loi mi kt hp
3.7 Bn s
3.8 M hnh ER m rng
Khoa HTTT-i hc CNTT 2
3.1 Gii thiu
M hnh thc th mi kt hp (Entity-
Relationship Model vit tc ER) c CHEN
gii thiu nm 1976.
M hnh ER c s dng nhiu trong thit
k d liu mc quan nim.


Khoa HTTT-i hc CNTT 3
3.2 Loi thc th
nh ngha: loi thc th (Entity Type) l
nhng loi i tng hay s vt ca th gii
thc tn ti c th cn c qun l.
V d : HOCVIEN, LOP, MONHOC,
K hiu:
HOCVIEN LOP
Khoa HTTT-i hc CNTT 4
3.2 Thc th (Entity)
nh ngha: thc th l mt th hin ca mt
loi thc th.
V d: Loi thc th l HOCVIEN c cc
thc th:
(HV001, Nguyen Minh, 1/2/1987,Nam)
(HV002, Tran Nam, 13/2/1987, Nam)

Khoa HTTT-i hc CNTT 5
3.3 Thuc tnh ca loi thc th
(Entity Attribute)
nh ngha: thuc tnh l nhng tnh cht
c trng ca loi thc th.
V d: Loi thc th HOCVIEN c cc
thuc tnh: M hc vin, h tn, gii tnh,
ngy sinh, ni sinh
K hiu:
HOCVIEN
Hoten
Gioitinh
Mahv
Ngaysinh
Noisinh
Khoa HTTT-i hc CNTT 6
3.3 Cc loi thuc tnh (1)
n tr (Simple): mi thc th ch c mt gi tr ng vi
mi thuc tnh.
V d: Mahv,Hoten
a hp (Composite): thuc tnh c th c to thnh t
nhiu thnh phn.
V d: DCHI(SONHA,DUONG,PHUONG,QUAN)
hay thuc tnh HOTEN(HO,TENLOT,TEN).
a tr (Multi-valued): thuc tnh c th c nhiu gi tr i
vi mt thc th.
V d: BANGCAP k hiu {BANGCAP}
Khoa HTTT-i hc CNTT 7
3.3 Cc loi thuc tnh (2)
Tm li, cc thuc tnh a hp v a tr c
th lng nhau ty .
V d: thuc tnh BANGCAP ca HOCVIEN l
mt thuc tnh a hp c k hiu bng

{BANGCAP(TRUONGCAP,NAM,KETQUA,
CHUYENNGANH)}
Khoa HTTT-i hc CNTT 8
3.4. Kho ca loi thc th
(entity type key)
Kha ca loi thc th l thuc tnh nhn
din thc th.
Cn c vo gi tr ca kha c th xc nh
duy nht mt thc th.
V d:
Mi hc vin c mt m s duy nht => Kho
ca loi thc th HOCVIEN l M hc vin
Khoa HTTT-i hc CNTT 9
nh ngha: loi mi kt hp l s lin kt
gia hai hay nhiu loi thc th
V d: gia hai loi thc th HOCVIEN v
LOP c loi mi kt hp THUOC
K hiu: bng mt hnh oval hoc hnh thoi
Thuoc
HOCVIEN
LOP
3.5 Loi mi kt hp (1)
(relationship type)
Khoa HTTT-i hc CNTT 10
3.5 Loi mi kt hp (2)
Gia hai loi thc th c th tn ti nhiu
hn mt loi mi kt hp.
V d
Thuc
HOCVIEN LOP
L trng lp
Khoa HTTT-i hc CNTT 11
3.5 S ngi ca loi mi kt hp
(relationship degree)
S ngi ca loi mi kt hp l s loi thc th
tham gia vo loi mi kt hp .
V d 1: Loi mi kt hp Thuc kt hp 2
loi thc th HOCVIEN v LOP nn c s
ngi l 2.
V d 2: Loi mi kt hp Thi kt hp 3 loi
thc th LANTHI,HOCVIEN, MONHOC nn
c s ngi l 3.
Khoa HTTT-i hc CNTT 12
3.5 S ngi ca loi mi kt hp
HOCVIEN LOP
Thuoc
HOCVIEN MONHOC
Thi
LANTHI
Khoa HTTT-i hc CNTT 13
3.6 Thuc tnh ca loi mi kt hp
(relationship type attribute)
Thuc tnh ca loi mi kt hp bao gm cc
thuc tnh kho ca cc loi thc th tham gia vo
loi mi kt hp . Ngoi ra cn c th c thm
nhng thuc tnh b sung khc.
V d: Loi mi kt hp Thi gia ba loi thc th
HOCVIEN, MONHOC v LANTHI c cc thuc
tnh l Mahv,Mamh,Lanthi, ngoi ra cn c thuc
tnh ring l Diem, Ngaythi
Khoa HTTT-i hc CNTT 14
Hocky
Nam
HOCVIEN LOP
Thuoc
GIAOVIEN MONHOC
Day
LOP
Khoa HTTT-i hc CNTT 15
3.7 Bn s
(relationship cardinality)
Loi mi kt hp th hin lin kt gia cc
thc th, mi lin kt c gi l mt nhnh.
nh ngha: bn s ca nhnh l s lng ti
thiu v s lng ti a cc thc th thuc
nhnh tham gia vo loi mi kt hp.
K hiu: (s lng ti thiu, s lng ti a)
V d: Loi thc th HOCVIEN v LOP c
loi mi kt hp Thuoc.
Khoa HTTT-i hc CNTT 16
Thuc
HOCVIEN LOP
(1,1) (1,n)
Khoa HTTT-i hc CNTT 17
Mt hc vin ch hc 1 lp
Thuc
Mt lp th c t nht l 1 hc vin
HOCVIEN LOP
1 hc vin th c th l trng lp hoc khng
L trng lp
! Lp th bao h cng c 1 trng lp
(1,1) (1,n)
(0,1)
(1,1)
Khoa HTTT-i hc CNTT 18
3.7 M hnh ER m rng
3.7.1 Chuyn bit ho / Tng qut ha
3.7.2 Mi kt hp quy
3.7.3 Loi thc th yu
3.7.4 Mi kt hp m rng
Khoa HTTT-i hc CNTT 19
3.7.1 Chuyn bit ha (tng
qut ha)
GiaoVien HocVien
ConNguoi
HocVi
NgayVL
NgayNH
Kha
SoCMND
HoTen
...
Khoa HTTT-i hc CNTT 20
3.7.2 Mi kt hp quy
nh ngha: l loi mi kt hp c to thnh t
cng mt loi thc th (hay mt loi thc th c
loi mi kt hp vi chnh n)
V d: Mi nhn vin c mt ngi qun l trc
tip v ngi qun l cng l mt nhn vin
NHANVIEN QuanLy
(0,n)
(0,1)
Khoa HTTT-i hc CNTT 21
nh ngha:
L loi thc th khng c thuc tnh kha
Phi tham gia trong mt loi mi kt hp xc nh trong
c mt loi thc th ch.
K hiu:


V d: loi thc th THANNHAN l loi thc th
yu c thuc tnh Stt, Hoten, Ngsinh,Quanhe v
tham gia trong loi mi kt hp C vi loi thc th
NHANVIEN.
Thc th
3.7.3 Loi thc th yu
Khoa HTTT-i hc CNTT 22
3.7.3 Loi thc th yu
C
NHANVIEN THANNHAN
(1,n) (1,1)
Khoa HTTT-i hc CNTT 23
3.7.4 Mi kt hp m rng
HOCVIEN LOP
Thuoc
LOP MONHOC
Hoc
GIAOVIEN
Giangday
(1,n) (1,n)
(1,n)
(1,1)
Khoa HTTT-i hc CNTT 24
Bi tp
Xy dng m hnh ER
Xy dng m hnh ER cho CSDL qun l
gio v gm c cc chc nng sau:
Lu tr thng tin: Hc vin, gio vin, mn hc
Xp lp cho hc vin, chn lp trng cho lp
Phn cng ging dy: gio vin dy lp no vi
mn hc g, hc k, nm hc no.
Lu tr kt qu thi: hc vin thi mn hc no,
ln thi th my, im thi bao nhiu.
Khoa HTTT-i hc CNTT 25
Bi 3: M hnh d liu quan h
(Relational Data Model)
Khoa HTTT-i hc CNTT 26
1. Gii thiu
2. Cc khi nim
2.1 Thuc tnh
2.2 Quan h
2.3 B gi tr
2.4 Th hin ca quan h
2.5 Tn t
2.6 Php chiu
2.7 Kha
2.8 Lc quan h v lc CSDL
2.9 Hin thc m hnh ER bng m hnh d liu quan h.
Ni dung
Khoa HTTT-i hc CNTT 27
1. Gii thiu
M hnh D liu Quan h (Relational Data
Model) da trn khi nim quan h.
Quan h l khi nim ton hc da trn nn
tng hnh thc v l thuyt tp hp.
M hnh ny do TS. E. F. Codd a ra nm
1970.
Khoa HTTT-i hc CNTT 28
2.1 Thuc tnh (attribute)
Thuc tnh:
Tn gi: dy k t (gi nh)
Kiu d liu: S, Chui, Thi gian, Lun l, OLE.
Min gi tr: tp gi tr m thuc tnh c th nhn. K
hiu min gi tr ca thuc tnh A l Dom(A).
V d:GIOITINH kiu d liu l Chui,min gi tr
Dom(GIOITINH)=(Nam,Nu)
Ti mt thi im, mt thuc tnh khng c gi tr
hoc cha xc nh c gi tr => gi tr Null
Khoa HTTT-i hc CNTT 29
2.2 Quan h (relation)
nh ngha: quan h l mt tp hu hn cc
thuc tnh.
K hiu:
Trong Q l tn quan h, l tp
cc thuc tnh ca quan h Q
V d:
HOCVIEN (Mahv, Hoten, Ngsinh, Gioitinh, Noisinh, Malop)
LOP (Malop, Tenlop, Siso, Trglop, Khoa)
) ,..., , (
2 1 n
A A A Q
{ }
n
A A A Q ,..., ,
2 1
=
+
Khoa HTTT-i hc CNTT 30
2.3 B (tuple)
nh ngha: B l cc thng tin ca mt i tng
thuc quan h, c gi l mu tin (record), dng.
Quan h l mt bng (table) vi cc ct l cc
thuc tnh v mi dng c gi l b.
Mt b ca quan h l
vi
V d: HOCVIEN(Mahv, Hoten, Ngsinh, Noisinh)
c q=(1003,Nguyen Van Lam, 1/1/1987,Dong Nai)
ngha l hc vin c m s l 1003, h tn l
Nguyen Van Lam, sinh ngy 1/1/1987 Dong Nai
) ,..., , (
2 1 n
A A A Q ) ,..., , (
2 1 n
a a a q =
) (
i i
A Dom a e
Khoa HTTT-i hc CNTT 31
2.4 Th hin ca quan h
(instance)
nh ngha: th hin ca mt quan h l tp hp cc
b gi tr ca quan h ti mt thi im.
K hiu: th hin ca quan h Q l T
Q
V d: T
HOCVIEN
l th hin ca quan h HOCVIEN ti
thi im hin ti gm c cc b nh sau:

HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
Khoa HTTT-i hc CNTT 32
2.5 Tn t
nh ngha: tn t l mt quy tc dng
m t mt quan h.
K hiu: ||Q||
V d: THI (Mahv, Mamh, Lanthi, Diem)
||THI||: mi hc vin c php thi mt mn
hc nhiu ln, mi ln thi lu tr hc vin
no thi mn g? ln thi th my? v im l
bao nhiu?
Khoa HTTT-i hc CNTT 33
2.6 Php chiu (1)
Php chiu : Dng trch gi tr ca mt s thuc tnh
trong danh sch cc thuc tnh ca quan h.
K hiu: php chiu ca quan h R ln tp thuc tnh X
l R[X] hoc R.X.
V d:


hv
1
=
hv
2
=
hv
3
=
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
Khoa HTTT-i hc CNTT 34
2.6 Php chiu (2)
Php chiu ca quan h HOCVIEN ln thuc
tnh NoiSinh ca quan h HOCVIEN:
HOCVIEN[Noisinh] = {Nghe An,Kien Giang,Tay Ninh}

HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
Khoa HTTT-i hc CNTT 35
Php chiu ln 1 tp thuc tnh
X={Hoten,Noisinh} ca quan h HOCVIEN
HOCVIEN[Hoten, Noisinh] = {(Ha Duy Lap, Nghe
An),(Tran Ngoc Han, Kien Giang),(Tran Ngoc
Linh,Tay Ninh)}
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
2.6 Php chiu (3)
Khoa HTTT-i hc CNTT 36
2.6 Php chiu (4)
Chiu ca mt b ln tp thuc tnh: dng trch
chn cc gi tr c th ca b gi tr theo cc thuc
tnh c ch ra trong danh sch thuc tnh ca mt
quan h.
K hiu: chiu ca mt b gi tr t ln tp thuc tnh
X ca quan h R l t
R
[X] hoc t[X]. Nu X c 1 thuc
tnh t
R
.X
V d: cho quan h HOCVIEN vi tp thuc tnh
HOCVIEN
+
={Mahv,Hoten,Gioitinh,Noisinh,Malop},
cha 3 b gi tr hv
1
,hv
2
v hv
3
Khoa HTTT-i hc CNTT 37
Php chiu 1 b ln 1 thuc tnh
hv
1
[Hoten] = (Ha Duy Lap)
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
hv
1
=
hv
2
=
hv
3
=
2.6 Php chiu (5)
Khoa HTTT-i hc CNTT 38
2.6 Php chiu (6)
Php chiu 1 b ln 1 tp thuc tnh
tp thuc tnh X={Hoten, Gioitinh}
hv
2
[X] = (Tran Ngoc Han,Nu)


hv
1
=
hv
2
=
hv
3
=
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
Khoa HTTT-i hc CNTT 39
2.7 Kha
2.7.1 Siu kha (super key)
2.7.2 Kha (key)
2.7.3 Kha chnh (primary key)
2.7.4 Kha tng ng
2.7.5 Kha ngoi (foreign key)
Khoa HTTT-i hc CNTT 40
2.7.1 Siu kha (super key) (1)
Siu kha : l mt tp con cc thuc tnh ca Q
+

m gi tr ca chng c th phn bit 2 b khc
nhau trong cng mt th hin T
Q
bt k.
Ngha l: t
1
, t
2
e T
Q
, t
1
[K] = t
2
[K] K l siu
kha ca Q.
Mt quan h c t nht mt siu kha (Q
+
) v c th
c nhiu siu kha.
Khoa HTTT-i hc CNTT 41
2.7.1 Siu kha (super key) (2)
V d: cc siu kha ca quan h HOCVIEN l:
{Mahv};{Mahv,Hoten};{Hoten};{Noisinh,Hoten}

HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
K1105 Tran Minh Long Nam TpHCM K11
K1106 Le Nhat Minh Nam TpHCM K11
Khoa HTTT-i hc CNTT 42
2.7.2 Kha (key) (1)
Kha : K l kha ca quan h R, tha mn 2 iu kin:
K l mt siu kha.
K l siu kha nh nht (cha t thuc tnh nht v
khc rng) ngha l
-K
1
c K, K
1
= C sao cho K
1
l siu kha.
Thuc tnh tham gia vo mt kha gi l thuc tnh
kha, ngc li l thuc tnh khng kha.
Khoa HTTT-i hc CNTT 43
V d: cc siu kha ca quan h HOCVIEN l:
{Mahv};{Mahv,Hoten};{Hoten};{Hoten,Gioitinh};
{Noisinh,Hoten};{Mahv,Hoten,Gioitinh,Noisinh}
=> th kha ca quan h HOCVIEN c th l
{Mahv}; {Hoten}
V d: kha ca quan h GIANGDAY (Malop,
Mamh, Magv, HocKy, Nam) l K={Malop,Mamh}.
Thuc tnh kha s l: Mamh,Malop. Thuc tnh
khng kha s l Magv, HocKy, Nam.
2.7.2 Kha (key) (2)
Khoa HTTT-i hc CNTT 44
2.7.3 Kha chnh (primary key)
Khi ci t trn mt DBMS c th, nu quan h c
nhiu hn mt kha, ta ch c chn mt v gi l
kha chnh
K hiu: cc thuc tnh nm trong kha chnh khi
lit k trong quan h phi c gch di.
V d:
HOCVIEN (Mahv,Hoten,Gioitinh,Noisinh,Malop)
GIANGDAY(Mamh,Malop,Magv,Hocky,Nam)
Khoa HTTT-i hc CNTT 45
2.7.4 Kha tng ng
Cc kha cn li (khng c chn lm kha
chnh) gi l kha tng ng.
V d: trong hai kha {Mahv},{Hoten} th
kha chnh l {Mahv}, kha tng ng l
{Hoten}
Khoa HTTT-i hc CNTT 46
2.7.5 Kha ngoi (1)
Cho R(U), S(V). K
1
_U l kha chnh ca R,K
2
_V
Ta ni K
2
l kha ngoi ca S tham chiu n kha
chnh K
1
ca R nu tha cc iu kin sau:
K
1
v K
2
c cng s lng thuc tnh v ng
ngha ca cc thuc tnh trong K
1
v K
2
cng
ging nhau.
Gia R v S tn ti mi quan h 1-n trn K
1
v K
2
,
s e S, !-r e R sao cho r.K
1
=s.K
2
Khoa HTTT-i hc CNTT 47
2.7.5 Kha ngoi (2)
V d, cho 2 quan h
LOP (Malop,Tenlop,Siso,Khoahoc)

HOCVIEN (Mahv,Hoten,Gioitinh,Noisinh,Malop)

Thuc tnh Malop trong quan h LOP l kha chnh
ca quan h LOP. Thuc tnh Malop trong quan h
HOCVIEN l kha ngoi, tham chiu n Malop
trong quan h LOP
Khoa HTTT-i hc CNTT 48
2.7.5 Kha ngoi (3)
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
K1105 Tran Minh Long Nam TpHCM K11
K1106 Le Nhat Minh Nam TpHCM K11
LOP
Malop Tenlop Trglop Siso Magvcn
K11 Lop 1 khoa 1 K1106 11 GV07
K12 Lop 2 khoa 1 K1205 12 GV09
K13 Lop 3 khoa 1 K1305 12 GV14
Khoa HTTT-i hc CNTT 49
2.8 Lc quan h (1)
Lc quan h nhm mc ch m t cu
trc ca mt quan h v cc mi lin h gia
cc thuc tnh trong quan h .
Cu trc ca mt quan h l tp thuc tnh
hnh thnh nn quan h .
Mt lc quan h gm mt tp thuc tnh
ca quan h km theo mt m t xc nh
ngha v mi lin h gia cc thuc tnh
Khoa HTTT-i hc CNTT 50
2.8 Lc quan h (2)
Lc quan h c c trng bi:
Mt tn phn bit
Mt tp hp hu hn cc thuc tnh (A
1
, , A
n
)

K hiu ca lc quan h Q gm n thuc
tnh (A
1
, A
2
, ... A
n
) l :
Q(A
1
, A
2
, ..., A
n
)
Khoa HTTT-i hc CNTT 51
2.8 Lc quan h (3)
HOCVIEN(Mahv,Hoten,Gioitinh,Noisinh,Malop)
Tn t: mi hc vin c mt m hc vin phn bit
vi cc hc vin khc. Cn lu tr h tn, gii tnh, ni
sinh v thuc lp no.
HOCVIEN
Mahv HoTen Gioitinh Noisinh Malop
K1103 Ha Duy Lap Nam Nghe An K11
K1102 Tran Ngoc Han Nu Kien Giang K11
K1104 Tran Ngoc Linh Nu Tay Ninh K11
K1105 Tran Minh Long Nam TpHCM K11
K1106 Le Nhat Minh Nam TpHCM K11
Khoa HTTT-i hc CNTT 52
2.8 Lc CSDL (1)
L tp hp gm cc lc quan h v cc mi
lin h gia chng trong cng mt h thng qun
l.

Cc CSDL
H Qun Tr
CSDL
Cc quan h
Khoa HTTT-i hc CNTT 53
HOCVIEN (MAHV, HO, TEN, NGSINH, GIOITINH, NOISINH, MALOP)
Tn t: mi hc vin phn bit vi nhau bng m hc vin, lu tr h tn, ngy sinh,
gii tnh, ni sinh, thuc lp no.
LOP (MALOP, TENLOP, TRGLOP, SISO, MAGVCN)
Tn t: mi lp gm c m lp, tn lp, hc vin lm lp trng ca lp, s s lp v
gio vin ch nhim.
KHOA (MAKHOA, TENKHOA, NGTLAP, TRGKHOA)
Tn t: mi khoa cn lu tr m khoa, tn khoa, ngy thnh lp khoa v trng khoa
(cng l mt gio vin thuc khoa).
MONHOC (MAMH, TENMH, TCLT, TCTH, MAKHOA)
Tn t: mi mn hc cn lu tr tn mn hc, s tn ch l thuyt, s tn ch thc hnh
v khoa no ph trch.
DIEUKIEN (MAMH, MAMH_TRUOC)
Tn t: c nhng mn hc hc vin phi c kin thc t mt s mn hc trc.

Lc CSDL qun l bn
hng
Khoa HTTT-i hc CNTT 54
GIAOVIEN(MAGV,HOTEN,HOCVI,HOCHAM,GIOITINH,NGSINH,NGVL,
HESO, MUCLUONG, MAKHOA)
Tn t: m gio vin phn bit gia cc gio vin, cn lu tr h tn, hc v, hc
hm, gii tnh, ngy sinh, ngy vo lm, h s, mc lng v thuc mt khoa.

GIANGDAY(MALOP,MAMH,MAGV,HOCKY, NAM,TUNGAY,DENNGAY)
Tn t: mi hc k ca nm hc s phn cng ging dy: lp no hc mn g do
gio vin no ph trch.

KETQUATHI (MAHV, MAMH, LANTHI, NGTHI, DIEM, KQUA)
Tn t: lu tr kt qu thi ca hc vin: hc vin no thi mn hc g, ln thi th
my, ngy thi l ngy no, im thi bao nhiu v kt qu l t hay khng t.