You are on page 1of 26

Chng 4

Rng buc ton vn

Khi nim
Trong mi CSDL lun tn ti nhiu mi lin
h gia cc thuc tnh, gia cc b; s
lin h ny c th xy ra trong cng mt
quan h hoc trong cc quan h ca mt
lc CSDL. Cc mi lin h ny l
nhng iu kin bt bin m tt c cc b
ca nhng quan h c lin quan trong
CSDL u phi tho mn mi thi im.
Nhng iu kin bt bin c gi l
rng buc ton vn..
Rng buc ton vn c xem nh l mt
cng c din t ng ngha ca CSDL

Khi nim
Cng vic kim tra rng buc ton
vn thng c tin hnh vo thi
im cp nht d liu ( thm, sa,
xo). Nhng rng buc ton vn pht
sinh phi cn c ghi nhn v x l
mt cch tng minh (thng l bi
mt hm chun hoc mt on
chng trnh).

Cc yu t ca rng buc ton vn


Mi rng buc ton vn c bn yu
t: iu kin, bi cnh, bng tm nh
hng v hnh ng phi cn thc
hin khi pht hin c rng buc ton
vn b vi phm:
iu kin:
V d:
- Mi lp hc phi thuc v mt khoa ca trng.
- Mi sinh vin c mt m s sinh vin duy nht, khng trng
vi bt c sinh vin no trong trng.
- Mi hc vin phi ng k vo mt lp hc trong trng.

Cc yu t ca rng buc ton vn


Bi cnh:
Nhng quan h m rng buc c
hiu lc hay ni mt cch khc, l
nhng quan h cn phi c kim
tra khi tin hnh cp nht d liu

Cc yu t ca rng buc ton vn


Bng tm nh hng:

Du + ti dng r1 ct Thm -> thm mt b vo r1 th s vi phm RBTV


Du ti dng r2 ct Sa -> khi sa mt b trn quan h r2 th khng
vi phm RBTV
Quy c:
-Khng c sa thuc tnh kho.
-Nu khng b vi phm do khng c php sa i th k hiu l -(*).

Cc yu t ca rng buc ton vn


Hnh ng cn phi c
C 2 gii php:
Th nht: a ra thng bo v yu cu sa cha
d liu ca cc thuc tnh cho ph hp vi quy
tc m bo tnh nht qun d liu. Thng bo
phi y v phi thn thin vi ngi s dng.
Gii php ny l ph hp cho vic x l thi gian
thc.
Th hai:T chi thao tc cp nht. Gii php ny
l ph hp i vi vic x l theo l. Vic t chi
cng phi c lu li bng nhng thng bo
y , r rng v sao thao tc b t chi v cn
phi sa li nhng d liu no ?

Phn loi rng buc ton vn


C 2 loi
Th nht: Rng buc ton vn c phm vi
l mt quan h bao gm :Rng buc ton
vn min gi tr, rng buc ton vn lin
thuc tnh, rng buc ton vn lin b.
Th hai: Rng buc ton vn c phm vi l
nhiu quan h bao gm :Rng buc ton
vn ph thuc tn ti, rng buc ton vn
lin b - lin quan h, rng buc ton vn
lin thuc tnh - lin quan h.

Phn loi rng buc ton vn


V d
Cho mt CSDL R dng qun l
vic t hng v giao hng ca mt
cng ty. Lc CSDL R gm cc
lc quan h nh sau:
Q1: Khach (MAKH, TENKH, DIACHIKH, DIENTHOAI)
Tn t:
Mi khch hng c mt m khch hng (MAKH)
duy nht, mi MAKH xc nh tn khch hng
(TENKH), a ch (DIACHIKH), s in thoi
(DIENTHOAI).

Phn loi rng buc ton vn


V d
Q2:Hang(MAHANG,TENHANG,QUYCACH, DVTINH)
Tn t:
Mi mt hng c mt m hng (MAHANG) duy
nht, mi MAHANG xc nh tn hng
(TENHANG), quy cch hng (QUYCACH), n v
tnh DVTINH).

Phn loi rng buc ton vn


V d
Q3: Dathang(SODH,MAHANG, SLDAT, NGAYDH,
MAKH)
Tn t:
Mi m s t hng (SODH) xc nh mt ngy
t hng (NGAYDH) v m khch hng tng ng
(MAKH). Bit m s t hng v m mt hng th
bit c s lng t hng(SLDAT). Mi khch
hng trong mt ngy c th c nhiu ln t hng

Phn loi rng buc ton vn


V d
Q4: Hoadon(SOHD, NGAYLAP, SODH, TRIGIAHD, NGAYXUAT)
Tn t:
Mi ho n tng hp c mt m s duy nht l SOHD, mi
ho n bn hng c th gm nhiu mt hng. Mi ho n
xc nh ngy lp ho n (NGAYLAP), ng vi s t hng
no (SODH). Mi ha n xc nh mt tr gi ca mt hng
trong ho n (TRIGIAHD) v mt ngy xut kho giao hng
cho khch (NGAYXUAT)

Phn loi rng buc ton vn


V d

Q5: Chitiethd (SOHD, MAHANG, GIABAN, SLBAN)


Tn t:
Mi SOHD, MAHANG xc nh gi bn (GIABAN)
v s lng bn (SLBAN) ca mt mt hng trong
mt ho n.
Q6: Phieuthu(SOPT, NGAYTHU, MAKH, SOTIEN)
Tn t:
Mi phiu thu c mt s phiu thu (SOPT) duy
nht, mi SOPT xc nh mt ngy thu
(NGAYTHU) ca mt khch hng c m khch
hng l MAKH v s tin thu l SOTIEN. Mi
khch hng trong mt ngy c th c nhiu s
phiu thu.

Phn loi rng buc ton vn


Rng buc ton vn lin b

+Rng buc ton vn v kho chnh:


y l mt trng hp c bit ca
Rng Buc ton Vn lin b, RBTV
ny rt ph bin v thng c cc
h qun tr CSDL t ng kim tra.

Phn loi rng buc ton vn


Rng buc ton vn lin b

Vi r l mt quan h trn lc quan h Khach


ta c rng buc ton vn sau:
R1: t1, t2 r
t1. MAKH t2. MAKH

Phn loi rng buc ton vn


Rng Buc Ton Vn V Min Gi Tr
Rng buc ton vn c lin quan n min
gi tr ca cc thuc tnh trong mt quan
h. Rng buc ny thng gp. Thng
thng cc h qun tr CSDL t ng
kim tra (mt s) rng buc loi ny.

Phn loi rng buc ton vn


Rng Buc Ton Vn V Min Gi Tr
V d
Vi r l mt quan h ca Hoadon ta c
rng buc ton vn sau
R3: t r
t.TRIGIAHD > 0

Phn loi rng buc ton vn


Rng Buc Ton Vn Lin Thuc Tnh
Rng buc ton vn lin thuc tnh (mt
quan h) l mi lin h gia cc thuc tnh
trong mt lc quan h.
V d
Vi r l mt quan h ca Hoadon ta c rng buc ton vn sau:
R4: t r
t.NGAYLAP <= t.NGAYXUAT

Phn loi rng buc ton vn


Rng Buc Ton Vn V Kho Ngoi
Rng buc ton vn v kho ngoi
cn c gi l rng buc ton vn
ph thuc tn ti. Cng ging nh
rng buc ton vn v kho ni, loi
rng buc ton vn ny rt ph bin
trong cc CSDL.

Phn loi rng buc ton vn


Rng Buc Ton Vn V Kho Ngoi
V d
R2. dathang[MAKH] khach[MAKH]

Phn loi rng buc ton vn


Rng Buc Ton Vn Lin Thuc Tnh Lin
Quan H
Rng buc loi ny l mi lin h gia cc
thuc tnh trong nhiu lc quan h.
V d
Vi r,s ln lt l quan h ca Dathang v
Hoadon. Ta c rng buc ton vn R5 nh
sau:
R5: t1 r, t2 s
Nu t1.SODH=t2.SODH th
t1.NGAYDH <= t2.NGAYXUAT

Phn loi rng buc ton vn


Rng Buc Ton Vn Lin Thuc Tnh Lin
Quan H

Phn loi rng buc ton vn


Rng Buc Ton Vn Lin B Lin Quan
H
Rng buc loi ny l mi lin h gia cc b
trong mt lc c s d liu. Chng hn nh
tng s tin phi tr trong mi ho n (chitiethd)
phi bng TR GI HO N ca ho n
trong quan h Hoadon. Hoc s lng hc vin
trong mt lp phi bng SOHOCVIEN ca lp .
Ngoi ra cn c mt s loi RBTV khc nh
:RBTV v thuc tnh tng hp, RBTV do tn ti
chu trnh ,RBTV v gi tr thuc tnh theo thi
gian.

V d:
Cho lc quan h:
IEMTHI(IEMTHISO,IACHIDIEMTHI)
Mt hi ng coi thi tuyn sinh c nhiu im thi,
mi im thi c t ti mt trng no . Cc
im thi (IEMTHISO) c nh s l im thi s
1, im thi s 2, im thi s 3,Mi im thi xc
nh a ch (IACHIDIEMTHI).
THISINH(SOBD,HOTEN,NGAYSINH, PHAI, IACHI,
MANGANH, PHONGTHI)

Mi th sinh c mt s bo danh (SOBD) duy nht,


mi s bo danh xc nh cc thng tin: h v tn
(HOTEN), ngy sinh (NGAYSINH), phi (PHAI), a
ch thng tr (IACHI), m ngnh ng k
thi(MANGANH), s hiu phng thi(PHONGTHI).

V d
NGANH(MANGANH,TENNGANH)
Mi ngnh c mt m ngnh (MANGANH) duy
nht, mi m ngnh xc nh tn ngnh
TENNGANH), chng hn ngnh Cng Ngh Thng
Tin c m ngnh l 01, ngnh Cng Ngh Ho
Thc Phm c m ngnh l 10,
PHONG(PHONGTHI,IEMTHISO)
Mi im thi c nhiu phng thi (PHONGTHI)
c nh s hiu khc nhau tt c cc im
thi (trong mt phng thi c th c cc th sinh ca
nhiu ngnh khc nhau)

V d
Hy xc nh cc rng buc ton vn
c trong lc c s d liu trn
(mi loi cho mt v d)