You are on page 1of 44

CHNG II

M hnh quan h thc th

H C S D Liu
HCSDL

1
Ni dung

M hnh quan h thc th ER


(Entity Relationship Model ER Model)
Thc th
Mi quan h
Thit k m hnh ER

2
Qu trnh thit k CSDL
H C S D Liu

3
M hnh quan h thc th

M hnh quan h thc th ER


(Entity Relationship Model ER Model) c
dng thit k CSDL mc khi nim
(Conceptual data modeling)
Biu din tru tng cu trc ca CSDL
M hnh ER nh 1 cng c trao i tng
gia nh thit k v ngi dng cui trong giai
on phn tch. N c lp vi DBMS v qu
trnh hin thc database.

4
S quan h thc th

S quan h thc th (entity relationship


diagram - ERD) l biu din hnh nh ca quan
h thc th
Ba phn t c bn:
Kiu thc th (Entity Type)
Mi quan h (Relationship)
Cc thuc tnh (Attribute)

5
V d
H C S D Liu

6
Thc th - Entity
H C S D Liu

Thc th l i tng chnh m ta c thng tin v


chng.
Thc th c th l
Mt ngi nh nhn vin, sinh vin,..

Mt ni chn nh thnh ph, t nc,..

Mt s kin nh u gi, thi ...

Mt khi nim nh mn hc, ti khon,

7
Cch t tn v k hiu

Mi kiu thc th phi c mt tn gi, nn l


danh t v vit ch hoa.
Th hin (instance) l mt trng hp c th
ca kiu thc th: Nguyn vn A
K hiu ca cc kiu thc th

EMPLOYEE DEPENDENT

Kiu thc th mnh Kiu thc th yu


8
Thc th

Thc th ch c mt th hin ???


Bo co chi ph c nhiu bo co rt ra t
CSDL, n c l thc th ???

9
V d kiu thc th mnh/yu

Dependent_Name
BirthDate
Employee_ID Employee_Name

Has DEPENDENT
EMPLOYEE

10
Kiu thc th kt hp
Associative entity type
H C S D Liu

L mt kiu thc th dng lin kt mt hay


nhiu kiu thc th v c cha thm mt s
thuc tnh ring bit ca mi lin kt ny

STUDENT CERTIFICATE COURSE

Certificate_ID Completed Date

11
Kiu thc th kt hp

Customer_ID Vendor_ID
Date

Customer_Name Address
Shipment_No

CUSTOMER Shipment VENDOR

12
Thuc tnh - attribute
H C S D Liu

Mi kiu thc th c 1 s thuc tnh.


Thuc tnh l c tnh ca 1 kiu thc th hay 1
mi lin kt.
V d: kiu thc th STUDENT c cc thuc tnh
nh Student_ID, Student_Name, Address, Major.

STUDENT Student_ID, Student_Name,

13
Cc kiu thuc tnh

Thuc tnh n (simple attribute): l thuc tnh


khng th phn nh c.
V d: Color, Weight, HorsePower
Thuc tnh phc hp (composite attribute): l
thuc tnh c th phn thnh nhiu thnh phn
V d: Thuc tnh Address bao gm cc thnh
phn Street, District, City

14
Cc kiu thuc tnh (tt)

Thuc tnh n tr (single valued attribute)


Thuc tnh a tr (multivalued attribute): c
th c nhiu hn mt tr cho mt th hin ca
thc th
V d: Thc th COURSE c thuc tnh Teacher
a tr, mt mn hc c th c dy bi nhiu
hn 1 thy c

15
Cc kiu thuc tnh (tt)

Thuc tnh kha: l 1 thuc tnh hoc 1 t hp


cc thuc tnh xc nh c cc th hin
(instance) ring bit ca 1 kiu thc th
V d: Student_ID l thuc tnh xc nh ca
kiu thc th STUDENT

16
Cc kiu thuc tnh (tt)

Thuc tnh dn xut ( derived attribute): l


thuc tnh m tr ca n c th tnh ra c t
cc thuc tnh khc
V d: Year_Employed l thuc tnh dn xut t
thuc tnh Date_Employed

17
Cch t tn v k hiu

Mi thuc tnh nn c biu din l danh t


s t v vit ch thng.
K hiu ca cc kiu thuc tnh:

Student_ID Teacher Years_Employed

Thuc tnh xc nh
Thuc tnh a tr Thuc tnh dn
xut

18
Gi tr ca thuc tnh
H C S D Liu

Kiu d liu (data type) v Min gi tr ca


thuc tnh (domain)
Kiu chui (string)
Kiu s nguyn (integer)
Kiu s thc
V d thc th NHANVIEN c cc thuc tnh
H tn (hoten: string[20])
Ngy sinh (ns: date)
im TB (DTB: float)

19
Chn thuc tnh thch hp
H C S D Liu

STUDENT Completes COURSE

Date_Completed

price

Bars Sells Beers

20
Mi quan h - Relationship

Mi quan h (relationship): din t s lin


quan gia mt hay nhiu kiu thc th vi
nhau. Tn quan h l mt ng t c ngha
Bc ca mi quan h: l s kiu thc th
tham gia vo mi quan h
Cc loi mi quan h (relationship type)
Mi quan h 1 ngi
Mi quan h 2 ngi
Mi quan h 3 ngi

21
Quan h mt ngi (Unary relationship)

L mi quan h gia cc th hin ca cng


mt thc th
Cn gi l mi quan h quy (Recursive
Relationship)
V d:

EMPLOYEE
Manages

22
Vai tr (Role)

i khi mt thc th xut hin nhiu hn 1 ln


trong mi quan h.
phn bit, nn cho bit vai tr trn cc
cnh ni gia mi quan h v thc th.

Nhn vin

EMPLOYEE
Manages

Gim c

23
Quan h hai ngi (Binary relationship)

L mi quan h gia hai kiu thc th

STUDENT Registers for COURSE

24
Quan h ba ngi (Ternary relationship)

L mi quan h gia 3 kiu thc th

PART

VENDOR Supplies WAREHOUSE

Shipping mode Unit cost

25
Lng s ca mi quan h (Cardinality)

Lng s l s th hin ca kiu thc th B m


c th lin kt vi mi th hin ca kiu thc
th A
Lng s ti thiu (minimum cardinality): l s
ti thiu ca cc th hin ca kiu thc th B
m c th lin kt vi mi th hin ca kiu
thc th A
Lng s ti a (maximum cardinality)

26
Lng s ca mi quan h (Cardinality)

Nu lng s ti thiu l 0, kiu thc th B c


gi l nhim
Nu lng s ti thiu v ti a u l 1 th lng
s ny c gi l bt buc (mandatory)
Ba dng lin kt (1, 2 ngi):
Lin kt 1-1
Lin kt 1-n
Lin kt n-n

27
V d

Name
Item_No Unit Cost

ITEM

Quantity Contains

28
K hiu ca lng s

Nhim : k hiu l O|
Bt buc: k hiu l ||
Nhiu
Mt hoc nhiu |
Khng hoc nhiu O

29 29
Lng s

30
Lng s - s thc th tham gia

EMPLOYEE Is_assigned_to PROJECT

Rose
PROJECT1

Peter
PROJECT2

Tom
PROJECT3
Heidi

31
Quy tc nghip v

Mi k s dng ch 1 s ghi chp cho 1 n.


Nhng k s khc nhau s dng nhng s ghi
chp khc nhau khi lm vic cho cng mt
n. Khng c k s no dng cng mt s ghi
chp cho mt hoc nhiu n khc nhau

32
Xc nh lng s

ENGINEER

CASEBOOK PROJECT

33
Xc nh quan h v lng s

Quy tc nghip v (business rule)


1. Mi ngi bn (vendor) c th cung cp nhiu
ph tng (part) cho 1 s kho (warehouse)
nhng khng cn phi cung cp tt c cc ph
tng
2. Mi ph tng c th c cung cp bi 1 s
ngi bn hng cho 1 hay nhiu kho, nhng
mi ph tng nht thit phi c cung cp bi
t nht 1 ngi bn hng cho 1 kho.
3. Mi kho c th c cung cp vi 1 s ph
tng t nhiu hn mt nh bn hng nhng
mi kho nht thit phi c cung cp vi t
nht 1 ph tng.
34
Xc nh lng s

PART

SUPPLY
VENDOR WAREHOUSE
SCHEDULE

Shipping mode Unit cost

35
Thit k m hnh ER

Bc 1: Nhn dng cc kiu thc th


Bc 2: Nhn dng cc kiu lin kt
Bc 3: Nhn dng cc thuc tnh ca cc
kiu thc th v cc mi lin kt
Bc 4: Nhn dng thuc tnh xc nh cho
mi kiu thc th
Bc 5: Nhn dng cc cu trc siu
kiu/kiu con
Bc 6: V s ER

36
HCSDL 37
Cc lu
H C S D Liu

Chn thuc tnh thch hp


Loi b mi lin kt tha
Trnh d tha d liu
Thc th ch c mt thuc tnh
Khng s dng thc th ch c 1 th hin
Thuc tnh ca quan h

38
V d
H C S D Liu

39
H C S D Liu

40
V d Qun l d n
H C S D Liu

Cty c nhiu n v, mi n v (phng ban) c


tn duy nht, m n v duy nht, mt trng
phng v ngy nhm chc. Mi n v c th
nhiu a im khc nhau.
D n c tn duy nht, m duy nht, do 1 mt
phng ban ch tr v c trin khai 1 a im.
Nhn vin c m s, tn, a ch, ngy sinh, gii
tnh v lng. Mi nhn vin lm vic 1 phng
ban, tham gia vo cc n vi s gi lm vic
khc nhau. Mi nhn vin u c mt ngi qun
l trc tip.
Mt nhn vin c th c nhng ngi con c
hng bo him theo nhn vin. Mi ngi con
ca nhn vin c tn, gii
41 tnh, ngy sinh.
Bi tp v th vin
H C S D Liu

HCSDL 42
Bi tp v trng hc
H C S D Liu

HCSDL 43
Bi tp

Xem ng dng mu trang 67 ca sch


Lm tt c bi tp chng 3 gio trnh

44