You are on page 1of 177

Y BAN NHN DN THNH PH H NI

S BU CHNH VIN THNG H NI

GIO TRNH NGN NG SQL


(M s gio trnh: 3CD3)

H NI. 2005

H NI, 12-2004

LI M U Ngn ng SQL (Structured Query Language) c s dng trong hu ht cc h qun tr c s d liu truy vn v sa i c s d liu. Ngn ng SQL h tr cc truy vn da trn cc php ton i s quan h, ng thi cng cha cc lnh sa i c s d liu v m t lc c s d liu. Nh vy, SQL va l mt ngn ng thao tc d liu, va l mt ngn ng nh ngha d liu. Ngoi ra SQL cng tiu chun ho nhiu lnh c s d liu khc. C nhiu phin bn khc nhau ca SQL. Trc tin, c ba bn chun. l ANSI (American National Standards Institute) SQL. Sau n nm 1992, bn chun SQL-92 ra i gi l SQL2. Gn y nht, chun SQL-99 (trc gi l SQL3) m rng SQL2 vi cc c trng quan h - i tng v mt s kh nng mi khc. Ngoi ra cn c nhiu phin bn ca SQL c cc nh bn cc h qun tr c s d liu sn xut. Cc phin bn ny c tt c cc kh nng ca chun ANSI nguyn gc v chng cng ph hp vi cc m rng ca SQL cng nh cc tnh cht ca chun SQL-99. Trong gio trnh ny chng ti trnh by da trn chun SQL-99. Gio trnh gm ba chng: Chng 1: SQL c bn, trnh by cc truy vn c bn trn cc bng c s d liu, cc kiu d liu c bn trong SQL v cch to c s d liu n gin trong SQL Chng 2: Cc rng buc v cc trigger. Chng ny trnh by cc loi rng buc: rng buc min, rng buc kha, rng buc ton vn thc th, rng buc ton vn tham chiu, cc rng buc khc v cch th hin chng trong SQL. Chng 3: Lp trnh vi SQL, trnh by cc phng php lp trnh trong SQL: lp trnh nhng, SQL ng, cc hm v cc th tc PSM, s dng giao din gi. Ngoi ra, chng ny cn cp n vn an ton trn c s d liu SQL. Cui mi chng c tng kt cc vn trnh by trong chng v mt s bi tp. hiu c gio trnh ny bn c cn phi c cc kin thc v c s d liu quan h. Do hn ch v thi gian v kinh nghim, chc chn gio trnh vn cn nhiu thiu st. Mong cc bn c gp , ph bnh. Chng ti xin cm n trc v ha s tip thu hon thin gio trnh hn. 2

- Tn mn hc: Ngn ng SQL. - M s mn hc: 3CD3 - Thi gian: 45 tit (l thuyt + thc hnh) - Mc tiu: Hng dn hc vin s dng thnh tho ngn ng truy vn SQL. - Nhng kin thc cn c trang b trc: C s d liu quan h. - Ni dung mn hc: Chng I: C BN V SQL. Chng II: CC RNG BUC V TRIGGER. Chng III: LP TRNH - i tng hc: Cc lp trnh vin. - Bin son: B mn Cc h thng thng tin, Khoa Cng ngh thng tin, Trng H Cng Ngh, HQG H Ni.

LI M U.........................................................................................2 CHNG I: SQL C BN...........................................................................8 1.1 CC TRUY VN N GIN TRONG SQL......................................8 1.1.1 Php chiu trong SQL....................................................................9 1.1.2 Php chn trong SQL...................................................................11 1.1.3 So snh cc xu............................................................................13 1.1.4 Ngy thng v thi gian...............................................................14 1.1.5 Cc gi tr NULL v cc so snh bao hm NULL.......................15 1.1.6 Gi tr lgic UNKNOWN ............................................................16 1.1.7 Sp th t d liu ra.....................................................................17 1.1.8 Cc hm thng dng trong SQL...................................................18 1.2 CC TRUY VN BAO GM NHIU HN MT QUAN H.......20 1.2.1 Tch v ni trong SQL..................................................................20 1.2.2 Lm r ngha cc thuc tnh.........................................................21 1.2.3 Cc bin b...................................................................................22 1.2.4 Php hp, php giao, php tr ca cc truy vn...........................23 1.3 CC TRUY VN CON......................................................................25 1.3.1 Cc truy vn con to ra cc gi tr v hng ...............................25 1.3.2 Cc iu kin c bao hm cc quan h.........................................27 1.3.3 Cc iu kin c bao hm cc b.................................................27 1.3.4 Cc truy vn con tng quan vi nhau.........................................28 1.3.5 Cc truy vn con trong mnh FROM......................................30 1.3.6 Cc biu thc ni ca SQL...........................................................31 1.3.7 Ni t nhin (Natural Join)..........................................................32 1.3.8 Ni ngoi......................................................................................33 1.4 CC PHP TON QUAN H Y ...........................................34 1.4.1 Loi b trng lp..........................................................................34 1.4.2 Trng lp trong php hp, php giao v php tr ......................34 1.4.3 Nhm v s kt hp trong SQL ...................................................36 1.4.4 Cc php ton nhm.....................................................................36 1.4.5 Nhm ...........................................................................................37 1.4.6 Cc mnh HAVING.................................................................40 1.5 SA I C S D LIU...............................................................41 1.5.1 Chn.............................................................................................41 1.5.2 Xa...............................................................................................43 1.5.3 Cp nht........................................................................................44 1.6 NH NGHA MT LC QUAN H TRONG SQL..............45 1.6.1 Cc kiu d liu............................................................................45 4

1.6.2 Cc khai bo bng n gin.........................................................46 1.6.4 Cc gi tr ngm nh...................................................................47 1.6.5 Cc ch s.....................................................................................48 1.6.6 Nhp mn v vic la chn cc ch s.........................................49 1.7 KHUNG NHN (VIEW).....................................................................50 1.7.1 Khai bo cc khung nhn..............................................................50 1.7.2 Truy vn cc khung nhn.............................................................51 1.7.3 t tn li cc thuc tnh..............................................................52 1.7.4 Sa i cc khung nhn................................................................53 1.7.5 Gii thch cc truy vn c cha cc khung nhn...........................56 1.8 TNG KT CHNG I.....................................................................59 MT S BI TP....................................................................................61 CHNG II: CC RNG BUC V CC TRIGGER.............................65 2.1 KHA V KHA NGOI.................................................................66 2.1.1 M t kho chnh..........................................................................66 2.1.2 Cc kho c m t vi UNIQUE.............................................67 2.1.3 Lm c hiu lc cc rng buc kho............................................68 2.1.4 M t cc rng buc kho ngoi...................................................69 2.1.5 Duy tr ton vn tham chiu.........................................................71 2.1.6 Lm chm vic kim tra rng buc..............................................73 2.2 CC RNG BUC TRN CC THUC TNH V CC B........76 2.2.1 Cc rng buc Not-Null...............................................................76 2.2.2 Cc rng buc kim tra (CHECK) da trn thuc tnh................77 2.2.3 Cc rng buc kim tra (CHECK)da trn b gi tr...................79 2.3 SA I CC RNG BUC...........................................................80 2.3.1 t tn cho cc rng buc ...........................................................80 2.3.2 Sa i cc rng buc trn cc bng............................................80 2.4 CC RNG BUC MC LC V CC TRIGGER.............81 2.4.1 Cc khng nh (assertion)...............................................................82 So snh cc rng buc...............................................................................85 2.4.2 Trigger.........................................................................................85 2.5 TNG KT CHNG II...................................................................92 MT S BI TP....................................................................................93 CHNG III: LP TRNH.........................................................................96 3.1 SQL TRONG MI TRNG LP TRNH.......................................96 3.1.1 Vn tr ngi khng ph hp ...................................................97 3.1.2 Giao din ngn ng ch /SQL......................................................98 3.1.3 Phn khai bo (DECLARE).........................................................99 3.1.4 S dng cc bin dng chung.....................................................100 3.1.5 Cc cu lnh Select n hng.....................................................102 5

3.1.6 Con tr........................................................................................103 3.1.7 Cp nht bng con tr.................................................................107 3.1.8 Bo v khi s cp nht ng thi.............................................108 3.1.9 Con tr cun (Scrolling Cursor).................................................110 3.1.10 SQL ng..................................................................................111 3.2 CC TH TC C LU GI (stored procedure) ...................113 3.2.1 To cc hm v cc th tc PSM...............................................114 3.2.2 Mt vi dng cu lnh n gin trong PSM..............................115 3.2.3 Cc cu lnh r nhnh.................................................................117 3.2.4 Cc truy vn trong PSM ...........................................................119 3.2.5 Vng lp trong PSM...................................................................120 3.2.6 Vng lp for................................................................................123 3.2.7 Nhng cu lnh lp khc............................................................124 3.3 MI TRNG SQL.........................................................................128 3.3.1 Mi trng..................................................................................128 3.3.2 Lc .....................................................................................130 3.3.4 Client v Server trong mi trng SQL.....................................132 3.3.5 Kt ni........................................................................................132 3.3.6 Phin (Session)...........................................................................133 3.3.7 Modules......................................................................................134 3.4 S DNG GIAO DIN MC GI (call-level interface)................135 3.4.1 Nhp mn SQL/CLI...................................................................135 3.4.2 X l cc lnh.............................................................................138 3.4.3 Ly d liu ra t kt qu truy vn..............................................140 3.5 GIAO TC TRONG SQL ................................................................142 3.5.1 Xp hng theo th t..................................................................142 3.5.2 Atomicity....................................................................................145 3.5.3 Giao tc (Transaction)................................................................147 3.5.4 Read-Only Transaction...............................................................148 3.5.5 Dirty Read..................................................................................150 3.5.6 Cc mc c lp khc..................................................................153 3.6 AN TON V CP QUYN TRONG SQL....................................154 3.6.1 Cc quyn...................................................................................155 3.6.2 To cc quyn.............................................................................157 3.6.3 Tin trnh kim tra c quyn.....................................................158 3.6.4 Cp cc quyn............................................................................159 3.6.5 Biu grant..............................................................................161 3.6.6 Hy b cc quyn.......................................................................162 3.7 TNG KT CHNG III................................................................167

CHNG I: SQL C BN
Ging nh cc ngn ng bc cao khc, ngn ng SQL c xy dng da trn cc ch ci, cc ch s, cc k t (du php ton, du ngn, du cch v cc k t c bit) v mt tp cc t kha. Mt lnh ca SQL c th c vit trn mt dng hoc nhiu dng, kt thc bng du chm phy ;. Ngn ng SQL c chia thnh ba nhm: - Ngn ng nh ngha d liu dng m t cu trc ca c s d liu (cc bng, cc khung nhn, cc thuc tnh, cc ch mc, ...) - Ngn ng thao tc d liu cho php thc hin cc thao tc trn c s d liu nh cp nht c s d liu v truy vn ly ra cc thng tin t c s d liu. - Ngn ng kim sot d liu bao gm cc lnh dng qun l cc giao tc, cc quyn truy cp d liu, kt ni vi server.. Ngn ng SQL c th s dng theo hai kiu: kiu trc tip v lp trnh. SQL trc tip cho php thc hin mt truy vn v nhn c kt qu ngay tc khc. SQL lp trnh cho php s dng SQL trong mt chng trnh vit bng ngn ng ngn ng lp trnh bc cao khc (C, Pascal,..), hoc vit cc chng trnh con. Trong chng ny chng ta s lm quen vi cc lnh c bn ca SQL. Cc lnh ny c minh ha da trn mt c s d liu CNGTY cho phn PHLC ca gio trnh.

1.1 CC TRUY VN N GIN TRONG SQL.


Gi s chng ta mun a ra cc nhn vin ca n v c MsV = 5, chng ta vit trong SQL nh sau SELECT * FROM NHNVIN WHERE MsV = 5 ; Truy vn ny trnh by dng c trng select-from-where ca hu ht cc truy vn SQL. Mnh FROM cho quan h hoc cc quan h m truy vn tham chiu n. Trong v d trn, quan h l NHNVIN.

Mnh WHERE l mt iu kin, ging nh iu kin chn trong i s quan h. Cc b phi tho mn iu kin chn ph hp vi truy vn. iu kin y l thuc tnh MsV ca b phi c gi tr 5. Tt c cc b p ng iu kin s tho mn iu kin chn. Mnh SELECT ni cc thuc tnh no ca cc b p ng iu kin s c a ra nh mt phn ca cu tr li. Du * trong v d ny ch ra rng tt c cc thuc tnh ca b s c a ra. Kt qu ca truy vn l mt quan h cha tt c cc b do tin trnh ny sn xut ra. Mt cch gii thch truy vn ny l xem xt tng b gi tr ca quan h c k ra trong mnh FROM. iu kin trong mnh WHERE c p dng cho b. Chnh xc hn, cc thuc tnh c k ra trong mnh WHERE c thay th bng cc gi tr ca thuc tnh trong b. Sau , iu kin c tnh, v nu ng th cc thnh phn xut hin trong mnh SELECT c sn xut ra nh l mt b ca cu tr li. 1.1.1 Php chiu trong SQL a) Chng ta c th chiu mt quan h do mt truy vn SQL sn xut ra ln trn mt s thuc tnh ca n. lm iu , v tr ca du * trong mnh SELECT ta lit k ra mt s thuc tnh ca quan h c ch ra trong mnh FROM. Kt qu s c chiu ln cc thuc tnh c lit k. V d 1: a ra Hm v Tn ca cc nhn vin n v c m s bng 5. Chng ta c th vit: SELECT Hm, Tn FROM NHNVIN WHERE MsV =5; Kt qu l mt bng c hai ct, c tn l Hm v Tn. Cc b ca bng ny l cc cp, mi cp gm Hm v Tn ca nhn vin, l cc nhn vin ca n v c m s bng 5. Bng kt qu c dng nh sau: Hm L Tn Vn

Trn c Nguyn

Nam Sn

V Hng Giang b) i khi chng ta mun to ra mt quan h vi u ct khc vi cc thuc tnh ca quan h c k ra trong mnh FROM. Chng ta c th vit sau tn ca thuc tnh mt t kho AS v mt b danh (alias), b danh s tr thnh u ct ca quan h kt qu. T kho AS l tu chn, ngha l c th vit b danh i ngay sau tn thuc tnh m khng cn phi c t kho AS. V d 2: Ta c th sa i v d 1 trn a ra mt quan h c cc thuc tnh Hnhnvin v Tnnhnvin thay cho v tr ca Hm v Tn nh sau: SELECT Hm AS Hnhnvin, Tn AS Tnnhnvin FROM NHNVIN WHERE MsV = 5 ; Bng kt qu c dng nh sau: Hnhnvin Tn nhnvin L Trn c Nguyn V Hng Vn Nam Sn Giang

c) Mt tu chn khc trong mnh SELECT s dng mt biu thc v tr ca mt thuc tnh. V d 3: Chng ta mun a ra Hm, Tn v lng sau khi c tng 10% ca cc nhn vin n v c m s bng 5. Ta vit: SELECT Hm, Tn, Lng*1.1 AS Lngmi FROM NHNVIN WHERE MsV =5; Kt qu L Hm Vn Tn Lngmi 3300 10

Trn c Nguyn

Nam Sn

4400 4180 2750

V Hng Giang

d) Chng ta cng c th cho php mt hng nh l mt biu thc trong mnh SELECT. V d 4: Ta mun a thm t ngn ng vo sau gi tr ca lng, ta vit:
SELECT Hm, Tn, Lng*1.1 AS Lngmi, ngn ng AS nvtnh FROM NHNVIN WHERE MsV =5; Kt qu L Nguyn Hm Vn Nam Sn Trn c Tn Lngmi 3300 4180 2750 4400 nvtnh ngn ng ngn ng ngn ng ngn ng

V Hng Giang

Chng ta sp xp mt ct c tn l nvtnh v mi b trong cu tr li s c hng ngn ng ct th t. 1.1.2 Php chn trong SQL Php ton chn ca i s quan h v nhiu th na sn c trong mnh WHERE ca SQL. Cc biu thc i sau WHERE bao gm cc biu thc iu kin ging nh cc biu thc iu kin trong cc ngn ng lp trnh. Chng ta c th xy dng cc iu kin bng cch so snh cc gi tr s dng su php ton so snh =, <>, <, >, <=, >=. Cc gi tr c th c so snh bao gm cc hng v cc thuc tnh ca cc quan h c k ra sau FROM. Chng ta cng c th p dng cc php ton s hc thng thng nh +, -, *, / i vi cc gi tr s trc khi chng ta so snh chng v p dng php ni || i vi cc xu. Mt v d v php so snh l MsV = 5 trong cc v d trn. Thuc tnh MsV c kim tra xem c bng hng 5 hay khng. Hng ny l mt gi tr s. Cc hng s, nh cc s nguyn v s thc c s dng v c ghi nh cch thng thng trong cc ngn ng lp trnh. Ngoi cc hng s cn c cc hng xu. Cc xu

11

trong SQL c ghi bng cch t chng v trong cp du nhy n, v d, H ni. Kt qu ca mt php so snh l mt gi tr l gic TRUE hoc FALSE. Cc gi tr l gic c th c kt hp bng cc php ton logic AND, OR, NOT vi cc ngha ca chng. V d 5: Truy vn sau y hi v Hm, Tn v Giitnh ca cc nhn vin n v c m s bng 5 v Giitnh = Nam SELECT Hm, Tn, Giitnh FROM NHNVIN WHERE (MsV =5) AND (Giitnh = Nam); Kt qu L Nguyn Hm Vn Nam Sn Tn Nam Nam Nam Giitnh

Trn c

Trong iu kin ny, chng ta c AND ca hai gi tr logic. Cc gi tr l cc php so snh bnh thng. Tip theo, ta xt v d sau: SELECT Hm, Tn FROM NHNVIN WHERE (MsV =5) AND (Giitnh = N OR Lng <= 3000); Hm L Tn Vn

V Hng Giang Truy vn ny i hi cc nhn vin hoc l n hoc c lng nh hn hoc bng 3000. Ch rng cc php so snh c th nhm li bng vic s dng cc du ngoc n. Cc du ngoc l cn thit bi v th t u tin ca cc php ton l gic trong SQL l ging nh trong cc ngn ng lp trnh, AND c th t cao hn OR, NOT c th t cao hn c AND v OR.

12

1.1.3 So snh cc xu Hai xu l bng nhau nu chng l cng mt dy k t. SQL cho php cc m t cc kiu xu khc nhau, v d, cc mng k t c di c nh v cc danh sch k t c di thay i. Khi chng ta so snh cc xu bng mt trong cc php ton nh hn nh l < hoc >=, chng ta ang hi xem c phi xu ny i trc xu kia trong th t t in. Nh vy, nu a1a2an v b1b2bm l hai xu, th xu th nht l nh hn xu th hai nu hoc a1 < b1, hoc nu a1 = b1 v a2 < b2, hoc a1 = b1, a2 = b2 v a3 < b3 Ta cng ni rng a1a2an < b1b2bm nu n<m v a1a2an = b1b2bn, ngha l xu th nht l mt tin t ng ca xu th hai. V d na < nam. SQL cng cung cp kh nng so snh cc xu trn c s mt mu i chiu n gin. Mt dng la chon ca biu thc logic l s LIKE p trong s l mt xu v p l mt mu i chiu. Mt mu i chiu l mt xu c s dng hai k t c bit % v _. Cc k t thng thng trong p ch i snh c vi chnh chng trong s, nhng % c th i snh vi mt dy c 0 hoc nhiu hn cc k t trong s, v _ i snh vi bt k k t no trong s. Gi tr ca biu thc ny l ng khi v ch khi xu s hp vi mu p. Mt cch tng t, s NOT LIKE p l ng khi v ch khi xu s khng hp vi mu p. V d 6: SELECT Tn FROM NHNVIN WHERE Tn LIKE N_ _ ; Truy vn ny i hi thuc tnh Tn c gi tr gm 3 k t, k t u tin l N v sau l mt dy no gm hai k t. Kt qu ca truy vn ny l mt tp cc tn nhn vin thch hp, chng hn nh Nam, Ni,.. V d 7: Chng ta hy tm tn ca cc nhn vin c cha ch a. Ta c truy vn sau: SELECT Tn 13

FROM NHNVIN WHERE Tn LIKE %a%; Kt qu ca truy vn ny l mt tp cc tn nhn vin tho mn iu kin chn, chng hn nh Nam, Thanh, Hoa. 1.1.4 Ngy thng v thi gian Cc th hin ca SQL ni chung h tr ngy thng v thi gian nh nhng kiu d liu c bit. Cc gi tr ny thng trnh by c trong nhiu dng khc nhau nh 14/5/1948 hoc 14-05-48. y chng ta s ch m t cch ghi chun ca SQL. Mt hng ngy thng c biu din bng t kho DATE sau l mt xu c dng c bit bn trong cp du nhy n. V d: DATE1948-05-14. Bn k t u l cc ch s biu din nm, sau l du -, hai k t tip theo l cc ch s biu din thng, tip theo l du - v cui cng l hai k t s biu din ngy. Mt hng thi gian c biu din tng t bng t kho TIME v mt xu c t trong cp du nhy n. Xu ny c hai ch s cho gi trn ng h qun s (24 gi), sau l du hai chm, hai ch s cho pht, mt du hai chm na v hai ch s cho giy. Nu phn l ca giy l cn thit, chng ta c th tip tc vi mt du chm v mt s cc ch s c ngha. V d, TIME15:00:02.5 biu din thi gian 15 gi khng pht hai giy 5 phn mi. Thi gian cn c th c biu din theo nhiu cch khc na. kt hp ngy thng v thi gian chng ta s dng mt gi tr kiu TIMESTAMP. Cc gi tr ny gm mt t kho TIMESTAMP, mt gi tr ngy thng, mt khong cch v mt gi tr thi gian. V d, TIMESTAMP1948-05-14 12:00:00 biu din 12 gi tra ngy 14 thng 5 nm 1948. Chng ta c th so snh ngy thng v thi gian bng cch s dng cc php ton so snh ging nh i vi cc s hoc cc xu. Nh vy, du < trn ngy thng chng t ngy thng th nht sm hn ngy thng th hai, cn du < trn thi gian chng t thi gian th nht sm hn thi gian th hai.

14

1.1.5 Cc gi tr NULL v cc so snh bao hm NULL. SQL cho php cc thuc tnh c gi tr c bit NULL, c gi l gi tr null. Mt thuc tnh c gi tr null khi khng bit gi tr ca n hoc khi gi tr l khng p dng c hoc gi tr b giu. Trong mnh WHERE, chng ta c th c chun b cho kh nng mt thnh phn ca mt b no l null. C hai quy tc quan trng cn phi nh khi chng ta lm php ton trn cc gi tr null. 1.Khi chng ta lm php ton mt gi tr null v mt gi tr no , bao gm c gi tr null khc, bng vic s dng mt php ton s hc nh l * hoc +, kt qu php ton l null. 2.Khi chng ta so snh mt gi tr null v mt gi tr khc, bao hm c gi tr null khc, bng cch s dng cc php so snh nh l = hoc >, kt qu php so snh l UNKNOWN. Gi tr UNKNOWN l mt gi tr l gic khc, ging nh TRUE v FALSE. Chng ta phi nh rng mc d NULL l mt gi tr c th xut hin trong cc b nhng n khng phi l mt hng. Nh vy, trong khi cc quy tc trn p dng khi chng ta c gng lm php ton trn mt biu thc m gi tr ca n l NULL, chng ta khng th dng NULL mt cch r nh l mt ton hng. V d 8: Gi s x c gi tr null. Khi gi tr ca x+3 cng l null. Tuy nhin null+3 khng phi l mt biu thc SQL hp l. Tng t, gi tr ca x = 3 l UNKNOWN bi v chng ta khng th ni rng gi tr ca x (mt gi tr NULL) l bng 3. Tuy nhin, php so snh NULL = 3 khng phi l php so snh SQL ng. Cch ng n hi xem x c gi tr null hay khng l dng biu thc x IS NULL. Biu thc ny c gi tr TRUE nu x c gi tr NULL v n c gi tr FALSE trong trng hp ngc li. Mt cch tng t, x IS NOT NULL c gi tr TRUE tr khi gi tr ca x l NULL. Trong mt s phin bn ca SQL, trc khi thc hin cc php ton vi cc gi tr null, ngi ta s dng cc hm chuyn i gi tr null thnh ra gi tr 0 (nu ton hng

15

tng ng c kiu s) hoc thnh mt xu rng nu ton hng tng ng l kiu k t. 1.1.6 Gi tr lgic UNKNOWN trn, chng ta gi thit rng kt qu ca mt php so snh hoc l TRUE hoc l FALSE, v cc gi tr lgic ny c kt hp mt cch r rng bng vic s dng cc php ton AND, OR, NOT. Khi xut hin gi tr NULL, cc php so snh c th cho mt gi tr l gic th ba UNKNOWN. By gi chng ta phi bit cc php ton logic i x nh th no trn cc t hp ca ba gi tr logic ny. Chng ta c th ngh n TRUE nh l 1, FALSE nh l 0, UNKNOWN nh l 1/2. Khi : 1.AND ca hai gi tr lgic l min ca cc gi tr . Nh vy, x AND y l FALSE nu x hoc y l FALSE, l UNKNOWN nu x v y khng l FALSE nhng t nht c mt gi tr l UNKNOWN v l TRUE khi c x v y l TRUE. 2.OR ca hai gi tr lgic l max ca cc gi tr . Nh vy x OR y l TRUE nu x hoc y l TRUE, l UNKNOWN nu x v y khng l TRUE nhng c t nht l mt gi tr UNKNOWN v c gi tr FALSE nu c x v y u FALSE. 3. Ph nh ca gi tr lgic v l 1-v. Nh vy, NOT x c gi tr TRUE khi x l FALSE, c gi tr FALSE khi x l TRUE v c gi tr UNKNOWN khi x l UNKNOWN. Bng di y tng kt cc php ton lgic trn cc gi tr lgic:
x TRUE TRUE y x AND y TRUE FALSE TRUE FALSE TRUE FALSE x OR y TRUE TRUE NOT x FALSE TRUE TRUE FALSE UNKNOWN UNKNOWN UNKNOWN TRUE FALSE UNKNOWN TRUE

TRUE UNKNOWN UNKNOWN UNKNOWN UNKNOWN FALSE UNKNOWN FALSE FALSE FALSE

UNKNOWN UNKNOWN UNKNOWN TRUE TRUE

UNKNOWN UNKNOWN

FALSE UNKNOWN

16

FALSE FALSE

FALSE

FALSE

TRUE

Hnh 1: Bng chn tr cho logic ba gi tr Cc iu kin SQL nh xut hin trong cc mnh WHERE ca cc lnh select-from-where, p dng cho mi b trong mt quan h no y, v vi mi b, mt trong ba gi tr TRUE, FALSE, hoc UNKNOWN c sinh ra. Tuy nhin, ch c cc b m i vi n iu kin c gi tr TRUE s tr thnh mt phn ca cu tr li; cc b c UNKNOWN hoc FALSE nh l gi tr s b loi ra khi cu tr li. V d 9: Gi s chng ta c truy vn trn y trn quan h NHNVIN_DN SELECT * FROM NHNVIN_DN WHERE Sgi <=12 OR Sgi > 12 ; Mt cch trc quan, chng ta mong i nhn c mt bn sao ca quan h NHNVIN_DN, bi v mi nhn vin c th lm vic cho mt d n t hn hoc bng 12 gi hoc nhiu hn 12 gi. Tuy nhin, gi s rng c cc b ca NHNVIN_DN c gi tr NULL trong thnh phn Sgi. Khi c hai php so snh Sgi <= 12 v Sgi >12 c tnh l UNKNOWN. OR ca hai UNKNOWN l UNKNOWN. Nh vy, vi mi b c mt NULL trong thnh phn Sgi, mnh WHERE c tnh l UNKNOWN. Mt b nh vy khng c tr li nh mt phn ca cu tr li cho truy vn. Kt qu c a ra theo ngha ng n ca truy vn l tm tt c cc b NHNVIN_DN c Sgi khng NULL. 1.1.7 Sp th t d liu ra Chng ta c th yu cu rng cc b c mt truy vn to ra s c biu din trong mt th t sp xp. Th t c th da trn gi tr ca mt thuc tnh no , kt hp vi gi tr ca thuc tnh th hai, . nhn c d liu ra theo mt th t sp xp, chng ta thm vo lnh select-from-where mt mnh ORDER BY < danh sch cc thuc tnh >

17

Th t c ngm nh l tng dn nhng chng ta c th nhn d liu ra theo th t gim dn bng cch thm vo t kho DESC. Tng t, chng ta c th ch ra th t tng dn bng cch thm vo t kho ASC (ty chn). V d 10: nhn c cc Hm, Tn theo th t tng dn ca Tn ca tt c cc nhn vin trong n v c m s bng 5, ta c truy vn sau: SELECT Hm, Tn FROM NHNVIN WHERE MsV = 5 ORDER BY Tn ; 1.1.8 Cc hm thng dng trong SQL Trong SQL c mt s cc hm xy dng sn. Sau y l mt s hm thng dng. 1) Cc hm nhm: Hm AVG tr v gi tr trung bnh ca ct. V d: SELECT AVG(Lng) FROM NHNVIN; Hm MIN tr v gi tr nh nht ca ct. V d: SELECT MIN(Lng) FROM NHNVIN ; Hm MAX tr v gi tr ln nht ca ct. V d: SELECT MAX(Lng) FROM NHNVIN ; Hm SUM tr v tng cc gi tr ca ct. V d: SELECT SUM(Lng) FROM NHNVIN ; Hm COUNT tr v s lng cc bn ghi. V d: SELECT COUNT(*) FROM NHNVIN ; Vic s dng cc hm ny trong cc php ton nhm s ni n trong cc phn sau. 18

2) Cc hm x l cc chui k t Hm ASCI I, tr v gi tr m ASCII ca k t bn tri chui. V d: Print ASCII (Ti); tr v kt qu 84 (m ASCII ca T). Hm CHAR, chuyn i m ASCII sang k t. V d: Print CHAR(35) ; tr v kt qu k t # Hm UPPER, chuyn i chui sang kiu ch hoa. V d: Print UPPER(Nam); tr v kt qu NAM Hm LOWER, chuyn i chui sang kiu ch thng. V d: Print LOWER(NAM) ; tr v kt qu nam Hm LEN, tr v di ca chui. V d: Print LEN(NAM) ; tr v kt qu 3. Hm LTRIM, loi b cc khong trng bn tri ca chui. V d: Print LTRIM( NAM) ; tr v kt qu NAM. Hm RTRIM, loi b cc khong trng bn phi ca chui. V d: Print RTRIM(NAM ) ; tr v kt qu NAM. Hm LEFT(chui,n) tr v n k t bn tri ca chui. V d Print LEFT(NAM, 2) ; tr v kt qu NA. Hm RIGHT(chui,n) tr v n k t bn phi ca chui. V d Print LEFT(NAM, 1) ; tr v kt qu AM. Hm CHARINDEX (chui1, chui2) tr v v tr bt u ca chui 1 trong chui 2. V d: CHARINDEX(Tm,Hu Tm) tr v kt qu 4. 3) Cc hm thi gian Hm GETDATE() tr v ngy thng nm ca h thng. V d SELECT GETDATE() tr v kt qu: 2004-10-17 14:25:36.234 Hm DATEPART() tr v mt phn ca mt chui dng ngy thng y DATEPART(d,GETDATE()), tr v ngy DATEPART(m,GETDATE()), tr v thng

19

DATEPART(yy,GETDATE()), tr v nm .Cc tham s d,m,yy l nh dng ngy, thng, nm, Hm DATEDIFF (nh dng, Ngytrc, Ngysau) hiu s gia Ngy sau v Ngytrc Hm DAY tr v ngy, Hm MONTH tr v thng, Hm YEAR tr v nm 4) Cc hm ton hc Hm SQUARE tr v bnh phng ca mt biu thc. Hm SQRT tr v cn bc hai ca mt biu thc Hm ROUND tr v s lm trn ca mt biu thc 5) Cc hm chuyn i Hm CAST tr v gi tr c kiu d liu theo nh ngha. V d PRINT CAST (GETDATE() AS VARCHAR) tr v Oct 18 2004. Hm CONVERT chuyn i gi tr t kiu ny sang kiu khc.

1.2 CC TRUY VN BAO GM NHIU HN MT QUAN H


Sc mnh ca i s quan h l kh nng t hp hai hoc nhiu quan h thng qua cc php ni, tch, hp, giao v tr. Trong SQL c tt c cc php ton . 1.2.1 Tch v ni trong SQL SQL c mt cch n gin ghp cp cc quan h vo mt truy vn: lit k tng quan h trong mnh FROM. Sau , cc mnh SELECT v WHERE c th tham chiu n cc thuc tnh ca bt k quan h no bn trong mnh FROM. V d 11: Gi s chng ta mun bit tn ca nhn vin v tn n v ca ngi . tr li cho cu hi ny, chng ta cn hai quan h NHNVIN v NV. Ta c truy vn sau: SELECT Tn, TnV FROM NHNVIN, NV 20

WHERE NHNVIN.MsV = NV.MsV ; Truy vn ny i hi chng ta xem xt tt c cc cp b gi tr, mt t NV v b kia t NHNVIN. iu kin trn cc cp ny c ni r trong mnh WHERE: Thnh phn MsV trong cc b ny phi c gi tr nh nhau. Khi no chng ta tm thy mt cp b tho mn iu kin, chng ta a ra cc thuc tnh Tn ca b t quan h NHNVIN v thuc tnh TnV ca b t quan h NV nh mt phn ca cu tr li. Qu trnh ny c m t bng hnh v di y: Tn M sV
M sV TnV
C bng nhau ?

NV NHNVIN
Nu ng th a ra Nu ng th a ra

Hnh 2: minh ho truy vn ca v d 11. 1.2.2 Lm r ngha cc thuc tnh i khi chng ta i hi mt truy vn bao gm nhiu quan h v trong nhng quan h ny c hai hoc nhiu thuc tnh c cng tn. Nu nh vy, chng ta cn c cch ch r thuc tnh no trong s cc thuc tnh l c s dng. SQL gii quyt vn ny bng cch cho php ta t tn quan h v mt du chm ng trc thuc tnh. Nh vy, R.A tham chiu n thuc tnh A ca quan h R. Trong v d 11, hai quan h NHNVIN v NV c cc thuc tnh MsV trng tn. phn bit, trong mnh WHERE ta vit NHNVIN.MsV = NV.MsV Mt quan h, theo sau l mt du chm c cho php ngay c trong trng hp khi khng c s khng r ngha. V d, chng ta hon ton thoi mi khi vit truy vn c dng nh sau: SELECT NHNVIN.Tn, NV.TnV 21

FROM NHNVIN, NV WHERE NHNVIN.MsV = NV.MsV ; Kt qu ca truy vn 11 l:


Vn Nam Thanh Bng Sn Giang Hoa Gip Tn Nghincu Hnhchnh Hnhchnh Nghincu Nghincu Hnhchnh Lnho TnV Nghincu

1.2.3 Cc bin b Vic lm r ngha cc thuc tnh bng cch thm tn quan h vo u hot ng khi mt truy vn bao hm t hp nhiu quan h khc nhau. Tuy nhin, i khi chng ta cn i hi mt truy vn bao hm hai hoc nhiu b t cng mt quan h Chng ta c th lit k mt quan h R bao nhiu ln nh ta mun trong mt mnh FROM, nhng chng ta cn c cch tham chiu n tng ln c mt ca R. SQL cho php chng ta nh ngha i vi tng ln c mt ca R trong mnh FROM mt bdanh m chng ta s tham chiu n n nh l mt bin b. Mi ln s dng ca R trong mnh FROM c theo sau bng mt t kho AS v tn ca bin b. T kha AS l khng bt buc, c th c hoc khng. Trong phm vi ti liu ny, chng ta s b qua t kho AS. Trong cc mnh SELECT v WHERE, chng ta c th lm r ngha cc thuc tnh ca R bng cch thm vo trc chng mt bin b thch hp v mt du chm. Nh vy, mt bin b phc v nh l mt tn khc ca quan h R v c th c s dng trong v tr ca n khi chng ta mun. V d 13: Gi s chng ta mun a ra tn ca nhn vin v tn ca ngi gim st nhn vin . Nh vy, cc tn ny u c ly t quan h NHNVIN. S dng cc bin b nh cc b danh cho hai s dng ca NHNVIN, chng ta c th vit truy vn nh sau: SELECT NV.Tn, NV1.Tn 22

FROM NHNVIN NV, NHNVIN NV1 WHERE NV.MsNGS = NV1.M sNV; Chng ta nhn thy trong mnh FROM m t ca hai bin b, NV v NV1; mi bin b l mt b danh cho quan h NHNVIN. Cc bin b c s dng trong mnh SELECT tham chiu n cc thnh phn Tn ca hai b. Cc b danh ny cng c s dng trong mnh WHERE ni rng hai b t NHNVIN c biu din bi NV v NV1 c gi tr nh nhau trong cc thnh phn MsNGS v MsNV ca chng Kt qu ca truy vn 13
NV.Tn Vn Nam Thanh Bng Sn Giang Hoa NV1.Tn Nam Gip Bng Gip Nam Nam Bng

1.2.4 Php hp, php giao, php tr ca cc truy vn i khi chng ta mun t hp cc quan h bng cch s dng cc php ton tp hp ca i s quan h: hp, giao, tr. SQL cung cp cc php ton tng ng p dng cho cc kt qu ca cc truy vn vi iu kin l cc truy vn to ra cc quan h c cng danh sch cc thuc tnh v cc kiu thuc tnh. Cc t kho c s dng l UNION, INTERSECT v EXCEPT cho hp, giao v tr tng ng. Cc t nh UNION c s dng gia hai truy vn, v cc truy vn ny phi c t trong cp du ngoc n. V d 14: Gi s chng ta mun a ra MsNV ca cc nhn vin lm vic cho d n c MsDA =1 v cc nhn vin lm vic cho d n c M sDA = 2. S dng quan h NHNVIN_DN, ta vit truy vn nh sau: (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 1)

23

UNION (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 2) Lnh SELECT u a ra cc MsNV ca cc nhn vin lm vic cho d n c MsDA =1, lnh SELECT sau a ra cc MsNV ca cc nhn vin lm vic cho d n c MsDA = 2, hai tp hp ny hp vi nhau to thnh cu tr li cho truy vn.
Kt qu MsNV NV001 NV002 NV016 NV018 (cc b trng lp b loi b)

V d 15: Theo cch tng t, chng ta mun a ra MsNV ca cc nhn vin va lm vic cho d n c MsDA =1 va lm vic cho d n c M sDA = 2. S dng quan h NHNVIN_DN, ta vit truy vn nh sau: (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 1) INTERSECT (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 2)
Kt qu MsNV NV001 NV018

V d 16: Gi s chng ta mun a ra MsNV ca cc nhn vin lm vic cho d n c MsDA =1 nhng khng lm vic cho d n c M sDA = 2. S dng quan h NHNVIN_DN, ta vit truy vn nh sau: (SELECT MsNV

24

FROM NHNVIN_DN WHERE MsDA = 1) EXCEPT (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 2)
Kt qu MsNV (khng c)

1.3

CC TRUY VN CON

Trong SQL, mt truy vn c th c s dng trong nhiu cch khc nhau gip vic tnh gi tr ca truy vn khc. Mt truy vn l mt phn ca truy vn khc c gi l mt truy vn con. Cc truy vn con li c th c cc truy vn con v nh vy c th i xung rt nhiu mc. Chng ta c c hi nhn thy vic s dng truy vn con. Trong cc v d phn trn, chng ta xy dng cc truy vn hp, giao, tr bng cch ni hai truy vn con to nn truy vn y . C rt nhiu cch s dng cc truy vn con: 1. Cc truy vn con c th tr li mt hng n v c th so snh hng vi gi tr khc trong mnh WHERE. 2. Cc truy vn con c th tr li cc quan h v c th s dng cc quan h ny bng nhiu cch trong mnh WHERE. 3. Cc truy vn con c th c cc quan h ca chng xut hin trong mnh FROM ging nh cc quan h c lu gi c th. 1.3.1 Cc truy vn con to ra cc gi tr v hng Mt gi tr nguyn t c th xut hin nh mt thnh phn ca mt b c xem l mt v hng. Mt biu thc select-from-where c th to ra mt quan h c s cc thuc tnh tu v c s b gi tr tu trong quan h. Tuy nhin, thng thng chng ta ch quan tm n cc gi tr ca mt thuc tnh n. Hn na, i lc chng ta c th suy ra t thng tin v kho hoc t cc thng tin khc. Chng hn, chng ta c th so snh kt qu ca mt truy vn con nh vy vi mt hng hoc mt thuc tnh. 25

V d 17: Chng ta mun a ra Hm v Tn ca cc nhn vin trong n v c tn l Nghincu. Chng ta cn truy vn hai quan h: NHNVIN v NV. Bi v ch c quan h u c thng tin v Hm v Tn v ch c quan h th hai c cc tn ca n v. Thng tin c lin kt bng MsV. C nhiu cch khc nhau nhn vo truy vn ny. Chng ta ch cn quan h NV nhn c s MsV cho n v c tn l Nghin cu. Mi khi chng ta c n, chng ta c th truy vn quan h NHNVIN tm ra h m v tn ca cc nhn vin trong n v . Vn u tin l nhn c MsV. Chng ta c th vit n nh mt truy vn con v kt qu ca n s l mt gi tr n. C th s dng gi tr ny trong truy vn chnh t c kt qu mong mun: 1) 2) 3) 4) 5) 6) SELECT Hm, Tn FROM NHNVIN WHERE MsV = (SELECT MsV FROM NV WHERE TnV = Nghincu ;

Cc dng t 4) n 6) l truy vn con. Ch nhn vo truy vn con ny chng ta s thy rng kt qu s l mt quan h c mt thuc tnh l MsV v chng ta hy vng s tm thy ch mt b trong quan h ny, gi s l 5. Khi thc hin truy vn con ny, chng ta c th thc hin cc dng t 1) n 3) ca truy vn trn nh l gi tr 5 thay th truy vn con. Nh vy, truy vn chnh s c thc hin nh l SELECT Hm, Tn FROM NHNVIN WHERE MsV = 5 ; Kt qu ca truy vn l Hm v Tn ca cc nhn vin ca n v c tn l Nghin cu.

26

1.3.2 Cc iu kin c bao hm cc quan h C mt s cc php ton ca SQL m ta c th p dng cho mt quan h R v to ra mt kt qu lgic. Thng thng, quan h R s l kt qu ca mt truy vn con select-from-where. Mt s cc php ton (EXISTS, IN, ALL, ANY) s c gii thch y di dng n gin nht ca n, trong c cha mt gi tr v hng s. Trong hon cnh ny, R phi l mt quan h c mt ct 1. EXISTS R l mt iu kin c gi tr true khi v ch khi R khng rng. 2. s IN R c gi tr TRUE khi v ch khi s bng mt trong cc gi tr ca R. Tng t, s NOT IN R c gi tr TRUE khi v ch khi s khng bng gi tr no trong R. y, chng ta gi thit R l quan h c mt ct. 3. s > ALL R l ng khi v ch khi s ln hn mi gi tr trong quan h mt ct R. Tng t, c th thay du > bng mt trong cc php so snh khc. 4. s > ANY R l ng khi v ch khi s ln hn t nht l mt gi tr trong quan h mt ct R Cc php ton EXISTS, IN, ALL v ANY c th c ph nh bng cch t NOT trc biu thc. V d, NOT EXISTS R ng khi v ch khi R l rng. NOT s > ALL R l ng khi v ch khi s khng phi l gi tr max trong R v NOT s > ANY R l ng khi v ch khi s l gi tr min trong R. 1.3.3 Cc iu kin c bao hm cc b Mt b trong SQL c biu din bng mt danh sch cc gi tr v hng c t trong du ngoc. V d: (5,Nghincu, NV002, 2000-09-15) l mt b ca quan h NV. Nu mt b t c cng s thnh phn nh mt quan h R th c th so snh t v R. V d: t IN R hoc t<> ANY R (Ch rng khi so snh mt b vi cc thnh phn ca mt quan h R, chng ta phi so snh cc thnh phn theo th t ca cc thuc tnh ca R). V d 18: Gi s ta mun a ra Hm v tn ca cc nhn vin c lng cao nht trong tng n v. Ta vit truy vn sau: 1) SELECT Hm, Tn

27

2) FROM NHNVIN 3) WHERE (Lng, MsV) IN 4) 5) 6) (SELECT (MAX(Lng), M sV FROM NHNVIN GROUP BY M sV);

Truy vn ny bao gm mt truy vn chnh v mt truy vn con. Truy vn con kho st cc b ca quan h NHNVIN v a ra mt quan h gm cc b vi gi tr ca cc thuc tnh MAX(Lng) v MsV. Truy vn chnh, t dng 1) n dng 3) xem xt cc b ca quan h NHNVIN tm cc b c thnh phn Lng v MsV l mt trong cc b do truy vn con a ra. Vi mi b nh vy, Hm v Tn s c a ra v cho chng ta tp hp nhng nhn vin c lng cao nht trong tng n v nh chng ta mong mun.
Kt qu: Hm Phm Hong Bng Gip Tn Trn c Nam

1.3.4 Cc truy vn con tng quan vi nhau Cc truy vn con n gin nht c th c thc hin mt ln cho tt c, v kt qu c s dng trong truy vn mc cao hn. Vic s dng cc truy vn con lng nhau phc tp hn i hi truy vn con c thc hin nhiu ln, mi ln i vi mt php gn gi tr cho mt mc no trong truy vn con. Gi tr gn xut pht t mt bin b bn ngoi truy vn con. Mt truy vn con kiu nh vy c gi l mt truy vn con tng quan. Chng ta bt u bng mt v d. V d 19: Chng ta mun a ra Hm v Tn ca cc nhn vin c lng ln hn lng trung bnh ca n v ca h. Chng ta xem xt ln lt cc b ca quan h NHNVIN, vi mi b nh vy, chng ta i hi trong mt truy vn con liu gi tr ca Lng c ln hn trung bnh lng ca n v c m s ging nh gi tr ca thnh phn MsV ca b hay khng. Ton b truy vn c vit nh sau:

28

1) SELECT Hm, Tn 2) 3) 4) 5) 6) FROM NHNVIN NV WHERE (Lng > (SELECT (AVG(Lng) FROM NHNVIN WHERE MsV = NV.MsV;

Ging nh vi cc truy vn lng nhau khc, chng ta hy bt u t truy vn con trong cng, cc dng t 4) n 6). Nu NV.MsV dng 6 c thay bng mt s nguyn nh l 5, chng ta c th hiu n hon ton d dng nh l mt truy vn hi trung bnh lng ca n v c m s l 5. Truy vn con hin ti c khc mt t. Vn l ch chng ta khng bit MsV c gi tr nh th no. Tuy nhin, nh chng ta sp xp trn cc b ca truy vn ngoi t dng 1) n dng 3), mi b cung cp mt gi tr ca M sV. Sau chng ta thc hin truy vn t dng 4) n dng 6) vi gi tr ca M sV quyt nh chn tr ca mnh WHERE tri trn cc dng t 3) n 6). iu kin ca dng 3) l ng nu c mt n v c trung bnh lng nh hn lng ca b ang xt.
Kt qu Hm Sn Bng Gip Tn Trn c Nam Nguyn Phm Hong

Khi vit mt truy vn tng quan, iu quan trng l chng ta phi nhn thc c quy tc phm vi i vi cc tn. Ni chung, mt thuc tnh trong mt truy vn con thuc v mt trong cc bin b ca mnh FROM ca truy vn con nu mt quan h no ca bin b c thuc tnh trong lc ca n. Nu khng, chng ta tm truy vn trc tip ngay bn ngoi . Tuy nhin, chng ta c th sp xp cho mt thuc tnh thuc v mt bin b khc nu chng ta vit trc n mt bin b v mt du chm. iu l v chng ta a ra b danh NV cho quan h NHNVIN ca truy vn ngoi v v chng ta tham chiu n NV.M sV trong dng 6). Ch rng nu 29

hai quan h trong cc mnh FROM ca cc dng 2) v 5) l khc nhau, chng ta khng cn n b danh. D nhin, trong truy vn con, chng ta c th tham chiu trc tip n cc thuc tnh ca quan h c ch ra trong dng 2). 1.3.5 Cc truy vn con trong mnh FROM Mt cch dng khc i vi cc truy vn con l nh cc quan h trong mt mnh FROM. Trong danh sch FROM, thay cho mt quan h c lu gi, chng ta c th s dng mt truy vn con trong du ngoc. Bi v chng ta khng c tn cho kt qu ca mt truy vn con, chng ta phi cho n mt b danh bin b. Sau chng ta tham chiu n cc b trong kt qu ca mt truy vn con nh chng ta s tham chiu n cc b trong mt quan h bt k xut hin trong danh sch FROM. V d 20: Chng ta hy xem li vn ca v d 18, chng ta vit mt truy vn tm Hm v Tn ca cc nhn vin c lng cao nht trong cc n v. Gi s rng chng ta c mt quan h cha Max(lng) v MsV. Khi vic tm cc Hm v Tn s n gin hn bng cch tm trong quan h NHNVIN. Truy vn nh vy c dng 1) 3) 4) 5) SELECT Hm, Tn FROM NHNVIN GROUP BY MsV) NV1 WHERE (Lng, MsV) = NV1.(MAX(Lng),MsV); 2) FROM NHNVIN, (SELECT MAX(Lng), MsV)

Cc dng t 2) n 4) l mnh FROM ca truy vn ngoi. Trong mnh , ngoi quan h NHNVIN n cn c mt truy vn con. Truy vn ny a ra quan h c hai thuc tnh l MAX(Lng) v MsV. Tp hp c gn cho b danh l NV1 dng 4. dng 5), cc quan h NHNVIN v truy vn con c b danh NV1 c ni theo hai iu kin l Lng v MsV phi nh nhau. Kt qu l a ra c tp Hm v Tn ging nh trong v d 18.

30

1.3.6 Cc biu thc ni ca SQL Chng ta c th xy dng cc quan h bng cc php ni khc nhau p dng trn hai quan h. Cc cch ny gm tch, ni t nhin, ni tta, v ni ngoi. Kt qu c th l mt truy vn. V cc biu thc ny to ra cc quan h nn chng c th c s dng nh cc truy vn con trong mnh FROM ca mt biu thc select-from-where. Dng n gin nht ca mt biu thc ni l ni cho (cross join). Thut ng ny ng ngha vi tch cac hoc tch. V d, nu chng ta mun c tch cac ca hai quan h NHNVIN v NV. Chng ta c th ni NHNVIN CROSS JOIN NV ; v kt qu s l mt quan h c 13 ct, cha tt c cc thuc tnh ca NHNVIN v NV. Mi mt cp gm mt b ca NHNVIN mt b ca NV s l mt b ca quan h kt qu. Cc thuc tnh trong quan h tch c th c gi l R.A, trong R l mt trong hai quan h ni v A l mt trong cc thuc tnh ca n. Nu ch c mt trong cc quan h c thuc tnh l A th c th b R v du chm i. Trong hon cnh hin ti, bi v quan h NHNVIN v quan h NV c mt thuc tnh chung l MsV, nn trong quan h tch cn phi phn bit chng NHNVIN.MsV v NV.MsV, cc tn khc ca cc thuc tnh trong hai quan h l khc nhau nn khng cn c tn quan h v du chm trc. Tuy nhin, php ton tch l mt php ton t khi c s dng. Php ni tta l thun tin hn. Php ni ny gm t kho JOIN c t gia hai tn quan h R v S, sau chng l t kho ON v mt iu kin. ngha ca JOIN ...ON ... l php tnh tch R x S, sau l mt php chn theo iu kin i sau ON. V d 21: Gi s chng ta mun ni hai quan h NHNVIN v NV vi iu kin l cc b c ni l cc b tham chiu n cng mt m s n v. Nh vy, cc m s n v t c hai quan h phi nh nhau. Chng ta c th i hi truy vn ny l:
NHNVIN JOIN NV ON NHNVIN.MsV = NV.MsV;

31

Kt qu li l mt quan h vi 13 ct cng vi cc tn thuc tnh nh trn. Tuy nhin, by gi mt b t NHNVIN v mt b t NV kt hp vi nhau to thnh mt b kt qu ch khi hai b c m s n v nh nhau. Trong kt qu, mt ct l tha bi v mi mt b ca kt qu s cng gi tr trong c hai thnh phn MsV Nu chng ta lo lng vi s kin l php ni trn c mt thnh phn tha, chng ta c th s dng biu thc y nh l mt truy vn con trong mnh FROM v s dng mnh SELECT loi b cc thuc tnh khng mong mun. Nh vy, chng ta c th vit: SELECT <danh sch cc thuc tnh trong hai quan h nhng thuc tnh MsV ch xut hin mt ln> FROM NHNVIN JOIN NV ON NHNVIN.MsV = NV.MsV; nhn c mt quan h c 12 ct, l cc b ca quan h NHNVIN c m rng thm cc b ca NV 1.3.7 Ni t nhin (Natural Join) Php ni t nhin khc vi php ni tta ch: 1. iu kin ni l tt c cc cp thuc tnh t hai quan h c mt tn chung c so snh bng v khng c iu kin no khc. 2. Mt thuc tnh trong mi cp thuc tnh c so snh bng c chiu ra ngoi. (ngha l trong quan h kt qu khng c hai ct ging nhau). Php ni t nhin ca SQL ng x mt cch chnh xc theo cch . Cc t kho NATURAL JOIN xut hin gia cc quan h biu th php ni. V d 22: Gi s chng ta mun lm php ni t nhin ca hai quan h NV v NHNVIN. Kt qu s l mt quan h c lc cha thuc tnh MsV cng vi tt c cc thuc tnh xut hin trong c hai quan h. Biu thc NHNVIN NATURAL JOIN NV M t sc tch quan h mong mun.

32

1.3.8 Ni ngoi Ni ngoi l mt cch lm tng kt qu ca mt php ni bng cc b treo, n thm vo cc gi tr null. Trong SQL, chng ta c th ch r mt ni ngoi; NULL c s dng nh l gi tr null. V d 23: Gi s chng ta a ra Hm v Tn ca cc nhn vin cng nh Hm v Tn ca nhng ngi gim st h. Trn thc t, khng phi nhn vin no cng c ngi gim st trc tip, v vy i vi nhng ngi khng c ngi gim st trc tip hoc thng tin v ngi gim st ca h l khng xc nh (null). Nu mun hin th c nhng b nh vy, ta s dng ni ngoi SQL xem ni ngoi chun n thm vo cc b treo t hai pha ca cc i s ca chng l ni ngoi y (full outerjoin). C php nh sau: NHNVIN FULL OUTER JOIN NHNVIN ON MsNV = MsNGS; Kt qu ca php ton ny l mt quan h, trong c nhng b c n vo cc gi tr NULL do khng c gi tr ni tng ng. (Ch , trong php ni bnh thng khng c nhng b nh vy). Tt c cc loi ni ngoi c ni n trong cc php ton i s quan h u c sn trong SQL. Nu chng ta mun mt left- hoc right-outerjoin, ta thm vo t LEFT hoc RIGHT thch hp vo v tr ca t FULL. V d: NHNVIN LEFT OUTER JOIN NHNVIN ON MsNV = MsNGS; NHNVIN RIGHT OUTER JOIN NHNVIN ON MsNV = MsNGS; Tip theo, gi s ta mun mt ni ngoi t nhin thay v mt ni ngoi tta. Khi chng ta s s dng t kha NATURAL t vo trc t JOIN v b ON i. V d 24: Chng ta hy xem li v d 22, chng ta mun ni hai quan h NHNVIN v NV vi iu kin l cc thuc tnh MsV ca hai quan h l bng nhau. Nu chng ta sa i v d ny nh sau NHNVIN NATURAL FULL OUTER JOIN NV th chng ta s nhn c khng ch l cc b c to nn t cc b tham gia ni m cn c thm cc b c n vo cc gi tr NULL

33

T kho FULL c th c thay th bng LEFT hoc RIGHT trong php ni ngoi trn.

1.4

CC PHP TON QUAN H Y

Trc tin chng ta rng SQL s dng cc quan h nh l cc ti (bag) ch khng phi nh tp hp. iu c ngha l mt b c th xut hin nhiu ln trong mt quan h. 1.4.1 Loi b trng lp Nh ni n trn, khi nim quan h ca SQL khc vi khi nim quan h tru tng c trnh by trong m hnh quan h. Mt quan h l mt tp hp, khng th c nhiu hn mt bn sao ca mt b cho trc. Khi mt truy vn SQL to mt quan h mi, h thng SQL khng loi b cc trng lp. Nh vy, SQL tr li cho mt truy vn c th lit k nhiu ln cng mt b. Nh li rng mt nh ngha cho mt truy vn select-from-where ca SQL l nh sau: Chng ta bt u vi tch cac ca cc quan h c tham chiu n trong mnh FROM. Mi b ca tch c kim tra bng iu kin trong mnh WHERE v nhng b no qua c kim tra s c a cho d liu ra cho php chiu ph hp vi mnh SELECT. Php chiu ny c th sinh ra cng mt b cho kt qu t nhiu b khc nhau ca tch, v nu nh vy, mi bn sao ca kt qu s c in ra. Hn na, khng c g sai i vi mt quan h SQL c trng lp. Nu chng ta khng mun c s trng lp trong kt qu, ta c th tip theo sau t kho SELECT bng t kho DISTINCT. T ni vi SQL ch to ra mt bn sao cho mt b gi tr. Chng hn SELECT DISTINCT Lng FROM NHNVIN ; 1.4.2 Trng lp trong php hp, php giao v php tr Khng ging nh lnh SELECT gi gn s trng lp nh mc nh v ch loi b chng khi a vo t kho DISTINCT, cc php ton hp, giao v tr thng loi b s trng lp. Nh vy, cc ti c i thnh tp hp v 34

bn tp hp ca php ton c p dng. ngn nga vic loi b trng lp, chng ta phi vit sau cc php ton UNION, INTERSECT, EXCEPT t kho ALL. Nu chng ta lm nh vy th chng ta nhn c c php ti thng qua cc php ton ny. V d 26: Xt biu thc hp ca v d 14 nhng c thm vo t kho ALL: (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 1) UNION ALL (SELECT MsNV FROM NHNVIN_DN WHERE MsDA = 2)
Kt qu MsNV NV001 NV001 NV002 NV018 NV018

Ta thy by gi trong kt qu xut hin cc b trng nhau. Nu mt nhn vin lm vic cho c d n 1 v d n 2 th m s ca nhn vin xut hin trong kt qu hai ln. Cng nh i vi UNION, cc php ton INTERSECT ALL v EXCEPT ALL l giao v tr ca cc ti (bag). Nh vy, nu R v S l cc quan h th kt qu ca biu thc R INTERSECT ALL S l mt quan h trong s ln xut hin ca mt b t l s nh nht ca s ln xut hin ca b trong R v s ln xut hin ca b trong S. Kt qu ca biu thc R EXCEPT ALL S

35

l mt quan h trong s ln xut hin b t bng s ln xut hin ca n trong R tr i s ln xut hin ca n trong S vi iu kin hiu s ny l dng. 1.4.3 Nhm v s kt hp trong SQL Php ton nhm v kt hp trong i s quan h cho php ta phn chia cc b ca mt quan h thnh cc nhm da trn cc gi tr ca mt hoc nhiu thuc tnh trong cc b. Sau chng ta c th kt hp mt s cc ct khc ca quan h bng cch p dng php ton kt hp i vi cc ct . Nu c cc nhm th php kt hp s c thc hin ring r cho tng nhm. SQL cung cp mi kh nng ca php ton trn thng qua vic s dng cc php ton nhm trong mnh SELECT v mt mnh GROUP BY c bit. 1.4.4 Cc php ton nhm SQL s dng 5 php ton nhm SUM, AVG, MIN, MAX, v COUNT. Cc php ton ny c s dng bng cch p dng chng cho cc biu thc c gi tr v hng, thng l mt tn ct, trong mnh SELECT. C mt ngoi l l biu thc COUNT(*), biu thc ny m tt c cc b trong mt quan h c thit lp t mnh FROM v mnh WHERE ca truy vn. Hn na, chng ta c tu chn loi tr trng lp ra khi ct trc khi p dng php ton nhm bng vic s dng t kho DISTINCT. Nh vy, mt biu thc nh l COUNT(DISTINCT x) m s cc gi tr khc nhau trong ct x. Chng ta c th s dng cc php ton khc v tr ca COUNT y nhng biu thc nh SUM(DISTINCT x) thng khng c ngha my, bi v n yu cu ta tnh tng cc gi tr khc nhau trong ct x. V d 27: Truy vn sau y tm gi tr lng trung bnh ca tt c cc nhn vin: SELECT AVG(Lng) FROM NHNVIN ; Ch rng y khng c mnh WHERE. Truy vn ny xem xt ct Lng ca quan h NHNVIN, tnh tng cc gi tr tm c y, mt gi tr cho mi b (cho d nu b l trng lp ca mt vi b khc), v chia tng s cho s cc b. Nu khng c cc b trng lp th truy vn ny cho

36

lng trung bnh nh chng ta mong i. Nu c cc b trng lp, th mt gi tr lng trng lp n ln s c tnh n ln trong trung bnh. V d 28: Truy vn sau y: SELECT COUNT(*) FROM NHNVIN ; m s cc b trong quan h NHNVIN. Truy vn tng t: SELECT COUNT(Lng) FROM NHNVIN ; m s gi tr trong ct Lng ca quan h. Bi v cc gi tr trng lp khng b loi b khi chng ta chiu ln ct Lng trong SQL, tng m ny s ging nh tng m do truy vn vi COUNT(*) sinh ra. Nu chng ta mun chc chn rng ta khng m cc gi tr trng lp qu mt ln, chng ta c th s dng t kho DISTINCT trc thuc tnh nhm, nh: SELECT COUNT(DISTINCT Lng) FROM NHNVIN ; By gi mi lng s c m mt ln, khng cn quan tm n vic n xut hin trong bao nhiu b. 1.4.5 Nhm nhm cc b, chng ta s dng mnh GROUP BY, i sau mnh WHERE. Theo sau t kho GROUP BY l mt danh sch cc thuc tnh nhm. Trong hon cnh n gin nht, ch c mt tham chiu quan h trong mnh FROM, v quan h ny c cc b ca n c nhm theo theo cc gi tr ca chng trong cc thuc tnh nhm. D php ton nhm no c s dng trong mnh SELECT cng ch c p dng bn trong cc nhm.

37

V d 29: Vn tm trong quan h NHNVIN tng lng theo tng n v: SELECT MsV, SUM(Lng) FROM NHNVIN GROUP BY MsV ; Chng ta c th tng tng l cc b ca quan h NHNVIN c sp xp li v c nhm sao cho tt cc cc b i vi n v 1 l cng vi nhau, tt c cc b ca n v 4 l cng vi nhau, . Cc tng ca cc thnh phn Lng ca cc b trong tng nhm c tnh ton, MsV c a ra cng vi tng . Quan st v d 29 ta thy mnh SELECT c hai loi s hng: 1. Cc kt hp, mt php ton nhm c p dng cho mt thuc tnh hoc mt biu thc bao gm cc thuc tnh. Nh cp n, cc s hng ny c tnh gi tr trn c s tng nhm. Trong v d ny, SUM(Lng) l mt kt hp. 2.Cc thuc tnh, chng hn nh MsV trong v d ny, xut hin trong mnh GROUP BY. Trong mt mnh SELECT c cc php ton nhm, ch nhng thuc tnh no c cp n trong mnh GROUP BY mi c th xut hin nh cc thuc tnh khng nhm trong mnh SELECT. Khi cc truy vn c cha GROUP BY ni chung c c cc thuc tnh nhm v s kt hp trong mnh SELECT, v mt k thut khng cn thit c mt c hai. V d, chng ta c th vit: SELECT MsV FROM NHNVIN GROUP BY MsV; Truy vn ny s nhm cc b ca NHNVIN theo m s n v ca n v sau in ra m s n v cho mi nhm, khng cn quan tm n c bao nhiu b c cng m s n v. Nh vy, truy vn trn c cng kt qu nh

38

SELECT DISTINCT MsV FROM NHNVIN ; C th s dng mnh GROUP BY trong mt truy vn vi nhiu quan h. Cc truy vn nh vy c th hin bng dy cc bc sau y: 1. Tnh quan h R c biu din bng cc mnh FROM v WHERE. Nh vy, quan h R l tch cac ca cc quan h c ch ra trong mnh FROM v p dng php chn ca mnh WHERE i vi n. 2.Nhm cc b ca R theo cc thuc tnh trong mnh GROUP BY. 3. Kt qu l cc thuc tnh v cc kt hp ca mnh SELECT c to ra c nh l truy vn trn mt quan h c lu tr R. V d 30: Gi s chng ta mun a ra tn n v v s lng cc nhn vin trong tng n v. Chng ta cn ly thng tin t hai quan h: NHNVIN v NV. Chng ta bt u bng cch ni tta chng bng cch so snh bng cc m s n v t hai quan h. Bc ny cho chng ta mt quan h m trong mi b NV c cp vi cc b NHNVIN c m s n v ging vi m s n v ca n. By gi, chng ta c th nhm cc b c chn ca quan h ny theo tn ca n v. Cui cng, chng ta m s cc nhn vin trong tng nhm. Truy vn c vit nh sau: SELECT TnV, COUNT(*) FROM NHNVIN NV, NV V WHERE NV.MsV = V.MsV GROUP BY TnV ; Kt qu TnV Nghincu Hnhchnh Lnho COUNT(*) 4 3 1

39

1.4.6 Cc mnh HAVING Gi s rng chng ta khng mun tnh n tt c cc tn n v trong bng ca chng ta v d 30 trn. Chng ta c th hn ch cc b trc khi nhm theo cch c th lm rng cc nhm khng mong mun. V d, nu chng ta ch mun s cc nhn vin ca mt n v phi ln hn hoc bng 3. Khi , chng ta tip theo mnh GROUP BY mt mnh HAVING. Mnh HAVING bao gm t kho HAVING theo sau l mt iu kin v nhm. V d 31: Gi s chng ta mun in ra Tn n v v s nhn vin trong tng n v i vi nhng n v no c nhiu hn hoc bng 3 nhn vin. Chng ta c th thm vo v d 30 mnh HAVING COUNT(*) >= 3 ; Truy vn kt qu c cho nh di y: Kt qu TnV Nghincu Hnhchnh COUNT(*) 4 3

Chng ta phi nh mt s quy tc v cc mnh HAVING: . Mt php nhm trong mnh HAVING ch p dng i vi cc b ca nhm c kim tra. . Bt k thuc tnh no ca cc quan h trong mnh FROM u c th c nhm trong mnh HAVING, nhng ch c cc thuc tnh c th xut hin trong danh sch GROUP BY khng c nhm trong mnh HAVING (cng quy tc nh vi mnh SELECT). Mt s iu cn nh: * Th t ca cc mnh trong cc truy vn SQL: Cho n by gi chng ta gp tt c su mnh trong mt truy vn select-from-where: SELECT, FROM, WHERE, GROUP BY, HAVING v ORDER BY. Ch c hai mnh u l bt buc, nhng ta khng th s 40

dng mnh HAVING m khng c mnh GROUP BY. Bt k mnh ph thm no cng phi xut hin theo th t trn. * Nhm, Tp hp v Null: Khi cc b c gi tr Null, cn nh mt s quy tc sau: Gi tr Null c l i trong tp hp. N khng gp phn vo sum, average, hoc count hoc khng l min hoc max trong ct ca chng. V d, COUNT(*) lun lun l mt php m ca s cc b trong mt quan h, nhng COUNT(A) l s cc b vi gi tr ca thuc tnh A khng Null.

Mt khc, NULL c x l nh l mt gi tr thng thng trong mt thuc tnh nhm. V d, SELECT a, AVG(b) FROM R s to ra mt b vi NULL cho gi tr ca a v gi tr trung bnh ca b i vi cc b vi a =NULL, nu c t nht mt b trong R vi thnh phn a l NULL.

1.5 SA I C S D LIU
Ngoi dng truy vn SQL chun select-from-where, c mt s cc dng lnh khc khng tr li mt kt qu nhng lm thay i trng thi ca quan h. Trong phn ny chng ta s hng n cc dng lnh cho php ta - Chn cc b vo mt quan h - Xo mt s b ra khi quan h - Cp nht cc gi tr ca mt s thnh phn ca mt s b tn ti. Cc php ton nh vy gi l cc php ton sa i. 1.5.1 Chn Dng c bn ca lnh chn bao gm: 1. T kho INSERT TO 2.Tn ca mt quan h R 3.Mt danh sch cc thuc tnh ca quan h R t trong du ngoc 4.T kho VALUES

41

5.Mt biu thc b, ngha l mt danh sch cc gi tr c th c t trong du ngoc, mt gi tr cho mi thuc tnh trong danh sch im 3. Nh vy, dng chn c bn l INSERT INTO R(A1,A2,,An) VALUES (v1,v2,,vn) Mt b c to ra bng cch s dng gi tr vi cho thuc tnh Ai, vi i = 1,2,,n Nu danh sch ca cc thuc tnh khng bao hm tt c cc thuc tnh ca quan h R th b c to ra c cc gi tr ngm nh cho tt c cc thuc tnh b thiu. Gi tr ngm nh hay dng l NULL, nhng cng c th c cc tu chn khc. V d 32: 1) 2) INSERT INTO NV (MsV, TnV, MsNQL, Ngybtu) VALUES (8,Khoch, NV018, 1977-16-24);

Kt qu ca vic thc hin lnh ny l mt b vi bn thnh phn dng 2) s c chn vo quan h NV. V tt c cc thuc tnh ca NV c k n dng 1) nn khng cn phi thm vo cc thnh phn ngm nh. Cc gi tr trn dng 2) ph hp vi cc thuc tnh trn dng 1) theo th t cho trc, nh vy, K hoch tr thnh gi tr ca thuc tnh TnV. Nu chng ta cung cp tt c cc gi tr cho tt c cc thuc tnh ca quan h th chng ta c th b qua danh sch cc thuc tnh i theo sau tn quan h. V d, chng ta c th vit li lnh trn di dng INSERT INTO NV VALUES (8,Khoch, NV018, 1977-16-24); Tuy nhin, nu chng ta ly tu chn ny th ta phi chc chn rng th t ca cc gi tr l ging nh th t chun ca cc thuc tnh i vi quan h. Nu ta khng chc chn v th t chun i vi cc thuc tnh th tt nht l lit k chng trong mnh INSERT ta chn cho cc gi tr ca n trong mnh VALUES. Lnh INSERT n gin c m t trn ch t mt b vo mt quan h. Thay v s dng cc gi tr r cho mt b, chng ta c th tnh ton mt tp hp cc b c chn vo bng cch s dng mt truy vn con. Truy vn 42

con ny thay th t kho VALUES v biu thc b trong dng lnh INSERT c m t trn. V d 33: Gi s chng ta c mt quan h NV1 cha cc b gi tr cng kiu vi cc b trong quan h NV. Nu mun chn tt c cc b ca NV1 vo quan h NV ta vit lnh chn nh sau: 1) INSERT INTO NV 2) 3) SELECT * FROM NV1 ;

1.5.2 Xa Mt lnh xo bao gm 1. Cc t kho DELETE FROM 2.Tn ca mt quan h, R 3.T kho WHERE v 4. mt iu kin. Nh vy, dng ca php xo l DELETE FROM R WHERE < iu kin> ; Hu qu ca vic thc hin lnh ny l mi b tho mn iu kin s b xo khi quan h. V d 34: Chng ta c th xo khi quan h NV b gi tr (8,Khoch, NV018, 1977-16-24) bng lnh SQL sau y: DELETE FROM NV WHERE TnV = Khoch; Ch rng khng ging nh lnh chn v d 32, chng ta khng th ch ra mt cch n gin mt b s b xo. ng hn, chng ta phi m t b mt cch chnh xc bng mnh WHERE. V d 35 Lnh sau y 43

DELETE FROM NHNVIN WHERE Lng < 3000 ; s xo khi quan h NHNVIN tt c cc b gi tr c thnh phn Lng nh hn 3000. 1.5.3 Cp nht Lnh update trong SQL lm thay i mt s thnh phn ca cc b tn ti trong c s d liu. Dng tng qut ca lnh update l: 1.T kho UPDATE 2. Mt tn quan h, R 3.T kho SET 4. Mt danh sch cc cng thc, mi cng thc t mt thuc tnh ca quan h R bng mt gi tr ca mt biu thc hoc mt hng. 5. T kho WHERE v 6. Mt iu kin. Nh vy dng lnh update l UPDATE R SET < gn gi tr mi > WHERE < iu kin> ; Mi mt < gn gi tr mi > l mt thuc tnh, mt du bng v mt cng thc. Nu c nhiu hn mt php gn th chng c phn cch nhau bng du chm phy (;). Hu qu ca lnh ny l tm tt c cc b gi tr trong R tho mn iu kin. Mi b ny sau s c thay i bng cch tnh gi tr cc cng thc v gn cho cc thnh phn ca b vi cc thuc tnh tng ng ca R. Vd 36: Hy sa i TnSV ca cc b trong quan h NV c tn n v l Hnhchnh thnh tn mi l Khoch. Ta vit lnh UPDATE nh sau: 1) UPDATE NV 2) SET TnV = Khoch 3) WHERE TnV = Hnhchnh ; 44

Dng 3) kim tra rng c phi tn n v l Hnhchnh hay khng. Nu ng, dng 2) s thay th tn ny bng Khoch.

1.6 NH NGHA MT LC QUAN H TRONG SQL


Trong phn ny chng ta s tho lun v nh ngha d liu, mt phn ca ngn ng SQL cho php m t cc cu trc thng tin trong c s d liu. Ngc li, cc kha cnh SQL tho lun trc kia - cc truy vn v cp nhtthng c gi l thao tc d liu. Ch ca phn ny l m t cc lc ca cc quan h c lu gi. Chng ta s thy m t mt quan h (bng) mi, mt khung nhn nh th no. 1.6.1 Cc kiu d liu Trc tin chng ta a ra cc kiu d liu nguyn t c h thng SQL h tr. Mi thuc tnh phi c mt kiu d liu. 1. Cc xu k t c di thay i hoc c nh. Kiu CHAR(n) k hiu mt xu gm n k t. Nh vy, nu mt thuc tnh c kiu CHAR(n) th trong mt b bt k, thnh phn cho thuc tnh ny l mt xu gm n k t. VARCHAR(n) k hiu mt xu gm nhiu nht l n k t. Cc thnh phn cho cc thuc tnh thuc kiu ny s l mt xu c t 0 n n k t. SQL cho php cc p buc hp l gia cc gi tr ca cc kiu xu k t. Thng thng, cc xu c di c nh c thm vo cc du khong trng nu gi tr ca n nh hn di cho php. Khi so snh mt xu vi mt xu khc, cc du trng thm vo s c b qua. 2. Cc xu bit c di c nh hoc thay i. Cc xu ny tng t nh cc xu k t c di c nh hoc thay i, nhng gi tr ca chng l cc xu bit. BIT(n) k hiu cc xu bit c di n, trong khi BIT VARYING(n) k hiu cc xu bit c di nh hn hoc bng n. 3. Kiu BOOLEAN k hiu cc thuc tnh c gi tr l gic. Cc gi tr c th c ca thuc tnh thuc loi ny l TRUE, FALSE v UNKNOWN. 4. Kiu INT hoc INTEGER k hiu cc gi tr nguyn. Kiu SHORTINT cng k hiu cc gi tr nguyn nhng c s cc ch s t hn. 45

5. Cc s du phy ng c th biu din bng nhiu cch. Chng ta s dng kiu REAL hoc FLOAT (hai kiu ny cng ngha) i vi cc s du phy ng. chnh xc cao hn c th nhn c vi kiu DOUBLE PRECISION. SQL cng c cc kiu vi cc s thc du phy c nh. l kiu DECIMAL(n,d) cho php cc gi tr cha n ch s thp phn, vi v tr ca du chm thp phn c gi thit l v tr d k t bn phi sang. Kiu NUMERIC hu nh ng ngha vi DECIMAL. 6. Ngy v gi cng c th c biu din nh cc kiu DATE v TIME. Cc gi tr ca chng l cc xu k t dng c bit. Thc vy, chng ta c th bin i ngy v gi thnh kiu xu v ngc li. 1.6.2 Cc khai bo bng n gin Dng n gin nht ca khai bo mt lc quan h bao gm cc t kho CREATE TABLE sau l tn ca quan h v mt danh sch cc thuc tnh cng vi kiu ca chng c t trong du ngoc. V d 1.37 Lc quan h NV biu th trong SQL nh sau: 1) 2) 3) 4) 5) CREATE TABLE NV ( MsV INT, TnV VARCHAR(15), MsNQL CHAR(9), Ngybtu DATE );

Thuc tnh u tin, MsV l mt s nguyn. Thuc tnh th hai l xu k t c di nh hn hoc bng 15. Thuc tnh th ba l mt xu c di c nh gm 9 k. Nh vy nu c mt m s ngi qun l khng c 9 k t th n s c h thng a thm vo mt s khong trng, cn mt m s c qu 9 k t th s b cht bt i. Cui cng, thuc tnh Ngybtu kiu DATE. Trong SQL chun khng c kiu ny, chng ta thay n bng CHAR(10). 1.6.3 Sa i cc lc quan h - Chng ta c th loi b mt quan h R bng lnh SQL:

46

DROP TABLE R; Quan h R s khng cn l mt phn ca lc c s d liu v chng ta khng cn c th truy cp n cc b gi tr ca n na. - Thng thng chng ta hay sa i lc ca mt quan h tn ti hn l xo b mt quan h l mt phn ca c s d liu tn ti lu di. Nhng s sa i ny c thc hin bng mt lnh bt u vi t kho ALTER TABLE v tn ca quan h. Sau chng ta c nhiu tu chn, quan trng nht l 1. ADD sau l mt tn ct v kiu ca n. 2. DROP sau l mt tn ct. V d 38: Chng ta c th thm vo quan h NV Sinthoi v b i thuc tnh Ngybtu bng cc lnh sau: DROP TABLE NV ADD Sinthoi CHAR(10); DROP TABLE NV DROP Ngybtu ; Kt qu l quan h NV c thm vo mt thuc tnh Sinthoi, l mt xu k t c di c nh gm 10 k t. Trong quan h hin ti, cc b gi tr u c cc thnh phn i vi Sinthoi nhng chng ta bit rng khng c s in thoi no c t vo . Nh vy, gi tr ca mi thnh phn s l NULL. Sau y chng ta s thy c kh nng chn mt gi tr ngm nh thay cho NULL i vi cc gi tr khng bit. 1.6.4 Cc gi tr ngm nh Khi chng ta to ra hoc sa i cc b gi tr, i lc chng ta khng c cc gi tr cho tt c cc thnh phn. Nh v d trn, khi ta thm mt ct vo mt quan h, cc b gi tr ang tn ti s khng c gi tr cho thuc tnh v NULL c s dng thay th cho gi tr thc. Tuy nhin SQL cn cung cp kh nng chn gi tr ngm nh, mt gi tr xut hin trong ct mi khi khng cc gi tr khc c bit. Ni chung, bt k ch no chng ta khai bo mt thuc tnh v kiu d liu ca n chng ta c th thm vo t kho DEFAUL v mt gi tr thch hp.

47

Gi tr hoc l NULL hoc l mt hng. Mt s cc gi tr khc cng c h thng cung cp nh l thi gian hin ti, hoc mt tu chn. Vd 39 Xt v d 37 trn. Chng ta c th s dng k t ? lm ngm nh cho MsNQL, s dng 0000-00-00 lm ngm nh cho Ngybtu, ta vit nh sau: 4) MsNQL CHAR(9) DEFAULT ?, 5) Ngybtu DATE DEFAULT DATE0000-00-00 1.6.5 Cc ch s Mt ch s trn mt thuc tnh A ca mt quan h l mt cu trc d liu lm c hiu qu vic tm cc b gi tr c gi tr c nh i vi thuc tnh A. Cc ch s thng gip vi cc truy vn trong thuc tnh A ca chng ta c so snh vi mt hng, v d A = 3 hoc A<=3. Khi cc quan h l rt ln, vic qut tt c cc b ca quan h tm cc b tho mn mt iu kin cho trc tr nn rt tn km. V d, xt truy vn sau y: SELECT * FROM NHNVIN WHERE Tn = Thanh AND Ngysinh = 1965-08-23 ; Gi s c 10000 b NHNVIN, trong ch c 100 c tn l Thanh v c 10 b c tn l Thanh v ngy sinh l 1965-08-23. Mt cch vng v thc hin truy vn ny l nhn tt c 10000 b v kim tra iu kin ca mnh WHERE trn tng b. Mt cch c hiu qu hn l ch nhn 100 b c tn l Thanh v kim tra tng b xem n c phi l sinh vo 1965-08-23 hay khng. N s cn hiu qu hn nu ch nhn 10 b tho mn c hai iu kin ca mnh WHERE. iu s lm c nh k thut ch s (index). Gi s chng ta mun c mt ch s trn thuc tnh Tn i vi quan h NHNVIN, ta vit CREATE INDEX TnIndex ON NHNVIN(Tn);

48

Kt qu l mt ch s c tn l TnIndex s c to ra trn thuc tnh Tn ca quan h NHNVIN. T nay v sau, nhng truy vn SQL c ch ra mt Tn c th c b x l truy vn SQL thc hin theo cch l ch nhng b no ca NHNVIN vi Tn c ch r l c xem xt. Nh vy, thi gian cn tr li cho truy vn s gim xung rt nhiu. Thng thng, mt h qun tr c s d liu cho php ta xy dng mt ch s n trn nhiu thuc tnh. Kiu ch s ny ly cc gi tr ca nhiu thuc tnh v tm c cc b vi cc gi tr cho i vi cc thuc tnh ny. V d, chng ta c th khai bo mt ch s trn cc thuc tnh ca quan h NHNVINN_DN nh sau: CREATE INDEX MsNV); DA_NVIndex ON NHNVIN (MsDA,

Nu chng ta mun xo b ch s, ta s dng lnh sau: DROP INDEX <Tn ch s> V d: DROP INDEX TnIndex ;

1.6.6 Nhp mn v vic la chn cc ch s Vic la chn cc ch s i hi ngi thit k c s d liu phi cn bng nhiu yu t v trn thc t vic la chn ny l mt trong cc s kin chnh c nh hng n vic mt thit k c s d liu c chp nhn c hay khng. Hai s kin quan trng cn xt l: - S tn ti ca mt ch s trn mt thuc tnh lm tng nhanh tc ca cc truy vn trong c ch ra mt gi tr i vi thuc tnh, v trong mt s trng hp c th tng tc cc php ni lin quan n thuc tnh . - Mt khc, mi mt ch s c xy dng cho mt thuc tnh ca mt quan h no y lm cho cc php chn, xo v cp nht i vi quan h phc tp v tn thi gian hn. Vic la chn ch s l mt trong nhng phn kh nht ca vic thit k c s d liu v n i hi phi nh gi s trn ln c th ca cc truy vn v cc php ton khc s c trn c s d liu. Nu mt quan h c truy vn 49

thng xuyn hn l cp nht th cc ch s trn cc thuc tnh thng c ch ra trong truy vn l c ngha. Cc ch s c li vi cc thuc tnh c kh nng c so snh vi cc hng trong mnh WHERE ca cc truy vn. Cc ch s cng c li i vi cc thuc tnh thng xut hin trong cc iu kin ni.

1.7 KHUNG NHN (VIEW)


Cc quan h c nh ngha vi lnh CREATE TABLE tn ti thc s trong c s d liu. Nh vy, h thng SQL lu tr cc bng trong mt t chc vt l no . Chng l thng trc v tn ti lu di, ch b thay i khi thc hin lnh INSERT hoc cc lnh cp nht. C mt lp cc quan h khc ca SQL, gi l cc khung nhn, khng tn ti mt cch vt l. ng hn l chng c nh ngha bng mt biu thc ging nh mt truy vn. Cc khung nhn c th c truy vn nh l chng tn ti mt cch vt l, v trong mt s trng hp, chng ta c th sa i cc khung nhn. 1.7.1 Khai bo cc khung nhn Dng n gin nht ca mt nh ngha khung nhn l 1. 2. 3. Cc t kho CREATE VIEW, Tn ca khung nhn, T kho AS v

4. Mt truy vn Q. Truy vn ny l nh ngha ca khung nhn. Mi khi chng ta truy vn khung nhn, SQL ng x nh l Q c thc hin ti thi im v truy vn c p dng i vi quan h do Q sinh ra. Nh vy, mt khai bo khung nhn n gin c dng: CREATE VIEW < tn khung nhn> AS < nh ngha khung nhn>; Vd 40: Gi s chng ta mun c mt khung nhn l mt phn ca quan h NHNVIN, cha MsNV, Hm,Tn, Lng v MsV ca cc nhn vin c a ch l Hni. Chng ta c th nh ngha khung nhn ny bng:

50

1) 2) 3) 4)

CREATE VIEW NVHNI AS SELECT MsNV, Hm,Tn, Lng, MsV FROM NHNVIN WHERE ach = H ni ;

Theo nh ngha ny, tn ca khung nhn l NVHNI, cc thuc tnh ca khung nhn l MsNV, Hm,Tn, Lng, ach, MsV. nh ngha ca khung nhn l t dng 2 n dng 4). 1.7.2 Truy vn cc khung nhn Quan h NVHNI khng cha cc b theo ngha thng thng. ng hn l nu chng ta truy vn NVHNI, cc b thch hp s nhn c t bng c s NHNVIN, v vy truy vn c th c tr li. Kt qu l chng ta c th hi NVHNI hai ln cng mt truy vn v nhn c cc tr li khc nhau. L do l ch, mc d chng ta khng thay i nh ngha ca khung nhn NVHNI nhng bng c s NHNVIN c th b thay i trong thi gian gia hai ln truy vn. Vd 41 Chng ta c th truy vn khung nhn NVHNI nh th n l mt bng c lu gi, chng hn: SELECT Tn FROM NVHNI WHERE MsV = 4 ; nh ngha ca khung nhn NVHNI c s dng bin i truy vn trn thnh truy vn mi ch nhm n bng c s NHNVIN. Truy vn trn tng ng vi truy vn SELECT Tn FROM NHNVIN WHERE ach = H ni AND MsV = 4 ; V d 42 C th vit cc truy vn cha c bng ln khung nhn, chng hn: SELECT TnV, Tn

51

FROM NVHNI, NV WHERE NVHNI.MsV = NV.MsV Truy vn ny i hi tn ca n v v tn ca cc nhn vin c a ch ti H ni. Vd 43 Chng ta hy xt mt truy vn phc tp hn c s dng nh ngha mt khung nhn. CREATE VIEW NVV AS SELECT TnV, Tn FROM NHNVIN, NV WHERE NHNVIN.MsV = NV.MsV; Chng ta c th truy vn khung nhn ny nh th n l mt quan h c lu tr, v d SELECT Tn FROM NVV WHERE Tn = Thanh; Truy vn trn tng ng vi truy vn: SELECT Tn FROM NHNVIN, NV WHERE (NHNVIN.MsV = NV.MsV) AND (Tn = Thanh); 1.7.3 t tn li cc thuc tnh i khi chng ta thch t tn mi cho cc thuc tnh ca khung nhn. lm iu , chng ta ch ra cc thuc tnh ca khung nhn bng cch lit k chng trong cp du ngoc v t sau tn ca khung nhn trong lnh CREATE VIEW. V d, chng ta c th vit li nh ngha khung nhn v d 1.43 nh sau: CREATE VIEW NVV(Tnnv, Tnnhnvin) AS

52

SELECT TnV, Tn FROM NHNVIN, NV WHERE NHNVIN.MsV = NV.MsV; Hai khung nhn l hon ton nh nhau nhng cc thuc tnh c t tn li, Tnnv v Tnnhnvin thay cho TnV v Tn. 1.7.4 Sa i cc khung nhn Trong nhiu trng hp chng ta khng th thc hin mt lnh chn, xo hoc cp nht i vi mt khung nhn bi v khung nhn khng ging nh mt bng c s. Tuy nhin, i vi cc khung nhn tng i n gin, gi l cc khung nhn cp nht c, c kh nng chuyn i cp nht ca khung nhn thnh mt cp nht tng ng trn mt bng c s v php cp nht c th c thc hin i vi bng c s. iu kin khung nhn cp nht c l cc khung nhn c nh ngha bng php chn mt s thuc tnh t mt quan h R (quan h ny cng c th l mt khung nhn cp nht c). Hai im k thut quan trng: Mnh WHERE khng c bao hm R trong mt truy vn con Danh sch trong mnh SELECT phi cha cc thuc tnh sao cho vi mi b c chn vo khung nhn, chng ta c th in cc thuc tnh khc vo vi cc gi tr null hoc ngm nh thch hp v c mt b ca quan h c s s c to nn t b c chn vo ca khung nhn. V d 44 Gi s chng ta c gng chn vo khung nhn NVHNI mt b: INSERT INTO NVHNI VALUES (NV065, Nguyn nh, Thi, 4500, 4); Khung nhn NVHNI hu nh tho mn cc iu kin cp nht c ca SQL bi v khung nhn ch yu cu mt s thnh phn ca cc b ca bng c s NHNVIN. Ch c mt vn l v thuc tnh ach ca bng NHNVIN khng phi l mt thuc tnh ca khung nhn, b gi tr m chng ta chn vo NHNVIN s c gi tr NULL ch khng phi l H

53

ni nh l gi tr ca n cho ach. B gi tr ny khng tho mn iu kin l a ch ca nhn vin l H ni. Nh vy, lm cho khung nhn NVHNI cp nht c, chng ta s thm thuc tnh ach cho mnh SELECT ca n, mc d r rng l a ch nhn vin l H ni. nh ngha li ca khung nhn NVHNI l: 1) 2) 3) 4) CREATE VIEW NVHNI AS SELECT MsNV, Hm,Tn, Lng, ach,MsV FROM NHNVIN WHERE ach = H ni ;

Sau , chng ta vit lnh chn vo khung nhn cp nht cNVHNI nh sau: INSERT INTO NVHNI VALUES (NV065, Nguyn nh, Thi, 4500, 4); Thc hin lnh chn, chng ta to ra mt b ca NHNVIN sinh t mt b ca khung nhn c chn vo khi nh ngha khung nhn c p dng cho NHNVIN. Cc thuc tnh khc khng xut hin trong khung nhn chc chn tn ti trong b NHNVIN c chn vo. Tuy nhin chng ta khng th suy ra gi tr ca chng. Trong kt qu b mi ca NHNVIN phi c trong cc thnh phn i vi mi thuc tnh ny cc gi tr mc nh thch hp hoc NULL hoc mt ngm nh no c khai bo cho thuc tnh. Chng ta cng c th loi b ra khi mt khung nhn cp nht c. Lnh xo, cng nh lnh chn, c thc hin thng qua mt quan h nn R v gy ra vic loi b mt b ca R gy ra b c xo ca khung nhn. Vd 45: Gi s chng ta mun xo khi khung nhn cp nht c NVHNI tt c cc b c tn cha t an. Ta c th vit lnh xo nh sau: DELETE FROM NVHNI WHERE Tn LIKE %an% ; 54

Lnh xo ny c chuyn thnh mt lnh xo tng ng trn bng c s NHNVIN; ch khc ch l iu kin nh ngha khung nhn NVHNI c thm vo cc iu kin ca mnh WHERE. Kt qu l lnh xo nh sau: DELETE FROM NHNVIN WHERE Tn LIKE %an% AND ach = H ni; Tng t, mt lnh cp nht trn mt khung nhn cp nht c c thc hin thng qua quan h nn. Nh vy lnh cp nht khung nhn c tc dng cp nht tt c cc b ca quan h nn sinh ra cc b c cp nht trong khung nhn. V d 46: Lnh cp nht khung nhn UPDATE NVHNI SET Lng = 4500 WHERE MsNV = NV002; c chuyn thnh lnh cp nht bng c s: UPDATE NHNVIN SET Lng = 4500 WHERE MsNV = NV002 AND ach = H ni; Loi cp nht mt khung nhn cui cng l loi b n. Lnh cp nht ny c th thc hin d khung nhn c cp nht c hay khng. Lnh DROP thng thng l: DROP VIEW NVHNI ; Ch rng lnh ny xo nh ngha ca khung nhn v vy chng ta khng th tip tc truy vn hoc cp nht i vi khung nhn ny na. Tuy nhin, vic xo b mt khung nhn khng lm nh hng n mt b no ca quan h nn NHNVIN. Ngc li DROP TABLE Movie

55

s khng ch xo b bng NHNVIN, n cng s lm cho khung nhn NVHNI tr nn v dng bi v mt truy vn s dng n s tham chiu n mt quan h NHNVIN khng tn ti. 1.7.5 Gii thch cc truy vn c cha cc khung nhn gii thch cc truy vn khung nhn, chng ta ln theo cch mt truy vn c cha khung nhn c x l nh th no. T tng c bn c minh ho hnh v di y (hnh 3). Mt truy vn Q c biu th bng cy biu thc trong i s quan h. Cy biu thc ny s dng cc quan h l cc khung nhn lm l. Trong hnh v cy c hai l, l cc khung nhn V v W. gii thch Q theo thut ng ca cc bng c s, chng ta tm cc nh ngha ca V v W. Cc nh ngha ny cng c biu th bng cc cy biu thc trong i s quan h. Trong hnh 3 ( bn phi) chng ta thay cc l V v W bng cc nh ngha ca cc khung nhn . Cy kt qu l mt truy vn trn cc bng c s tng ng vi truy vn gc trn cc khung nhn.

Q V W

Hnh 3: Thay th cc nh ngha khung nhn cho cc tham chiu khung nhn V d 47: Ta xt khung nhn v truy vn c nh ngha nh sau: 1) 2) 3) 4) CREATE VIEW NVHNI AS SELECT MsNV, Hm,Tn, Lng, MsV FROM NHNVIN WHERE ach = H ni ;

56

Mt cy biu thc cho truy vn nh ngha khung nhn ny c ch ra hnh 4.

MsNV, Hm,Tn, Lng, MsV

ach = Hni

NHNVIN Hnh 4: Cy biu thc cho khung nhn NVHNI Truy vn v d 41 c dng SELECT Tn FROM NVHNI WHERE MsV = 4 ; Cy biu thc cho truy vn ny c ch ra hnh 5

Tn

M sV = 4

NVHNI Hnh 5: Cy biu thc cho truy vn v d 41

57

Ch rng l ca cy ny biu din khung nhn NVHNI. T , chng ta gii thch truy vn bng cch thay th cy truy vn ca NVHNI vo v tr ca NVHNI trong cy biu thc ca truy vn. Kt qu, chng ta c cy biu thc nh sau:

Tn M sV = 4 MsNV, Hm,Tn, Lng, MsV ach = Hni


NHNVIN

Cy truy vn ny l mt gii thch chp nhn c ca truy vn. Tuy nhin n c din t bng cch phc tp khng cn thit. H thng SQL s p dng cc bin i i vi cy ny lm cho n ging nh biu thc cy i vi truy vn SELECT Tn FROM NHNVIN WHERE ach = Hni AND MsV = 4 ; Chng hn, chng ta c th a php chiu
MsNV, Hm,Tn, Lng, MsV

ln

trn php chn M sV = 4 v vic thay i khng lm nh hng n kt qu ca biu thc. Sau , chng ta c hai php chiu lin tip, u tin chiu trn MsNV, Hm,Tn, Lng, MsV, sau chiu trn Tn. R rng ln chiu th nht l tha, chng ta c th loi b n. Nh vy chng ta c th thay th hai php chiu bng bng mt php chiu trn Tn. Hai php chn cng c th c kt hp li. Ni chung, c th thay th hai php chn lin tip bng mt php chn vi php ton AND ca cc iu kin ca chng. Cy biu thc kt qu l:

58

Tn M sV = 4 AND ach = Hni


NHNVIN l cy nhn c t truy vn: SELECT Tn FROM NHNVIN WHERE ach = H ni;

1.8 TNG KT CHNG I


1- SQL: Ngn ng SQL l ngn ng truy vn chnh cho cc h c s d liu quan h. Chun hin ti c gi l SQL-99 hoc SQL3 2- Cc truy vn Select-From-Where: Dng truy vn SQL ph bin nht c dng select-from-where. N cho php ta ly tch ca nhiu quan h (mnh FROM), p dng mt iu kin i vi cc b ca kt qu (mnh WHERE) v sinh ra cc thnh phn mong mun (mnh SELECT). 3- Truy vn con: Cc truy vn Select-From-Where cng c th c s dng nh cc truy vn con bn trong mt mnh WHERE hoc mnh FROM ca mt truy vn khc. Cc php ton EXIST, IN, ALL, v ANY coa th c s dng din t cc iu kin c gi tr Lgic v cc quan h l kt qu ca mt truy vn con trong mnh WHERE. 4- Cc php ton tp hp trn cc quan h: Chng ta c th ly hp, giao, hoc tr ca cc quan h bng cch ni cc quan h hoc ni cc truy vn xc nh cc quan h, vi cc t kha UNION, INTESECT v EXCEPT tng ng. 5- Cc biu thc ni: SQL c cc php ton nh NATURAL JOIN c th p dng cho cc quan h nh cc truy vn hoc xc nh cc quan h trong mt mnh FROM.

59

6- Cc gi tr Null: SQL cung cp mt gi tr NULL c bit, n xut hin trong cc thnh phn ca cc b khng c gi tr c th cho chng. Cc php ton s hc v logic khng dng c vi NULL. Vic so snh mt gi tr bt k vi NULL, thm ch gi tr so snh l NULL, s cho gi tr UNKNOWN. Gi tr UNKNOWN i x trong cc biu thc c gi tr logic nh l na ng gia TRUE v FALSE. 7- Ni ngoi: SQL cung cp mt php ton OUTER JOIN. Php ton ny ni cc quan h v to ra kt qu c cha cc b treo t mt hoc c hai quan h tham gia php ni. Cc b treo c n thm cc gi tr NULL trong kt qu. 8- M hnh ti (bag) ca cc quan h: SQL xem cc quan h nh cc ti cc b ch khng phi tp hp cc b. Chng ta c th p buc vic loi b cc b trng lp bng t kha DISTINCT, trong khi t kha ALL cho php kt qu l mt ti trong hon cnh cc ti khng phi l ngm nh. 9- Php nhm: Cc gi tr xut hin trong mt ct ca quan h c th c tnh tng (kt hp li) bng cch s dng mt trong cc t kha SUM, AVG, MIN, MAX hoc COUNT. Cc b c th c phn nhm trc kt hp vi cc t kha GROUP BY. Mt s nhm c th b loi b vi mt mnh HAVING. 10-Cc lnh cp nht: SQL cho php chng ta lm thay i cc b trong mt quan h. Chng ta c th INSERT (chn cc b mi), DELETE (xa cc b), UPDATE(thay i mt s b tn ti) bng cch vit cc lnh SQL s dng cc t kha . 11- nh ngha d liu: SQL c cc lnh m t cc phn t ca mt lc c s d liu. Lnh CREATE TABLE cho php chng ta m t lc cho cc quan h c lu gi (gi l cc bng), ch ra cc thuc tnh v kiu ca chng v cc gi tr ngm nh. 12- Sa i cc lc : Chng ta c th thay i din mo ca mt lc c s d liu bng mt lnh ALTER. Cc thay i ny bao gm vic thm v loi b cc thuc tnh cc lc quan h v thay i cc gi

60

tr ngm nh lin kt vi mt thuc tnh hoc mt min. Chng ta cng c th s dng lnh DROP loi b hon ton cc quan h hoc cc thnh phn lc khc. 13- Cc ch s: Trong khi khng phi l mt phn ca chun SQL, cc h thng SQL thng mi cho php m t cc ch s trn cc thuc tnh; cc ch s ny l tng tc mt s truy vn v cp nht cha c t ca mt gi tr i vi thuc tnh c ch s ha. 14- Khung nhn: Mt khung nhn l mt nh ngha v vic mt quan h (khung nhn) c xy dng t cc bng c lu gi trong c s d liu nh th no. Cc khung nhn c th c truy vn ging nh chng l cc bng c lu gi, v mt h thng SQL sa i cc truy vn v mt khung nhn sao cho truy vn c thay th thnh truy vn trn bng c s c s dng nh ngha khung nhn.

MT S BI TP
Gi s chng ta c c s d liu sau(xem PH LC 2): Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) I. Vit cc truy vn sau: 1) Tm s model, tc, v kch c a cng ca tt c cc my PC c gi thp hn $1200. 2) Lm nh cu 1) nhng thay tn ct speed bng megahertz v ct hd bng gigabyte 3) Tm cc nh sn xut cc my in 4) Tm s model, kch c b nh v kch c mn hnh ca cc my xch tay (Laptop) c gi trn $2000

61

5) a ra tt c cc b trong quan h Printer vi cc my in mu. Ch rng color l thuc tnh c gi tr lgic 6) a ra s model, tc v kch c ca a cng i vi cc my PC coa a DVD 12X hoc 16X v gi thp hn $2000. Bn c th xem thuc tnh rd nh l thuc tnh c gi tr l chui k t. II Vit cc truy vn 1) a ra nh sn xut v tc ca cc Laptop vi a cng t nht l 30 gigabyte. 2) a ra s model v gi ca tt c cc sn phm (vi mi kiu) do nh sn xut B lm ra 3) a ra nhng nh sn xut bn cc Laptop nhng khng bn cc my PC 4) Tm cc kch c a cng c trong hai hoc nhiu hn PC 5) a ra cc cp model PC c cng tc v b nh (ram). Mt cp ch c lit k mt ln ngha l lit k (i,j) nhng khng lit k (j,i). 6) a ra cc nh sn xut sn xut t nht l hai loi my tnh khc nhau (PC hoc Laptop) vi tc t nht l 1000. III. Trong phn bi tp ny, bn phi s dng t nht mt truy vn con trong cu tr li v vit mi truy vn trong hai cch khc nhau.

1) a ra nhng ngi sn xut cc PC c tc t nht l 1200. 2) a ra cc my in c gi cao nht. 3) a ra cc my xch tay c tc thp hn tc ca cc PC 4) a ra s model ca cc mc (PC, laptop, printer) vi gi cao nht. 5) a ra nh sn xut ca my in mu c gi thp nht. 6) a ra cc nh sn xut ca cc PC c b x l nhanh nht trong s cc PC c dung lng RAM b nht. IV. S dng hm nhm

62

1) Tm tc trung bnh ca cc PC. 2) Tm tc trung bnh ca cc Laptop c gi trn $2000 3) Tm gi trung bnh ca cc PC do nh sn xut A lm ra 4) Tm gi trung bnh ca cc PC v laptop do nh sn xut D lm ra 5) a ra gi trung bnh ca cc PC i vi mi tc khc nhau. 6) i vi mi nh sn xut, hy a ra kch c trung bnh ca mn hnh ca cc laptop. 7) a ra cc nh sn xut lm ra t nht l 3 model my PC khc nhau. 8) a ra kch c trung bnh ca cc a cng ca cc PC i vi cc nh sn xut lm ra cc my in. IV. Sa i c s d liu.

1) S dng hai lnh INSERT lu tr vo c s d liu s kin model PC 1100 c nh sn xut C lm ra, c tc 1800, RAM 256, a cng 80, 20x DVD v bn vi gi $2499. 2) Chn vo cc s kin kim tra rng vi mi PC c mt laptop vi cng nh sn xut, tc , RAM v a cng, mt mn hnh 15 inch, mt s model ln hn 1100, v mt gi trn $500. 3) Loi b tt c PC vi a cng nh hn 20 gigabyte. 4) Loi b tt c cc laptop ca cc nh sn xut khng sn xut my in. 5) Nh sn xut A mua nh sn xut B. Thay i tt c cc sn phm do B sn xut thnh ra do A sn xut. 6) Vi mi PC, hy lm tng gp i dung lng ca RAM v thm a vo cng 20 gigabyte. 7) Vi mi laptop do nh sn xut B lm ra hy thm vo kch c mn hnh 1 inch v gim gi $100. VI. M t c s d liu. Vit cc khai bo sau:

1) Mt lc thch hp cho quan h Product 2) Mt lc thch hp cho quan h PC 63

3) Mt lc thch hp cho quan h Laptop 4) 5) 6) Mt lc thch hp cho quan h Printer. Mt ty chn cho lc Printer t 4) loi b thuc tnh Color Mt ty chn cho lc Laptop 3) thm v thuc tnh cd. Gi s gi tr mc nh ca thuc tnh ny l none nu laptop khng c CD reader

64

CHNG II: CC RNG BUC V CC TRIGGER


Trong chng ny chng ta s nhm vo cc kha cnh ca SQL cho php chng ta to ra cc phn t tch cc. Mt phn t tch cc l mt biu thc hoc mt lnh c vit mt ln, c lu tr vo c s d liu v ch i phn t thc hin vo nhng lc thch hp. Thi gian hnh ng c th l khi mt s kin no xy ra, chng hn khi chn mt b gi tr vo mt quan h c th hoc c th l khi c s d liu thay i sao cho mt iu kin c gi tr logic tr thnh ng. Mt trong nhng vn nghim trng i mt vi nhng ngi vit chng trnh ng dng cp nht c s d liu l thng tin mi c th sai theo nhiu cch, chng hn do cc li chnh t hoc chp li khi nhp d liu bng tay. Mt cch n gin nht m bo rng vic sa i c s d liu khng cho php cc b gi tr khng thch hp trong cc quan h l vit cc chng trnh ng dng sao cho mi lnh chn, xo v cp nht lin kt vi cc kim tra cn thit m bo tnh ng n. Cc i hi v tnh ng n lun lun phc tp v chng thng lp li; cc chng trnh ng dng phi thc hin cc kim tra nh nhau sau mi ln sa i. Tht may mn, SQL cung cp nhiu k thut biu th cc rng buc ton vn nh l mt phn ca lc c s d liu. Trong chng ny chng ta s nghin cu cc phng php c bn. Trc tin chng ta ni n cc rng buc kho, trong mt thuc tnh hay mt tp thuc tnh c khai bo nh l mt kho i vi quan h. Tip theo, chng ta xem xt mt dng ca ton vn tham chiu c gi l cc rng buc kho ngoi, chng i hi rng mt gi tr trong mt (hoc cc) thuc tnh ca mt quan h cng phi xut hin nh l mt gi tr ca mt (hoc cc) thuc tnh ca mt quan h khc. Sau , chng ta xem xt cc rng buc trn cc thuc tnh, cc b v cc quan h. Chng ta s nhm vo cc rng buc gia cc quan h c gi l cc khng nh (asertion). Cui cng, chng ta tho lun v trigger, l mt dng ca phn t tch cc c gi vo hot ng trn cc s kin c th no , chng hn nh chn vo mt quan h c th.

65

2.1

KHA V KHA NGOI

Rng buc quan trng nht trong mt c s d liu l khai bo mt (hoc mt tp) thuc tnh no to nn mt kho cho mt quan h. Nu mt tp cc thuc tnh S l mt kho cho mt quan h R th hai b bt k ca R phi khc nhau ti t nht l mt thuc tnh trong tp S. Nh vy, nu R c mt kho c khai bo th trong R khng c c cc b trng lp. Ging nh cc rng buc khc, mt rng buc kho c m t bn trong lnh CREATE TABLE ca SQL. C hai cch tng ng m t cc kho: s dng t kho PRIMARY KEY hoc t kho UNIQUE. Tuy nhin, mt bng ch c th c mt kho chnh nhng c nhiu m t unique. SQL cng s dng thut ng kho kt hp vi rng buc ton vn tham chiu. Cc rng buc ny, c gi l cc rng buc kho ngoi, khng nh rng mt gi tr xut hin trong thnh phn kho ngoi ca mt quan h cng phi xut hin trong thnh phn kho chnh ca mt quan h khc. 2.1.1 M t kho chnh Mt quan h ch c th c mt kho chnh. C hai cch m t kho chnh trong lnh CREATE TABLE. 1- Chng ta c th mt thuc tnh l kho chnh khi thuc ny c lit k trong lc quan h. Theo cch ny, chng ta thm vo t kho PRIMARY KEY sau thuc tnh v kiu ca n. 2- Chng ta c th thm vo danh sch cc mc c m t trong lc mt m t ph thm ni rng mt (hoc mt tp) thuc tnh c th to nn kho chnh. Theo cch ny, chng ta to ra mt phn t mi trong danh sch cc thuc tnh bao gm t kho PRIMARY KEY v mt danh sch cc thuc tnh to nn kho t trong cc du ngoc. Ch rng, nu kho cha nhiu hn mt thuc tnh, chng ta cn s dng cch 2. Tc ng ca vic m t mt tp thuc tnh S l kho chnh i vi quan h R l:

66

1. Hai b gi tr trong R khng th bng nhau trn mi thuc tnh trong tp S. Mi chn hoc cp nht mt b gi tr m vi phm quy tc ny s gy ra vic h qun tr c s d liu loi b hnh ng gy ra s vi phm. 2. Cc thuc tnh trong S khng c php c gi tr NULL. V d 2.1 Xt lc quan h NV. Kho chnh ca quan h ny l M sV. V vy chng ta c th thm s kin ny vo dng m t MsV: 1) CREATE TABLE NV 2) (TnV VARCHAR(15), 3) MsV INT PRIMARY KEY, 4) MsNQL CHAR(9) 5) Ngybtu DATE) ; Theo cch 2, chng ta c th s dng mt nh ngha kho chnh tch ring. Sau dng 5 ca v d 2.1, ta thm vo m t ca kho chnh v chng ta khng cn phi m t n dng 2. Ta c m t lc nh sau: 1) CREATE TABLE NV 2) (TnV VARCHAR(15), 3) MsV INT, 4) MsNQL CHAR(9), 5) Ngybtu DATE, 6) PRIMARY KEY(M sV);

C hai cch m t nh trn u chp nhn c bi v kho chnh ch gm mt thuc tnh. Tuy nhin, trong hon cnh kho chnh c nhiu hn mt thuc tnh chng ta phi dng cch m t th hai. V d, nu ta m t lc quan h NHNVIN_DN c kho chnh l cp thuc tnh MsNV, MsDA, sau danh sch cc thuc tnh ta s thm vo dng sau: PRIMARY KEY(MsNV, MsDA) 2.1.2 Cc kho c m t vi UNIQUE Mt cch khc m t kho l s dng t kho UNIQUE. T kho ny c th xut hin ng ch m PRIMARY KEY c th xut hin: hoc i sau mt thuc tnh v kiu ca n, hoc nh l mt mc ring bn trong lnh

67

CREATE TABLE. ngha ca m t UNIQUE gn ging nh ngha ca m t PRIMARY KEY. C hai im khc, l: 1- Chng ta c th c nhiu m t UNIQUE cho mt bng nhng ch c mt kho chnh. 2- Trong khi PRIMARY KEY ngn cm cc gi tr NULL trong cc thuc tnh ca kho th UNIQUE li cho php. Hn na, quy tc hai b gi tr khng th bng nhau trong mi thuc tnh ca tp thuc tnh c m t bng UNIQUE c th b vi phm nu mt hoc nhiu thnh phn cho php c gi tr NULL. Trn thc t n cn cho php hai b c NULL trong mi thuc tnh tng ng ca kho UNIQUE. V d 2.2: Chng ta c th vit li m t trong v d 2.1 bng cch s dng UNIQUE nh sau: 1) CREATE TABLE NV 2) (TnV VARCHAR(15), 3) MsV INT UNIQUE, 4) MsNQL CHAR(9) 5) Ngybtu DATE); Tng t, chng ta c th thay dng 6) trong cch m t th hai bng 6) UNIQUE(M sDV); 2.1.3 Lm c hiu lc cc rng buc kho Trong phn ni v ch s, chng ta bit rng mc d chng khng phi l mt phn ca chun SQL, mi phin bn SQL c mt cch to ra cc ch s nh l mt phn ca nh ngha lc c s d liu. Thng thng, ch s c xy dng trn kho chnh h tr kiu truy vn c ch ra mt gi tr i vi kho chnh. Chng ta cng c th xy dng cc ch s trn cc thuc tnh khc c m t l UNIQUE. Sau khi to ra ch s, nu mt mnh WHERE ca truy vn cha mt iu kin c to nn t mt kho v mt gi tr c th, b gi tr ph hp s c tm thy rt nhanh chng m khng phi tm kim qua tt c cc b gi tr ca quan h. Nhiu phin bn SQL cung cp mt lnh to ch s bng cch s dng t kho UNIQUE m t mt

68

thuc tnh l kho ti cng thi im n to ra mt ch s trn thuc tnh . V d, lnh CREATE UNIQUE INDEX MsNVIndex ON NHNVIN(MsNV); to ra ch s ng thi m t rng buc v tnh duy nht trn thuc tnh M sNV ca quan h NHNVIN. By gi chng ta hy xem xt h thng SQL s lm c hiu lc mt rng buc kho nh th no. V nguyn tc, rng buc phi c kim tra mi mt ln chng ta lm thay i c s d liu. Tuy nhin, mi rng buc kho i vi quan h R c th b vi phm ch khi R b thay i. Trn thc t, mt lnh xo cc b gi tr ra khi quan h R khng th gy ra mt vi phm, ch c php chn v php cp nht l c th. Nh vy, thng thng h thng SQL kim tra rng buc kho ch khi xut hin mt php chn hoc mt php cp nht i vi quan h. Nu c ch s trn thuc tnh (hoc cc thuc tnh) c m t l kho th h thng SQL lm hiu lc rng buc kho mt cch rt hiu qu. Tht vy, nu c sn ch s th khi ta chn mt b gi tr vo quan h hoc cp nht mt thuc tnh kho trong mt b no y, h thng dng ch s kim tra rng c hay khng c sn mt b vi cng gi tr trong thuc tnh (hoc cc thuc tnh) c m t l kho. Nu c, h thng phi ngn nga s thay i xy ra. Nu khng c ch s trn cc thuc tnh kho th vn cn kh nng lm c hiu lc mt rng buc kho. Vic sp xp quan h theo gi tr kho s gip vic tm kim ca h thng. Tuy nhin, nu khng c s h tr tm kim th h thng phi xem xt ton b quan h tm kim mt b gi tr vi gi tr kho cho trc. Qu trnh l cc k tn thi gian v c th lm cho vic sa i cc quan h ln ca c s d liu tr nn khng th. 2.1.4 M t cc rng buc kho ngoi Mt loi rng buc quan trng th hai trn lc c s d liu l gi tr i vi mt s thuc tnh no phi c ngha.

69

Trong SQL, chng ta c th khai bo mt (hoc cc) thuc tnh ca mt quan h l kho ngoi tham chiu n mt (hoc cc) thuc tnh ca mt quan h th hai (c th cng mt quan h). Khai bo c ngha l: 1. Cc thuc tnh c tham chiu ca quan h th hai phi c tuyn b l UNIQUE hoc PRIMARY KEY i vi quan h ca chng. Ngc li, chng ta khng th khai bo kho ngoi. 2. Cc gi tr ca kho ngoi xut hin trong quan h th nht cng phi xut hin trong cc thuc tnh c tham chiu ca mt b no y. Chnh xc hn, gi s c mt kho ngoi F tham chiu mt tp thuc tnh G ca mt quan h no . Gi s mt b t ca quan h th nht c cc gi tr xc nh trong tt c cc thuc tnh ca F; gi danh sch cc gi tr ca t trong cc thuc tnh l t[F]. Khi , trong quan h c tham chiu phi c mt b s no y ph hp vi t[F] trn cc thuc tnh G, ngha l s[G] = t[F]. Cng nh i vi kho chnh, chng ta c hai cch khai bo mt kho ngoi. a) Nu kho ngoi ch gm mt thuc tnh, chng ta c th tip sau tn v kiu ca n bng mt khai bo rng n tham chiu thuc tnh no y ca mt bng no y (thuc tnh ny phi l kho chnh hoc duy nht). Khai bo c dng: REFERENCE < bng>(<thuc tnh>) b) Cch th hai, chng ta c th thm vo danh sch cc thuc tnh trong mt lnh CREATE TABLE mt hoc nhiu khai bo pht biu rng mt tp thuc tnh l mt kho ngoi. Sau chng a ra tn bng v cc thuc tnh c kho ngoi tham chiu n ca bng . Cc thuc tnh ny phi l cc thuc tnh kho. Khai bo c dng nh sau: FOREIGN KEY (<cc thuc tnh>) REFERENCE < Tnbng> (cc thuc tnh c tham chiu n) V d 2.3 Gi s chng ta mun m t quan h DN (MsDA, TnDA, aimDA, MsV) 70

c kho chnh l MsDA v kho ngoi MsV tham chiu thuc tnh MsV ca quan h NV. Chng ta c th c hai cch m t nh sau: cch 1: CREATE TABLE DN (TnDA VARCHAR(15), MsDA INT PRIMARY KEY, aimDA VARCHAR(15), MsV INT REFERENCES NV(MsV)); cch 2: CREATE TABLE DN (TnDA VARCHAR(15), MsDA INT PRIMARY KEY, aimDA VARCHAR(15), MsV INT, FOREIGN KEY(MsV) NV(MsV));

REFERENCES

Ch rng thuc tnh c tham chiu MsV trong quan h NV phi l kho ca quan h . ngha ca vic m t kho ngoi l ch mi khi mt gi tr xut hin trong thnh phn MsV ca mt b ca quan h DN th gi tr cng phi xut hin trong thnh phn MsV ca mt b gi tr no ca quan h NV. C mt ngoi tr l thnh phn MsV ca mt b c th ca quan h DN c th c gi tr NULL, khi s khng c i hi rng NULL xut hin nh gi tr ca thnh phn MsV ca NV (bi v MsV l kho chnh). 2.1.5 Duy tr ton vn tham chiu Chng ta nhn thy lm th no m t mt kho ngoi v chng ta cng bit rng m t ko theo vic mt tp gi tr (khc NULL) i vi cc thuc tnh kho ngoi cng phi xut hin trong cc thuc tnh ca quan h c tham chiu. Rng buc ny s c duy tr th no khi c xy ra vic cp nht c s d liu? C ba cch sau y:

a) Chnh sch ngm nh: Loi b cc vi phm

71

SQL c mt chnh sch ngm nh l mi cp nht vi phm rng buc ton vn tham chiu s b h thng loi ra. V d, xt v d 2.3, trong i hi rng mt gi tr ca MsV trong quan h DN cng l mt gi tr ca MsV trong NV. Cc hnh ng sau y s b h thng loi b: - Chng ta c gng chn vo quan h DN mt b gi tr mi m gi tr ca thuc tnh MsV ca n l khc NULL v khng l gi tr ca thuc tnh MsV ca bt k mt b gi tr no ca quan h NV. Php chn b h thng loi b v b gi tr s khng bao gi c chn vo quan h - Chng ta c gng cp nht mt b ca quan h DN thay i thnh phn MsV thnh mt gi tr khc NULL m khng l thnh phn ca bt k b gi tr no ca quan h NV. Php cp nht b loi b v b khng c thay i. - Chng ta c gng loi b mt b gi tr ca quan h NV m gi tr ca thuc tnh MsV ca n xut hin nh mt gi tr ca thuc tnh MsV ca mt hay nhiu b gi tr ca quan h DN. Php xo b loi b v b vn cn trong NV. - Chng ta c gng cp nht mt b ca quan h NV theo cch lm thay i gi tr ca thuc tnh MsV v gi tr c ca MsV l gi tr ca thuc tnh MsV trong mt b ca quan h DN. H thng loi b s thay i v NV vn nh c. b) Chnh sch Cascade C mt cch khc iu khin vic xo v cp nht i vi mt quan h c tham chiu gi l chnh sch cascade. Mt cch trc quan, cc thay i i vi cc thuc tnh c tham chiu c bt chc kho ngoi. Di chnh sch cascade, khi chng ta loi b mt b gi tr ca quan h c tham chiu, duy tr ton vn tham chiu, h thng s b cc b gi tr tng ng ca quan h tham chiu. Cc cp nht cng c thc hin mt cch tng t. Nu chng ta thay i gi tr ca kho chnh trong mt b ca quan h c tham chiu, th cc gi tr ca cc thuc tnh tng ng (kho ngoi) trong cc b gi tr ca quan h tham chiu cng c thay i theo. 72

c) Chnh sch Set-Null Mt cch tip cn khc gii quyt vn l thay i gi tr ca kho ngoi ca cc b b loi b hoc b cp nht thnh NULL. Cch ny gi l chnh sch Set-Null. Cc tu chn c th c chn i vi cc php cp nht v xo mt cch c lp v chng c lit k ra cng vi khai bo ca kho ngoi. Chng ta khai bo chng bng ON DELETE hoc ON UPDATE theo sau l SET NULL hoc CASCADE. V d 2.4 Chng ta hy sa i khai bo ca quan h DN(TnDA, MsDA, aimDA, MsV) trong v d 2.3 ch r iu khin xo v cp nht trong quan h NV. Ta c khai bo nh sau 1) CREATE TABLE DN 2) (TnDA VARCHAR(15), 3) MsDA INT PRIMARY KEY, 4) aimDA VARCHAR(15), 5) MsV INT REFERENCES NV(MsDA)), 6) ON DELETE SET NULL 7) ON UPDATE CASCADE ); Khai bo trn c ngha l mi khi ta xo mt b gi tr ca quan h NV ta s lm cho gi tr ca thuc tnh MsVca quan h DN tham chiu n n tr thnh NULL. Mi khi ta cp nht gi tr MsV ca mt b gi tr ca quan h NV, ta s lm thay i ca MsV trong b gi tr ca quan h DN tham chiu n n. Gi tr c cp nht ca MsV trong quan h NV l gi tr mi ca MsV trong DN. 2.1.6 Lm chm vic kim tra rng buc. Xt v d 2.3, trong MsV trong DN l kho ngoi tham chiu n MsV ca NV. Nu ta thc hin chn mt b gi tr vo quan h DN

73

INSERT INTO DN VALUES (DA08, 25, H ni,6); th chng ta s gp rc ri bi v khng c b gi tr no ca quan h NV c gi tr 6 cho thuc tnh MsV. Nh vy, ta vi phm rng buc kho ngoi. Mt sa i c th c l trc tin hy chn vo quan h DN mt b gi tr khng c thuc tnh MsV: INSERT INTO DN (TnDA, MsDA, aimDA) VALUES (DA08, 25, H ni); Thay i ny trnh vi phm rng buc bi v b ny c chn vo vi gi tr null cho MsV v NULL trong mt kho ngoi khng i hi phi kim tra s tn ti ca bt k gi tr no trong ct c tham chiu n. Sau chng ta chn vo quan h NV mt b gi tr mi c gi tr cho thuc tnh MsV l 6, cui cng ta cp nht quan h DN Studio bng lnh: UPDATE DN SET MsV = 6 WHERE TnDA = DA08; Nu ta khng sa i quan h NV trc th lnh update ny cng s vi phm rng buc kho ngoi. Tuy nhin, trong mt s trng hp ta khng th sp xp mt cch khn ngoan cc bc sa i c s d liu nh vy. Trong SQL gii quyt kh khn trn ta cn hai im: 1. Trc tin ta cn c kh nng nhm nhiu lnh SQL (hai php chn, mt vo DN v mt vo NV) vo mt n v gi l mt giao tc (transaction). 2. Sau chng ta cn mt cch ni vi h thng SQL khng kim tra cc rng buc cho n sau khi ton b giao tc c hon thnh. thc hin im 2 chng ta cn bit mt s chi tit:

74

a) Mi rng buc (kho, kho ngoi hoc rng buc khc) c th c khai bo DEFERABLE hoc NOT DEFERABLE. Khai bo NOT DEFERABLE l ngm nh v c ngha l mi mt ln xut hin mt sa i c s d liu, rng buc c kim tra ngay sau nu php sa i i hi rng n c kim tra. Tuy nhin nu ta khai bo mt rng buc l DEFERABLE, th chng ta c la chn bo n ch i cho n khi mt giao tc hon thnh trc khi kim tra rng buc. b) Nu mt rng buc l chm c (deferable), th chng ta cng c th khai bo n l INITIALLY DEFERED hoc INITIALLY IMMEDIATE. Trong trng hp th nht vic kim tra s c tr hon n ht ca giao tc hin ti, tr khi chng ta bo h thng dng vic tr hon rng buc ny. Nu m t INITIALLY IMMEDIATE, vic kim tra s c lm trc mi sa i, nhng bi v rng buc l tr hon c, chng ta c quyn la chn quyt nh kim tra chm v sau. V d 2.5 Hnh di y biu th m t ca DN c ci tin cho php vic kim tra rng buc kho ngoi c tr hon cho n sau mt giao tc. Ta cng m t MsV l UNIQUE vi mc ch l n c th c cc rng buc kho ngoi ca cc quan h khc tham chiu n. CREATE TABLE DN (TnDA VARCHAR(15), MsDA INT PRIMARY KEY, aimDA VARCHAR(15), MsV INT UNIQUE REFERENCES NV(MsDA)), DEFERABLE INITIALLY DEFERRED); C hai im cn ch : - C th t tn cho cc rng buc - Nu mt rng buc c mt tn, v d MyConstraint, th chng ta c th i mt rng buc chm t immediate thnh deferred bng lnh SQL SET CONSTRAINT MyConstraint DEFERRED ; 75

v chng ta c th o ngc x l bng cch i t DEFERRED trn thnh IMMEDIATE.

2.2 CC RNG BUC TRN CC THUC TNH V CC B


Chng ta xem xt cc rng buc kho, chng buc cc thuc tnh no y phi c cc gi tr khc bit trn tt c cc b gi tr ca mt quan h. Chng ta cng xem xt cc rng buc kho ngoi, chng bt tun theo rng buc tham chiu gia cc thuc tnh ca hai quan h. By gi chng ta s xem xt mt loi rng buc quan trng th ba: chng l hn ch cc gi tr c th xut hin trong cc thnh phn i vi mt s thuc tnh. Cc rng buc ny c th c din t bng mt trong hai cch sau: 1. Mt rng buc trn thuc tnh trong nh ngha ca lc quan h ca n, hoc 2. Mt rng buc trn mt b gi tr. Rng buc ny l mt phn ca lc quan h, khng lin quan n bt k thuc tnh no ca n. 2.2.1 Cc rng buc Not-Null Mt rng buc n gin gn kt vi mt thuc tnh l NOT NULL. Hiu qu ca n l cm cc b m trong gi tr ca thuc tnh ny l NULL. Rng buc c m t bng cc t kho NOT NULL i sau m t ca thuc tnh trong lnh CREATE TABLE V d 2.6: Gi s quan h DN i hi thuc tnh MsDV l xc nh. Khi ta c m t ca thuc tnh trong dng 5) ca v d 2.4 c thay i nh sau: 5) MsV INT REFERENCES NV(MsV) NOT NULL Thay i ny c nhiu h qu, chng hn: - Chng ta khng th thm vo quan h Studio mt b gi tr bng cch ch ra ba thuc tnh TnDA, MsDA, aimDA bi v b thm vo s c gi tr NULL cho thnh phn MsV.

76

. Chng ta khng th s dng chnh sch set-null trong hon cnh ging nh dng 6) ca v d 2.4, bi v n yu cu h thng gn c gi tr NULL cho MsV bo ton rng buc kho ngoi. 2.2.2 Cc rng buc kim tra (CHECK) da trn thuc tnh Cc rng buc phc tp hn c th gn vi mt m t thuc tnh bng t kho CHECK sau l mt iu kin phi tho mn i vi mi gi tr ca thuc tnh ny. iu kin c t trong du ngoc. Trn thc t, mt rng buc CHECK da trn thuc tnh ging nh mt hn ch n gin trn cc gi tr, nh l vic lit k cc gi tr hp thc hoc l mt bt phng trnh s hc. Tuy nhin, v nguyn tc, iu kin c th l bt k ci g c th i sau WHERE trong mt truy vn SQL. iu kin ny c th tham chiu n thuc tnh b rng buc bng cch s dng tn ca thuc tnh trong biu thc ca n. Tuy nhin, nu iu kin tham chiu n cc quan h khc hoc cc thuc tnh khc ca quan h th quan h phi c a vo trong mnh FROM ca mt truy vn con (ngay c nu quan h c tham chiu n l mt quan h cha thuc tnh b kim tra). Mt rng buc CHECK da trn thuc tnh c kim tra khi mt b no y nhn mt gi tr mi cho thuc tnh ny. Gi tr mi c th c a vo bng mt sa i i vi b hoc c th l mt phn ca b c thm vo. Nu gi tr mi vi phm rng buc th php sa i b loi b. Chng ta s thy trong v d 2.7, rng buc CHECK da trn thuc tnh s khng c kim tra nu mt sa i c s d liu khng lm thay i gi tr ca thuc tnh gn vi rng buc. Hn ch c th dn n rng buc tr nn b vi phm. Trc tin ta hy xt mt v d n gin v kim tra da trn thuc tnh. V d 2.7 Gi s chng ta mun i hi rng cc M sV ch gm c mt ch s. Ta c th sa i dng 5) ca v d 2.4, mt m t ca lc i vi quan h DN nh sau: 4) MsDA INT REFERENCES NV(MsDA) CHECK (MsDA < 10)

77

Vic cp n cc thuc tnh khc ca quan h hoc cc quan h khc trong iu kin l c php. lm iu , trong iu kin phi c truy vn con. Nh chng ta ni, iu kin c th l bt k ci g c th i sau WHERE trong mt lnh select-from-where ca SQL. Tuy nhin, chng ta cn nhn thy rng vic kim tra rng buc ch c kt hp vi thuc tnh cn kim tra ch khng phi vi mi quan h hoc thuc tnh k ra trong rng buc. Kt qu l mt iu kin phc tp c th tr thnh sai nu mt phn t no khc vi thuc tnh cn kim tra thay i V d 2.8 Chng ta c th gi thit rng ta c th thay th mt rng buc ton vn tham chiu bng mt rng buc CHECK da trn thuc tnh i hi s tn ti ca gi tr c tham chiu. Sau y l mt s c gng sai lm nhm thay th i hi rng gi tr ca MsDA trong mt b (TnDA, MsDA, aimDA, MsV) ca quan h DN phi xut hin trong thnh phn MsV ca mt b no y ca quan h NV. Gi s dng 5) ca v d 2.4 c thay th bng: 4) MsV INT CHECK (MsV IN(SELECT MsV FROM NV) Lnh ny l mt rng buc CHECK da trn thuc tnh hp l nhng ta hy xem xt hiu qu ca n. Nu ta c gng chn mt b mi vo DN v b c gi tr ca MsV khng l m s ca mt n v no c, khi php chn b loi b.

Nu ta c gng cp nht thnh phn MsV ca mt b DN, v gi tr mi khng phi l mt gi tr ca thuc tnh MsV trong mt b no c ca NV, php cp nht b loi b. Tuy nhin, nu chng ta thay i quan h NV bng cch loi b mt b gi tr, thay i l khng nhn thy i vi rng buc CHECK trn. Nh vy, php loi b c chp nhn mc d rng buc CHECK da vo thuc tnh trn MsV by gi b vi phm.

78

2.2.3 Cc rng buc kim tra (CHECK)da trn b gi tr. khai bo mt rng buc trn cc b ca mt bng R, khi chng ta nh ngha bng bng lnh CREATE TABLE, chng ta c th thm vo danh sch cc khai bo thuc tnh, kho v kho ngoi t kho CHECK theo sau l mt iu kin t trong du ngoc. iu kin ny c th l bt c ci g c th xut hin trong mnh WHERE. N c ci t nh l mt iu kin v mt b trong bng R, v cc thuc tnh ca R c th c tham chiu n bng tn trong biu thc ny. Tuy nhin, cng nh i vi cc rng buc CHECK da trn thuc tnh, iu kin cng c th cp n cc quan h khc hoc cc b gi tr khc ca cng quan h R trong cc truy vn con. iu kin ca mt rng buc CHECK da trn b c kim tra mi khi mt b c chn vo R v mi khi mt b ca R c cp nht v c nh gi i vi b c cp nht hoc b mi c chn vo. Nu iu kin l sai i vi b th rng buc b vi phm v php chn hoc php cp nht sinh ra s vi phm s b loi b. Tuy nhin, nu iu kin cp n mt quan h no (thm ch n chnh R) trong mt truy vn con, v mt thay i i vi quan h ny gy ra vic iu kin tr thnh sai i vi mt b no ca R th kim tra khng ngn cm thay i . Nh vy, ging nh mt CHECK da trn thuc tnh, mt CHECK da trn b l khng thy c i vi cc quan h khc. Mc d cc kim tra da trn b c th gm cc iu kin rt phc tp nhng nn trnh trng hp bi v n c th b vi phm di cc iu kin no y. Nu cc kim tra da trn b ch cha cc thuc tnh ca b cn kim tra th n lun lun ng. V d 2.9 Chng ta vit lnh CREATE TABLE i vi quan h GIOVIN, trong ta thm vo mt rng buc: nu mt gio vin l nam gii th tn ca anh ta khng c bt u bng Ms.: 1) CREATE TABLE GIOVIN ( 2) Tn CHAR(30) PRIMARY KEY, 3) ach VARCHAR(255), 4) Giitnh CHAR(1), 79

5) Ngysinh DATE, 6) CHECK (Giitnh = F OR Tn NOT LIKE Ms.%)); Dng 6) trn m t rng buc. iu kin ca rng buc ny l ng i vi cc gio vin n hoc cc gio vin khc c tn khng bt u bng Ms.. Cc b lm cho iu kin tr thnh sai l cc b c gii tnh l nam v tn bt u bng Ms..

2.3 SA I CC RNG BUC


C th thm, sa i, hoc loi b cc rng buc ti mi thi im. Cch din t cc sa i ph thuc vo vic rng buc lin quan n mt thuc tnh, mt b gi tr, mt bng hoc mt lc c s d liu. 2.3.1 t tn cho cc rng buc sa i hoc xo mt rng buc ang tn ti iu cn thit l rng buc phi c mt tn. lm iu , chng ta t trc rng buc t kho CONSTRAINT v mt tn cho rng buc ny. V d 2.10: Tn CHAR(30) CONSTRAINT Tnlkha PRIMARY KEY, Giitnh CHAR(1) CONSTRAINT Gi trGT CHECK (gender IN (F, M)), CONSTRAINT Tnng CHECK (Giitnh = F OR Tn NOT LIKE Ms.%), 2.3.2 Sa i cc rng buc trn cc bng Trong phn trc chng ta ni rng ta c th chuyn vic kim tra mt rng buc t tc khc (immediate) sang chm (deferred) vi lnh SET CONSTRAINT. Cc thay i khc i vi cc rng buc c thc hin vi lnh ALTER TABLE. Chng ta cng s dng lnh ny trong phn trc thm hoc loi b cc thuc tnh. Cc lnh cng c th c s dng sa i cc rng buc. ALTER TABLE c s dng i vi c hai kim tra: kim tra da trn thuc tnh v kim tra da trn b. Chng ta c th dng mt rng buc vi t kho

80

DROP v tn ca rng buc b dng. Chng ta cng c th thm mt rng buc vi t kho ADD, tip theo l rng buc c thm vo. Tuy nhin, cn rng chng ta ch c th thm mt rng buc vo mt bng khi rng buc tho mn i vi trng thi hin ti ca bng. V d 2.11: Ba lnh sau y lm dng cc rng buc i vi quan h GIOVIN: ALTER TABLE GIOVIN DROP CONSTRAINT Tnlkha ; ALTER TABLE GIOVIN DROP CONSTRAINT GitrGT ; ALTER TABLE GIOVIN DROP CONSTRAINT Tnng ; By gi, gi s ta mun khi phc li cc rng buc , ta vit cc lnh sau: ALTER TABLE GIOVIN ADD CONSTRAINT Tnlkha PRIMARY KEY (Tn); ALTER TABLE GIOVIN ADD CONSTRAINT GitrGT CHECK (Giitnh IN (F, M)); ALTER TABLE GIOVIN ADD CONSTRAINT RightTitle CHECK (Giitnh = F OR Tn NOT LIKE Ms.% ; Cc rng buc ny tr thnh cc rng buc da trn b ch khng cn l cc rng buc da trn thuc tnh.

2.4 CC RNG BUC MC LC V CC TRIGGER


Cc dng phn t tch cc mnh m nht trong SQL khng kt hp vi cc b hoc cc thnh phn ca cc b c th. Cc phn t ny c gi l cc khng nh (assertion) v cc trigger, chng l mt thnh phn ca lc c s d liu, ngang hng vi cc quan h v cc khung nhn. . Mt khng nh l mt biu thc SQL c gi tr l gic, n phi ng ti mi thi im.

81

. Mt trigger l mt lot hnh ng kt hp vi cc s kin no , chng hn nh chn vo mt quan h c th, v chng c thc hin khi cc s kin ny xy ra.

2.4.1 Cc khng nh (assertion)


Chun SQL ngh mt dng khng nh n gin cho php chng ta bt buc mt iu kin no . Ging nh cc phn t lc khc, chng ta m t mt khng nh bng lnh CREATE. Dng ca mt khng nh l: 1. Cc t kha CREATE ASSERTION, 2. Tn ca khng nh 3. T kha CHECK, v 4. Mt iu kin c t trong du ngoc. Nh vy, dng ca lnh ny l CREATE ASSERTION < Tn > CHECK < iukin > iu kin trong assertion phi ng khi assertion c to ra v phi lun lun vn cn ng; bt k mt sa i c s d liu no lm cho n tr thnh sai s b loi b. Nh li rng cc kiu rng buc CHECK khc m chng ta xt c th b vi phm di mt s iu kin. C mt s khc nhau gia cch vit cc rng buc CHECK da trn b v cch vit assertion. Cc kim tra da trn b c th tham chiu n cc thuc tnh ca quan h c kim tra ny xut hin trong m t ca n. V d, trong dng 6) ca v d 2.8 chng ta s dng cc thuc tnh Gii tnh v Tn m khng ni chng i n t u. Chng tham chiu n cc thnh phn ca mt b c chn vo hoc c cp nht trong bng GIOVIN bi v bng l mt trong cc bng c m t trong lnh CREATE TABLE. iu kin ca mt assertion khng c c quyn nh vy. Cc thuc tnh bt k c tham chiu n trong iu kin phi c gii thiu trong assertion, thng l bng vic ch ra quan h ca chng trong mt biu thc select-from-where. Bi v iu kin phi c mt gi tr lgic, vic nhm cc kt qu ca iu kin theo mt cch no to ra mt la chn true/false n l vic bnh thng. V d, chng ta c th vit iu kin nh l mt 82

biu thc sn xut ra mt quan h m NOT EXISTS c p dng cho quan h ; iu c ngha l, rng buc l quan h ny lun lun rng. Mt cch khc, chng ta c th p dng mt php ton nhm nh l SUM cho mt ct ca mt quan h v so snh n vi mt hng. V d, chng ta c th i hi rng tng lun lun b hn mt gi tr c gii hn no . V d 2.12: Gi s chng ta mun yu cu rng mun tr thnh ngi qun l ca mt n v th phi c lng t nht l 4000. Ta s khai bo mt assertion vi vi mc ch rng tp hp cc n v c ngi qun l vi lng nh hn 4000 l rng. Assertion ny cn c hai quan h NHNVIN v NV. CREATE ASSERTION Quanly CHECK (NOT EXISTS (SELECT * FROM NHNVIN NV, NV V WHERE NV.MsNV = V.M sNQL AND Lng <4000); Nhn tin, cn ch rng mc d rng buc ny c hai quan h, chng ta c th vit n nh cc rng buc CHECK da trn b trn hai quan h hn l nh mt assertion n. V d, chng ta c th vit nh sau CREATE TABLE NV (TnV VARCHAR(15) NOT NULL, MsV INT PRIMARY KEY, Ngybtu DATE, MsNQL CHAR(9) REFERENCES NHNVIN(MsNV), CHECK (MsNQL NOT IN (SELECT MsNV FROM NHNVIN WHERE Lng < 4000)));

83

Tuy nhin, ch rng rng buc ny ch s c kim tra khi c mt thay i xy ra i vi quan h ca n, quan h NV. C th n khng chn c tnh trng c nhng ngi qun l c ghi vo quan b NHNVIN nhng li c lng < 4000. nhn c hiu qu y ca assertion, chng ta phi thm mt rng buc khc vo khai bo ca bng NHNVIN, i hi rng lng ca mt nhn vin phi >=4000 nu anh ta l ngi qun l mt n v. V d 2.13 Assertion sau y ni rng tng lng ca mi n v khng c vt qun 100000. N bao hm quan h NHNVIN. CREATE ASSERTION Tnglng CHECK (100000 >= ALL (SELECT SUM(Lng) FROM NHNVIN GROUP BY MsV)); V rng buc ny ch bao hm c mt quan h NHNVIN, n c th c trnh by nh mt rng buc CHECK da trn bin b trong lc i vi NHNVIN hn l nh mt assertion. V vy, chng ta c th thm vo nh ngha ca bng NHNVIN rng buc CHECK da trn bin b nh sau: CHECK (100000>= ALL (SELECT SUM(Lng) FROM NHNVIN GROUP BY MsV)); rng, v nguyn tc, iu kin ny p dng cho mi b ca bng NHNVIN. Tuy nhin, n khng k ra cc thuc tnh ca b mt cch r rng v mi cng vic c thc hin trong truy vn con. Cng thm rng nu c ci t nh mt rng buc da trn b th vic kim tra khng th thc hin trn php xa mt b ra khi quan h NHNVIN. Trong v d ny, s khc nhau khng gy ra tai hi bi v nu rng buc c tha mn trc khi xa th sau khi xa n cng c tha mn. Tuy nhin, nu rng buc khng phi l cn trn m l cn di ca tng lng theo tng

84

n v th chng ta c th tm thy rng buc b vi phm ngay c khi chng ta vit n nh l mt kim tra da trn b m khng phi l mt assertion. Cui cng, ta c th b mt assertion. Lnh lm vic cng tun theo mu i vi mi phn t ca lc c s d liu. DROP ASSERTION <Tn ca assertion>.

So snh cc rng buc


Bng sau y lit k cc khc nhau c bn gia cc kim tra da trn thuc tnh, cc kim tra da trn b v assertion

Kiu rng buc

c m t Khi no c c m u kch hot bo ng ?

CHECK da trn Cng vi thuc Trn php chn Khng m thuc tnh tnh vo quan h bo nu c hoc cp nht truy vn con thuc tnh CHECK da trn Phn t ca lc Trn php chn Khng m b quan h vo quan h bo nu c hoc cp nht truy vn con b Assertion Phn t ca lc Trn mt thay m bo c s d liu i i vi quan h c k ra

2.4.2 Trigger Cc trigger (hay cn gi l cc quy tc ECA event-condition-action rules) khc vi cc loi rng buc tho lun trn ba im: 1. Cc trigger ch c nh thc khi xy ra mt s kin do ngi lp trnh c s d liu ch ra. Cc loi s kin cho php thng l chn, xa

85

hoc cp nht i vi mt quan h c th. Mt loi s kin khc c cho php trong nhiu h thng SQL l mt kt thc giao tc. 2. Thay cho vic ngn nga tc khc s kin nh thc n, trigger kim tra mt iu kin. Nu iu kin khng tha mn th chng c ci g lin quan ti trigger xy ra trong tr li cho s kin ny. 3. Nu iu kin ca trigger c tha mn th h qun tr c s d liu s thc hin hnh ng lin kt vi trigger. Hnh ng c th l ngn nga s kin xy ra hoc loi b (undo) s kin (v d nh xa b c chn vo). Ni tm li, hnh ng c th l mt h qu no ca cc php ton c s d liu, ngay c cc php ton khng kt ni theo mt cch no vi s kin n ra Trigger trong SQL Mt cu lnh trigger ca SQL cho ngi s dng mt s cc ty chn trong cc phn s kin, iu kin v hnh ng. Sau y l mt s c trng chnh: 1. Hnh ng c th c thc hin hoc trc hoc sau khi s kin n ra 2. Hnh ng c th tham chiu n c cc gi tr c v gi tr mi ca cc b c chn, xa hoc cp nht trong s kin lm bng n hnh ng. 3. Cc s kin cp nht c th c gii hn n mt thuc tnh hoc mt tp thuc tnh c th. 4. Mt iu kin c th c ch ra bng mnh WHEN; hnh ng ch c thc hin khi quy tc c bng n v iu kin tha mn khi xy ra s kin bng n 5. Ngi lp trnh c ty chn ch ra rng hnh ng c thc hin hoc: a) Mt ln i vi mi b c cp nht b) Mt ln i vi tt c cc b c thay i trong mt php ton c s d liu. Trc khi a ra chi tit v c php ca trigger, chng ta hy xt mt v d minh ha cc im quan trng v c php cng nh v ng ngha. Trong v d ny, trigger thc hin mt ln i vi mi b c cp nht 86

V d 2.14: Vit mt trigger ca SQL p dng cho quan h NHNVIN. N c kch hot do cc cp nht i vi thuc tnh Lng. Hiu qu ca trigger ny l chn nh lm gim lng ca mt nhn vin. M t ca trigger nh sau: 1) CREATE TRIGGER LngTrigger 2) AFTER UPDATE OF Lng ON NHNVIN 3) REFERENCING 4) OLD ROW AS OldTuple 5) NEW ROW AS NewTuple 6) FOR EACH ROW 7) WHEN (OldTuple.Lng > Newtuple.Lng) 8) UPDATE NHNVIN 9) SET Lng = Oldtuple.Lng 10) WHERE MsNV = Newtuple.MsNV; Dng 1) a ra m t vi cc t kha CREATE TRIGGER v tn ca trigger. Dng 2) cung cp s kin bng n, l cp nht thuc tnh Lng ca quan h NHNVIN. Dng 3) n dng 5) thit lp mt cch cc phn hnh ng v iu kin ni v b c (b trc khi cp nht) v b mi (b sau khi cp nht). Cc b ny s c tham chiu n nh l Oldtuple v Newtuple theo m t dng 4) v dng 5) tng ng. Trong iu kin v hnh ng, cc tn ny c th c s dng nh l chng l cc bin b c m t trong mnh FROM ca mt truy vn SQL thng thng. Dng 6), cu FOR EACH ROW, biu th i hi rng trigger ny c thc hin mt ln i vi mi mt b c cp nht. Nu khng c cu ny hoc n c thay bng ngm nh FOR EACH STATEMENT, th trigger s xut hin mt ln i vi mt lnh SQL, khng quan tm n s kin bng n thay i cc b bao nhiu ln. Chng ta khng th khai bo b danh cho cc hng mi v cc hng c nhng chng ta c th s dng OLD TABLE v NEW TABLE s a ra sau y. Dng 7) l phn iu kin ca trigger. N ni rng chng ta ch thc hin hnh ng khi lng mi thp hn lng c, 87

ngha l lng ca mt nhn vin b ct gim. Cc dng 8) n 10) to nn phn hnh ng. Hnh ng ny l mt lnh cp nht ca SQL thng thng, n c tc ng khi phc lng ca nhn vin thnh lng trc khi cp nht. rng v nguyn tc, mi mt b ca NHNVIN c xem xt cp nht, nhng mnh WHERE ca dng 10) m bo rng ch c b c cp nht l s b nh hng. Sau y chng ta s a ra cc ty chn do trigger cung cp v biu din cc ty chn nh th no. - Dng 2) ca v d trn ni rng hnh ng ca quy tc c thc hin sau s kin xy ra nh c ch ra bng t kha AFTER. Chng ta c th thay th AFTER bng BEFORE, trong trng hp ny iu kin WHEN c kim tra trc khi s kin xy ra, ngha l trc khi s cp nht lm nh thc trigger c thc hin i vi c s d liu. Nu iu kin l ng th hnh ng ca trigger c thc hin. Sau , s kin nh thc trigger c thc hin m khng cn quan tm n iu kin c ng hay khng. - Ngoi UPDATE, cc s kin bng n khc l INSERT, DELETE. Mnh OF Lng trong dng 2) l ty chn i vi s kin UPDATE v nu c mt th n xc nh s kin ch l cp nht ca cc thuc tnh c lit k sau t kha OF. Mt mnh OF khng c cho php i vi cc s kin INSERT hoc DELETE; cc s kin ny ch c ngha i vi cc b ton vn. - Mnh WHEN l ty chn. Nu khng c n th hnh ng c thc hin mi khi trigger c nh thc. - Trong khi chng ta ch ra mt cu lnh SQL n nh l mt hnh ng, Hnh ng c th gm nhiu cc cu lnh nh vy. Cc cu lnh cch nhau bng du hai chm v c t trong cp BEGIN END. Khi mt s kin bng n l mt cp nht th s c cc b c v cc b mi, l cc b trc v sau cp nht tng ng. Chng ta cho cc b ny cc tn OLD ROW AS V NEW ROW AS nh thy trong cc dng 4) v 5). Nu s kin bng n l mt php chn th chng ta

88

c th s dng mnh NEW ROW AS t tn cho b c chn vo v khng cho php c OLD ROW AS. Ngc li, trong php xa OLD ROW AS c s dng t tn cho b b xa v khng cho php c NEW ROW AS. - Nu chng ta b qua FOR EACH ROW dng 6) th trigger mc dng nh v d trn s tr thnh trigger mc lnh. Mt trigger mc lnh c thc hin mt ln khi mt lnh ca mt kiu thch hp c thc hin, khng quan tm n bao nhiu hng khng, mt, nhiu c nh hng. V d, nu chng ta cp nht ton b bng vi mt lnh cp nht SQL, mt trigger cp nht mc lnh s ch thc hin mt ln, trong khi mt trigger mc b s thc hin mt ln i vi mi b c cp nht. Trong mt trigger mc lnh, chng ta khng th tham chiu n cc b c v cc b mi mt cch trc tip ging nh chng ta ni trong cc dng 4) v 5). Tuy nhin, mt trigger bt k - mc dng hoc mc b - c th tham chiu n quan h ca cc b c (cc b b xa hoc phin bn c ca cc b c cp nht) v quan h ca cc b mi (cc b c chn vo hoc phin bn mi ca cc b c cp nht) bng vic s dng cc m t ging nh OLD TABLE AS OldStuff hoc NEW TABLE AS NewStuff. V d 2.15: Gi s chng ta mun ngn nga trung bnh lng ca cc nhn vin xung di 3000. Rng buc ny c th b vi phm bng mt php chn, mt php xa hoc bng mt php cp nht i vi Lng trong bng NHNVIN. im t nh l ch, trong mt lnh INSERT hoc UPDATE chng ta c th chn vo hoc sa i nhiu b ca NHNVIN v trong qu trnh cp nht, trung bnh lng c th tm thp xung di 3000 v sau li vt ln trn 3000 ti thi im s cp nht hon thnh. Chng ta ch mun loi b ton b tp hp cc cp nht nu trung bnh lng xung di 3000 cui mi lnh. Vic vit mt trigger cho mi s kin chn, xa, cp nht ca quan h NHNVIN l cn thit. Sau y l trigger cho s kin cp nht. Cc trigger cho chn v xa cng tng t. 1) CREATE TRIGGER TBlngTrigger 89

2) AFTER UPDATE OF Lng ON NHNVIN 3) REFERENCING 4) 5) OLD TABLE AS OldStuff NEW TABLE AS NewStuff

6) FOR EACH STATEMENT 7) WHEN (3000 > (SELECT AVG(Lng) FROM NHNVIN) 8) BEGIN 9) DELETE FROM NHNVIN 10) WHERE
(Hm,Tn, MsNV, Ngysinh, ach, Giitnh,Lng, MsNGS, MsV) IN NewStuff ;

11) INSERT INTO NHNVIN 12) (SELECT * FROM OldStuff) 13) END; Cc dng t 3) n 5) khai bo rng NewStuff v OldStuff l tn ca cc quan h cha cc b mi v cc b c dnh dng n php ton quan h lm nh thc trigger ca chng ta. Ch rng mt php ton quan h c th sa i nhiu b ca mt quan h v nu mt lnh nh vy c thc hin th s c nhiu b trong Newstuff v OldStuff. Nu mt php ton l mt UPDATE th NewStuff v OldStuff tng ng l phin bn mi v c ca cc b c cp nht. Nu trigger tng t c vit cho php xa th cc b b xa s trong OldStuff v s khng c m t tn quan h NewStuff cho NEW TABLE nh trong trigger ny. Cng nh vy, trong trigger tng t cho php chn, cc b mi s trong NewStuff v khng c khai bo cho OldStuff. Dng 6) ni vi ta rng trigger ny c thc hin mt ln cho mt lnh, khng cn quan tm n bao nhiu b c cp nht Dng 7) l mt iu kin. iu kin ny c tha mn nu trung bnh lng sau khi cp nht l nh hn 3000.

90

Hnh ng t dng 8) n dng 13) gm hai lnh lm khi phc quan h NHNVIN c nu iu kin ca mnh WHEN c tha mn; ngha l trung bnh lng thp qu. Cc dng 9) v 10) loi b tt c cc b mi (cc b c cp nht) cn cc dng 11) v 12) khi phc cc b nh chng c trc khi cp nht. Cc trigger Thay v (Instead-Of) C mt c trng tin li ca trigger cha c trong chun SQL-99 nhng c mt trong cc cuc tho lun v chun v c mt s h thng thng mi h tr. M rng ny cho php thay th BEFORE hoc AFTER bng INSTEAD OF, ngha ca n l khi mt s kin nh thc mt trigger, hnh ng ca trigger s c thc hin thay cho bn thn s kin. Kh nng ny l b nh khi mt trigger c dng trn mt bng c lu gi, nhng n rt mnh khi c s dng trn mt khung nhn. L do l ch chng ta khng th cp nht mt khung nhn mt cch tht s. Mt trigger instead of ngn chn m mu cp nht mt khung nhn v v tr ca n thc hin bt k hnh ng no m ngi thit k c s d liu cho l thch hp. Sau y l mt v d mu: V d 2.16 Ta hy xem li nh ngha khung nhn v d 40 chng 1: 1) 2) 3) 4) CREATE VIEW NVHNI AS SELECT MsNV, Hm,Tn, Lng, MsV FROM NHNVIN WHERE ach = H ni ;

Nh chng ta tho lun, khung nhn ny l cp nht c nhng n c mt k h khng c tn trng l khi ta chn mt b vo NVHNI, h thng khng th suy din c gi tr ca thuc tnh ach chnh l H ni, v vy thuc tnh l NULL trong b nhn vin c chn vo. S c mt kt qu tt hn nu chng ta to ra mt trigger instead of trn khung nhn ny nh sau 1) CREATE TRIGGER NVInsert 2) INSTEAD OF INSERT ON NVHNI 91

3) REFERENCING NEW ROW AS NewRow 4) FOR EACH ROW 5) INSERT INTO NHNVIN (MsNV, Hm,Tn, Lng, MsV ,ach) 6) VALUES (NV065, Nguyn nh, Thi, 4500, 4,H ni); Chng ta nhn thy t kha INSTEAD OF dng 2) chng minh rng mt php chn vo NVHNi s khng bao gi xy ra. Hn na, cc dng 5) v 6) l hnh ng thay th cho php chn b ngn chn. C mt php chn vo NHNVIN, n ch ra cc thuc tnh, ach. Cc thuc tnh MsNV, Hm, Tn, Lng, MsV l cc thuc tnh t b m ta c gng chn vo khung nhn; Chng ta tham chiu n cc gi tr ny bng bin b NewRow c m t dng 3). Bin b ny biu th b m chng ta mun chn. Gi tr ca thuc tnh ach l hng H ni. Gi tr ny khng phi l thnh phn ca b c chn. ng hn l chng ta gi thit l a ch ng i vi b nhn vin c chn vo bi v php chn i qua khung nhn NVHNI.

2.5 TNG KT CHNG II


1- Cc rng buc kha: Chng ta c th khai bo mt thuc tnh hoc mt tp thuc tnh l mt kha vi mt m t UNIQUE hoc PRIMARY KEY trong lc quan h. 2- Cc rng buc ton vn tham chiu: Chng ta c th khai bo rng mt gi tr xut hin trong mt thuc tnh hoc mt tp thuc tnh no cng phi xut hin trong cc thuc tnh tng ng ca mt b no ca mt quan h khc vi m t REFERENCE hoc FOREIGN KEY trong lc quan h 3- Cc rng buc kim tra da trn thuc tnh: Chng ta c th t mt rng buc trn gi tr ca mt thuc tnh bng cch thm t kha CHECK v iu kin s c kim tra vo sau m t ca thuc tnh ny trong lc quan h ca n.

92

4- Cc rng buc kim tra da trn b: Chng ta c th t ln cc b ca mt quan h bng cch thm t kha CHECK v iu kin s c kim tra vo m t ca chnh quan h . 5- Sa i cc rng buc: C th thm v xa mt kim tra d trn b bng lnh ALTER i vi bng thch hp. 6- Assertion: Chng ta c th khai bo mt assertion nh mt thnh phn ca ca lc c s d liu vi t kha CHECK v mt iu kin s c kim tra. iu kin ny c th dnh dng n mt hoc nhiu quan h ca lc c s d liu, v c th dnh dng n mt quan h y , chng hn vi php nhm cng nh l cc iu kin v cc b ring r. 7- Vic gi cc kim tra: Cc assertion c kim tra mi khi c mt thay i i vi mt trong cc quan h dnh dng n. Cc kim tra da trn b v da trn thuc tnh ch c kim tra khi php chn hoc php cp nht lm thay i thuc tnh hoc quan h m cc kim tra p dng vi chng. Nh vy, cc rng buc ny c th b vi phm nu chng c cc truy vn con 8- Trigger: Chun SQL c cc trigger ch ra cc s kin no y (Chn, xa hoc cp nht mt quan h c th). Mi ln c nh thc, mt iu kin c th c kim tra v nu ng th mt dy hnh ng ghi r s c thc hin. Cc hnh ng l cc lnh SQL nh l cc truy vn v cp nht c s d liu.

MT S BI TP
I. Gi s chng ta c c s d liu sau: Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) Hy vit m t y ca lc c s d liu trn.

93

II.

Vit cc rng buc trn y trn cc thuc tnh ca cc quan h trn: 1) Tc ca laptop t nht l 800. 2) a CD ch c th l 32x hoc 40x hoc a DVD l 12x hoc 16x. 3) Cc my in ch c kiu l laser, ink-jet hoc bubble. 4) Ch c cc kiu sn phm l PC, laptop v printer.

III. Vit cc iu sau y nh l cc trigger hoc assertion. Trong mi trng hp loi b hoc khng cho php cc cp nht nu chng khng tha mn cc rng buc. 1) Khi cp nht gi ca mt PC, hy kim tra rng khng c PC no c cng tc nhng c gi r hn. 2) Khng c nh sn xut PC c th sn xut laptop. 3) Mt nh sn xut PC phi sn xut mt laptop vi t nht mt tc b x l ln 4) Khi chn vo mt printer mi hy kim tra rng s model tn ti trong quan h Product. 5) Khi thc hin mt cp nht bt k trong quan h Laptop, hy kim tra rng gi trung bnh ca cc laptop i vi mi nh sn xut t nht l $2000. 6) Khi sa i RAM hoc a cng ca mt PC bt k, hy kim tra rng PC c cp nht c a cng nhiu hn RAM 100 ln. 7) Nu mt laptop c b nh chnh rng hn mt b nh mt PC th laptop cng phi c gi cao hn PC. 8) Khi chn vo mt PC, lap top, hoc printer mi hy m bo rng s model khng c xut hin trc trong cc quan h PC, Laptop, Printer 9) Nu quan h Product cp n mt model v kiu ca n th model ny phi xut hin trong quan h thch hp vi kiu .

94

95

CHNG III: LP TRNH


Trong cc chng trc, chng ta ch ra rng SQL l mt ngn ng truy vn tng tc mnh. Kiu truy cp n thng tin ny rt tin li bi v nhng ngi s dng lun lun c yu cu truy vn c s d liu theo mt logic khng c thit lp trc. V nguyn tc, SQL tng tc rt tin li cho nhim v bi v n cho ngi s dng xy dng truy vn p ng cc yu cu ca anh ta. Trn thc t, iu khng thch hp lm vi nhng ngi dng cui bi v c php ca ngn ng phc tp. Vic vit cc iu kin ni thch hp v cc truy vn lng nhau t ra rt nhiu vn . V vy, hin nhin l ngi dng cui khng thch th vic t mnh xy dng cc truy vn SQL. Vi nhng ngi dng cui cn c mt cng c cho php h to ra cc truy vn v son tho cc bo co d dng hn, khng cn c nhiu hiu bit v c php SQL v cu trc ca cc bng. Cng c l chng trnh. C nhiu phng php vit mt chng trnh ng dng da trn SQL. l nhng cc cu lnh SQL vo trong cc chng trnh c vit trong mt ngn ng lp trnh thng thng, chng hn nh C, hoc kt hp SQL vi lp trnh chung, hoc xy dng cc th vin SQL mu, v..v. Trong chng ny chng ta s xt n cc phng php lp trnh da trn SQL v nhng vn lin quan.

3.1 SQL TRONG MI TRNG LP TRNH


Ti thi im ny, chng ta s dng giao din SQL chung trong cc v d. l, chng ta gi s rng c mt b thng dch SQL, n chp nhn v thc hin cc loi truy vn v cc cu lnh chng ta hc. Mc d c hu ht cc h qun tr c s d liu cung cp nh mt ty chn, kiu thao tc ny him khi c s dng. Trong thc t, hu ht cc cu lnh SQL l mt phn ca mt mu phn mm ln. Mt khung cnh thc tin hn l c mt chng trnh c vit trong mt ngn ng ch thng thng no chng hn nh C, nhng mt s bc trong chng trnh ny l nhng cu lnh SQL. Trong phn ny chng ta s m t mt cch lm cho SQL thao tc trong mt chng trnh thng thng.
Hnh 3.1 l mt phc ha ca mt h thng lp trnh in hnh c cha nhng cu lnh SQL. , chng ta nhn thy ngi lp trnh ang vit cc chng trnh

96

trong mt ngn ng ch nhng vi mt vi cu lnh SQL nhng c bit khng l mt phn ca ngn ng ch. Ton b chng trnh c gi ti mt b tin x l, n bin i nhng cu lnh SQL nhng thnh nhng th c ngha trong ngn ng ch. S biu din ca SQL c th n gin nh l mt li gi ti mt hm, hm ny nhn cu lnh SQL nh mt tham s xu k t v thc hin cu lnh SQL . Chng trnh ngn ng ch c tin x l sau c bin dch theo cch thng thng. Nhng nh cung cp h qun tr c s d liu thng chun b y mt th vin cung cp nhng nh ngha hm cn thit. Do vy, nhng hm th hin SQL c th c x l, v ton b chng trnh ng x nh mt n v. Chng ta cng a vo trong Hnh 3.1 kh nng ngi lp trnh vit m lnh trc tip trong ngn ng ch, s dng nhng li gi hm khi cn. Cch tip cn ny, thng c tham kho n nh mt giao din mc gi (call-level interface CLI), s c tho lun trong phn 3.4.

3.1.1 Vn tr ngi khng ph hp Vn c bn ca vic kt ni nhng cu lnh SQL vi nhng cu lnh ca mt ngn ng lp trnh thng thng l tr ngi khng ph hp, trn thc t l m hnh d liu ca SQL khc cc m hnh ca nhng ngn ng khc rt nhiu. Nh chng ta bit, SQL s dng m hnh d liu quan h li ca n. Tuy nhin, C v nhng ngn ng lp trnh ph bin khc s dng mt m hnh d liu vi nhng s nguyn, s thc, cc s, cc k t, con tr, cu trc bn ghi, mng v..v. Tp hp khng c mt trc tip trong C hoc nhng ngn ng khc , trong khi SQL khng s dng bin tr, vng lp v r nhnh, hoc nhiu cu trc ngn ng lp trnh ph bin khc. Kt qu l vic trao i d liu truyn d liu gia SQL v nhng ngn ng khc l khng n gin, v phi a ra mt k thut cho php xy dng nhng chng trnh s dng c SQL v ngn ng khc. u tin ngi ta c th gi thit rng tt hn ht l s dng mt ngn ng n; hoc lm tt c nhng tnh ton trong SQL hoc qun SQL i v lm tt c nhng tnh ton trong mt ngn ng thng thng. Tuy nhin, chng ta c th nhanh chng b qua tng b qua SQL khi c nhng php ton c s d liu c a vo. Cc h thng SQL h tr rt nhiu cho ngi lp trnh trong vic vit nhng php ton c s d liu c th c 97

thc hin mt cch hiu qu tuy rng chng c th c biu bin mc rt cao. SQL gii phng ngi lp trnh khi s cn thit hiu d liu c t chc nh th no trong b nh hoc khai thc cu trc lu tr nh th no thao tc mt cch c hiu qu trn c s d liu. Mc khc, c nhiu vic quan trng m SQL khng th lm ni. V d, ngi ta khng th vit mt truy vn SQL tnh ton giai tha ca mt s n [n!=n*(n-1)**2*1], d l mt bi tp n gin trong C hoc trong nhng ngn ng tng t. Mt v d khc, SQL khng th nh dng u ra ca n mt cch trc tip thnh mt dng thch hp nh l mt ha. Do vy, lp trnh c s d liu thc s i hi c SQL v mt ngn ng thng thng; ngn ng ny thng c tham chiu n nh l ngn ng ch (host language). 3.1.2 Giao din ngn ng ch /SQL Vic truyn thng tin gia c s d liu (ch c th c truy cp bi nhng cu lnh SQL) v chng trnh ngn ng ch thng qua nhng bin ca ngn ng ch, gi l cc bin dng chung. Nhng bin ny c th c cc cu lnh SQL c hoc ghi. Tt c nhng bin dng chung (shared variable) nh vy c mt du hai chm i trc khi chng c tham chiu n trong mt cu lnh SQL, nhng chng xut hin khng c du hai chm trong nhng cu lnh ngn ng ch. Khi chng ta mun s dng mt cu lnh SQL trong mt chng trnh ngn ng ch, chng ta phi t cc t kha EXEC SQL trc n. Mt h thng in hnh s tin x l nhng cu lnh v thay th chng bi nhng li gi hm ph hp trong ngn ng ch, to ra vic s dng mt th vin cc hm SQL c lin quan.

98

Ngn ngch + SQL nhng Bo tien xu ly

Ngn ngch + Li gi h m B bin d ch ngn ngch Chng trnh ch H 3.1: Vi x l cc ch tr v i cc c l SQL nhng nh c ng nh u nh

Th vi SQL n

Mt bin c bit, c gi l SQLSTATE trong SQL chun, phc v kt ni chng trnh ngn ng ch vi h thng thc hin SQL. Kiu SQLSTATE l mt mng nm k t. Mi khi mt hm ca th vin SQL c gi, mt m c t vo trong bin SQLSTATE ch ra nhng vn bt k c tm thy trong sut qu trnh gi . SQL chun nh r mt lng ln nhng m nm k t v ngha ca chng. V d, 00000 (nm s khng) ch ra rng tnh trng khng li xy ra, v 02000 ch ra rng khng tm thy b gi tr c yu cu. Chng ta s thy rng m 02000 rt quan trng, v n cho php chng ta to mt vng lp trong chng trnh ngn ng ch kim tra ln lt cc b ca mt quan h v ph b vng lp sau khi b cui cng c kim tra. Chng trnh ngn ng ch c th c c gi tr ca SQLSTATE v a mt quyt nh da trn c s gi tr tm c . 3.1.3 Phn khai bo (DECLARE) khai bo nhng bin dng chung, chng ta t nhng khai bo ca chng gia hai cu lnh SQL nhng: EXEC SQL BEGIN DECLARE SECTION; 99

EXEC SQL END DECLARE SECTION; Nhng g xut hin gia chng c gi l phn khai bo. Dng ca nhng khai bo bin trong phn khai bo l tt c mi th m ngn ng ch yu cu. Hn na, n ch c ngha i vi khai bo cc bin c kiu m c ngn ng ch v SQL c th giao tip, chng hn nh s nguyn, s thc, v xu k t hoc mng. V d 3.1: Nhng cu lnh sau y c th xut hin trong mt hm C cp nht quan h NV: EXEC SQL BEGIN DECLARE SECTION; INT Ms INT, CHAR Tn[15]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; Cc dng lnh u v cui l quy nh bt u v kt thc phn khai bo. gia l mt dng lnh khai bo hai bin Ms v Tn. Chng l mt s nguyn v mt mng k t v, nh chng ta s thy, chng c th c s dng lu gi mt m s v tn ca mt n v c a vo trong mt b v c chn vo trong quan h NV. Dng lnh th ba khai bo SQLSTATE l mt mng su k t. 3.1.4 S dng cc bin dng chung. Mt bin dng chung c th c s dng trong cc cu lnh SQL nhng v tr chng ta mun hoc c php t mt hng. Nhc li rng nhng bin dng chung c mt du hai chm t trc n khi s dng. y l mt v d trong chng ta s dng cc bin ca V d 3.1 nh l nhng thnh phn ca mt b c chn vo trong quan h NV. V d 3.2: Trong Hnh 3.2 l mt minh ha ca mt hm C getDonvi nhc ngi dng cho ms v tn ca mt n v, c tr li v chn b thch hp vo NV. Dng (1) n dng (4) l nhng khai bo chng ta c hc trong V d 3.1. Chng ta b qua cc lnh ca C yu cu a cc gi tr vo hai bin Ms v Tn. Sau , trong cc dng (5) v (6) l mt cu lnh SQL 100

nhng, l mt cu lnh INSERT thng thng. Lnh ny c t kha EXEC SQL i trc ch ra rng n qu thc l mt cu lnh SQL nhng ch khng phi l mt cu lnh C sai ng php. B tin x l c ngh trong Hnh 3.1 s tm kim EXEC SQL pht hin nhng cu lnh phi c tin x l. Nhng gi tr c cc dng (5) v (6) chn vo khng phi l cc hng r rng. Nhng gi tr xut hin trong dng (6) l nhng bin dng chung m gi tr hin thi ca chng tr thnh nhng thnh phn ca b c chn. Cng vi cu lnh INSERT cn c nhiu kiu cu lnh SQL khc c th c nhng vo trong mt ngn ng ch, chng s dng nhng bin dng chung nh mt giao din. Mi cu lnh nhng SQL c cc t kha EXEC SQL i trc trong chng trnh ngn ng ch v c th tham chiu n cc bin dng chung trong v tr ca cc hng. Cu lnh SQL bt k khng tr v mt kt qu (tc l, khng phi l mt truy vn) c th c nhng. Nhng v d v nhng cu lnh SQL c th nhng c bao gm nhng cu lnh xa v cp nht v nhng cu lnh to, sa, hoc xa nhng thnh phn lc chng hn nh cc bng v cc khung nhn. Void getDONVI() { 1) EXEC SQL BEGIN DECLARE SECTION; 2) 3) INT Ms INT, CHAR Tn[15]; char SQLSTATE[6];

4) EXEC SQL END DECLARE SECTION; /* Cc cu lnh C yu cu nhp d liu cho Ms v Tn */ 5) EXEC SQL INSERT INTO NV(MsV, TnV) 6) VALUES(:M s,:Tn) } Hnh 3.2: Minh ha v d 3.2 Tuy nhin, nhng truy vn select-from-where l khng nhng c mt cch trc tip vo trong mt ngn ng ch, bi v tr ngi khng ph hp. Cc truy vn a ra kt qu l nhng tp hp cc b, trong khi khng c mt ngn ng ch no h tr kiu d liu tp hp mt cch trc tip. Do vy, 101

SQL nhng phi s dng mt trong hai k thut kt ni kt qu ca cc truy vn vi mt chng trnh ngn ng ch. 1. Mt truy vn a ra mt b n c th c b c lu tr trong cc bin dng chung, mt bin cho mi thnh phn ca b. lm nh vy, chng ta s dng mt dng sa i ca cu lnh select-fromwhere c gi l mt select n-hng (single-row select). 2. Cc truy vn to ra nhiu hn mt b c th c thc hin nu chng ta khai bo mt con tr cho truy vn.Con tr duyt qua tt c cc b trong quan h tr li, v mi b c th ln lt c t vo trong nhng bin dng chung v c x l bi chng trnh ngn ng ch.
Chng ta s xem xt ln lt tng k thut mt.

3.1.5 Cc cu lnh Select n hng Khun mu ca mt select n hng cng ging nh mt cu lnh selectfrom-where bnh thng, ngoi tr rng theo sau mnh SELECT l t kha INTO v mt danh sch cc bin dng chung. Nhng bin dng chung c cc du hai chm i trc, nh l trng hp cho tt c nhng bin dng chung trong mt cu lnh SQL. Nu kt qu ca truy vn l mt b n, cc thnh phn ca b ny s tr thnh cc gi tr ca nhng bin dng chung. Nu kt qu hoc l khng c b hoc c nhiu hn mt b, th s khng c php gn no cho cc bin dng chung c thc hin, v mt m li thch hp c ghi vo bin SQLSTATE. V d 3.3: Chng ta s vit mt hm C c tn ca mt n v v in ra lng ca ngi qun l n v. Mt phc ha ca hm ny c th hin trong Hnh 3.3. N bt u vi mt phn khai bo, dng (1) n dng (5), cho nhng bin chng ta s cn n. Tip theo l cc cu lnh C yu cu nhp vo Tnnv (cc lnh ny khng c vit r). Dng (6) n (9) l cu lnh select n hng. N hon ton tng t cc truy vn chng ta c bit. Hai iu khc bit l gi tr ca bin Tnnv c s dng thay cho mt hng xu trong iu kin ca dng (9), v c mt mnh INTO dng (7) cho chng ta bit ni t kt qu ca 102

truy vn. Trong trng hp ny, chng ta mong i mt b n, v cc tuple ch c mt thnh phn cho thuc tnh Lng. Gi tr ca mt thnh phn ny ca mt b c lu tr trong bin chia dng chung LuongNQL. void printLuong() { 1) 2) 3) 4) 5) EXEC SQL BEGIN DECLARE SECTION; char Tendonvi[15]; int LuongNQL; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* Cc lnh C yu cu nhp gi tr cho Tendonvi */ 6) 7) 8) 9) EXEC SQL SELECT Lng INTO:LuongNQL FROM NHNVIN, NV WHERE NHNVIN.MsNV = NV.M sNQL AND TnV =:Tendonvi;

/* Cc lnh C kim tra xem c phi SQLSTATE cha ton s 0 hay khng. Nu ng th in ra gi tr ca LuongNQL */ } Hnh 3.3: Mt select n hng c nhng vo mt hm C 3.1.6 Con tr Cch linh hot nht kt ni cc truy vn SQL ti mt ngn ng ch l dng mt con tr chy qua cc b ca mt quan h. Quan h ny c th l mt bng c lu tr, hoc n c th l mt bng do mt truy vn sinh ra. to v s dng mt con tr, chng ta cn nhng cu lnh sau y: 1. Mt khai bo con tr. Hnh thc n gin nht ca mt khai bo con tr bao gm: (a) Mt m u EXEC SQL, ging nh tt c nhng cu lnh SQL nhng. 103

(b) T kha DECLARE. (c) Tn ca con tr. (d) Cc t kha CURSOR FOR. (e) Mt biu thc nh mt tn quan h hoc mt biu thc select-fromwhere m gi tr ca n l mt quan h. Con tr c khai bo duyt qua cc b ca quan h ny; ngha l, con tr ln lt tham chiu ti mi b ca quan h ny, nh l chng ta ly ra bng vic s dng con tr. Tng qut, dng ca mt khai bo con tr l: EXEC SQL DECLARE <con tr> CURSOR FOR <truy vn> 2. Mt cu lnh EXEC SQL OPEN tip theo sau l tn con tr. Cu lnh ny khi to con tr n mt v tr ni n sn sng ly ra b u tin ca quan h m con tr s i qua trn . 3. Mt hoc nhiu s dng ca cu lnh FETCH. Mc ch ca mt lnh FETCH l nhn b tip theo ca quan h m con tr i qua trn . Nu cc b c vt cn th khng c b no c tr v, v gi tr ca SQLSTATE c thit lp thnh 02000, mt m c ngha l khng tm thy b . Cu lnh FETCH bao gm nhng thnh phn sau y: (a) Cc t kha EXEC SQL FETCH FROM. (b) Tn ca con tr. (c) T kha INTO. (d) Mt danh sch cc bin dng chung, c phn cch bi cc du phy. Nu c mt b ly ra, th nhng thnh phn ca b ny c t trong nhng bin , theo th t. Nh vy, dng ca mt cu lnh FETCH l: EXEC SQL FETCH FROM <con tr> INTO <danh sch cc bin>
4. Cu lnh EXEC SQL CLOSE sau l tn ca con tr. Cu lnh ny ng con tr, by gi n khng cn i qua trn cc b ca quan h na. Tuy nhin n c

104

th c khi to li bi cu lnh OPEN khc, trong trng hp , mt ln na n li duyt qua cc b ca quan h ny.

Vd 3.4: Gi s chng ta mong mun m xem c bao nhiu nhn vin c lng gm 1 ch s, 2 ch s, 3 ch s,.v..v ... Chng ta s thit k mt truy vn ly ra trng Lng ca tt c cc b NHNVIN a vo mt bin dng chung gi l LuongNV. Mt con tr c gi l execCursor s duyt qua tt c nhng b c mt thnh phn . Mi ln mt b c ly ra, chng ta tnh s cc ch s trong bin nguyn LuongNV v gia tng phn t tng ng ca mt mng m Hm C Hnglng bt u trong dng (1) ca Hnh 3.4. Dng (2) khai bo mt s bin ch c hm C s dng, SQL nhng khng s dng. Mng m lu gi s lng cc nhn vin ; bin Chs m s lng cc ch s trong mt gi tr Lng, v i l mt ch s duyt qua cc phn t ca mng m. 1) void Hnglng () { 2) i, Chuso, Dem[15]; 3) EXEC SQL BEGIN DECLARE SECTION; 4) int LuongNV; 5) char SQLSTATE[6]; 6) EXEC SQL END DECLARE SECTION; 7) EXEC SQL DECLARE execCursor CURSOR FOR 8) SELECT Lng FROM NHNVIN 9) EXEC SQL OPEN execCursor; 10) for (i=0; i<15; i++) Dem[i] = 0; 11) while (1) { 12) EXEC SQL FETCH FROM execCursor INTO:LuongNV; 13) if (NO_MORE_TUPLES) break; 14) Chuso = 1; 15) while((LuongNV /= 10) > 0) Chuso++; 16) if(Chuso <= 14) Dem[Chuso]++; } 17) EXEC SQL CLOSE execCursor ; 18) for (i=0; i<15; i++) 19) printf(chuso = %d: so nhan vien = %d\n, i, Dem[i]); }

105

Hnh 3.4 m s nhn vin c lng gm 1 ch s, 2 ch s, .


Dng (3) n (6) l mt phn khai bo SQL bin dng chung LuongNV v SQLSTATE thng dng c khai bo. Dng (7) v (8) khai bo execCursor l mt con tr duyt qua nhng gi tr c truy vn trn dng (8) cung cp.Truy vn ny n gin l yu cu nhng thnh phn Lng ca tt c cc b trong NHNVIN. Con tr ny sau c m dng (9). Dng (10) hon thin vic khi to bng vic gn 0 cho cc phn t ca mng Dem.

Cng vic chnh c lm bi vng lp dng (11) n dng (16). dng (12) mt b c ly ra t vo trong bin dng chung LuongNV. Khi nhng b do truy vn ca dng (8) tuple cung cp ch c mt thnh phn, chng ta ch cn mt bin dng chung mc d trong trng hp tng qut s c nhiu bin nh l s thnh phn ca nhng b tm c. Dng (13) kim tra vic ly ra thnh cng cha. y, chng ta s dng mt macro NO_MORE_TUPLES. Chng ta c th gi thit macro ny c nh ngha bi: #define NO_MORE_TUPLE !(strcmp(SQLSTATE, 02000)) Nhc li rng 02000 l m SQLSTATE c ngha l khng c b no c tm thy. Do vy, dng (13) kim tra xem c phi l tt c cc b c truy vn tr v c tm thy trc v khng c b tip theo ly. Nu ng, chng ta thot khi vng lp v i ti dng (17). Nu mt b c ly ra, th dng (14) chng ta khi to s lng cc ch s trong Lng l 1. Dng (15) l mt vng lp lp li vic chia LuongNV cho 10 v tng Chuso ln 1. Khi LuongNV t ti 0 sau khi chia cho 10, Chuso lu gi s lng cc ch s trong gi tr ca LuongNV c ly ra. Cui cng, dng (16) tng phn t tng ng ca mng Dem ln 1. Chng ta gi s rng s lng cc ch s khng nhiu hn 14. Tuy nhin, c th c mt gi tr Lng vi 15 ch s hoc nhiu hn, dng (16) s khng tng bt k phn t no ca mng Dem, v khng c hng thch hp; ngha l, nhng Lng rt ln b vt i v khng nh hng n nhng thng tin thng k.

106

Dng (17) bt u kt thc hm. Con tr c ng, v cc dng (18) v (19) in cc gi tr trong mng Dem. 3.1.7 Cp nht bng con tr Khi mt con tr duyt qua cc b ca mt bng c s (ngha l, mt quan h c lu tr trong c s d liu ch khng phi l mt khung nhn hoc mt quan h do mt truy vn to ra), th ngi ta khng ch c th c v x l gi tr ca mi b, m cn c th cp nht hoc xa cc b. C php ca nhng cu lnh UPDATE v DELETE ging nh chng ta bt gp chng 1, ngoi tr mnh WHERE. Mnh c th ch l WHERE CURRENT OF sau l tn ca con tr. D nhin chng trnh ngn ng ch c th c b p dng mt iu kin no m n mun trc khi quyt nh c xa hoc cp nht n hay khng. Vd 3.5: Trong Hnh 3.5 chng ta thy mt hm C, hm ny xem xt mi b ca NHNVIN v quyt nh hoc l xa b nu lng nh hn 1000 hoc l nhn i gi tr Lng. Trong cc dng (3) v (4) chng ta khai bo cc bin tng ng vi chn thuc tnh ca NHNVIN, cng vi bin SQLSTATE. Sau , dng (6), execCursor c khai bo t n duyt qua quan h c lu tr NHNVIN. Ghi nh rng, trong khi chng ta c th c gng sa i cc b thng qua mt con tr duyt qua mt quan h tm thi l kt qu ca mt truy vn no , th chng ta ch c th c mt nh hng bn vng trn c s d liu nu con tr duyt qua mt quan h c lu tr chng hn nh NHNVIN. Dng (8) n (14) l vng lp, con tr execCursor ln lt tham chiu n tng b ca NHNVIN. Dng (9) ly ra b hin hnh t vo trong chn bin c s dng cho mc ch ny; lu rng ch c Luong1 c s dng thc s. Dng (10) kim tra xem chng ta ly ht cc b ca NHNVIN hay cha. Macro NO_MORE_TUPLES c s dng li cho iu kin bin SQLSTATE c m khng cn tuple 02000. Trong kim tra iu kin dng (11) chng ta hi lng c di 1000 hay khng. Nu ng, b b xa do cu lnh DELETE ca dng (12). Ghi nh rng mnh WHERE tham chiu con tr, v th b hin thi ca NHNVIN, b m chng ta va ly c, b xa khi NHNVIN. Nu 107

lng t nht l 1000 th dng (14), Lng trong b c thay th bi gi tr gp i. 1) void ThaydoiLuong() { 2) EXEC SQL BEGIN DECLARE SECTION; 3) Char Hm1[20], Tn1[15], MsNV1[9], date ngaysinh1, Char Dach1[30], char Giitinh1, Int Luong1, 4) char MsNGS1[9], Int MsV1, SQLSTATE[6]; 5) EXEC SQL END DECLARE SECTION; 6) EXEC SQL DECLARE execCursor CURSOR FOR NHNVIN; 7) EXEC SQL OPEN execCursor; 8) While (1) { 9) EXEC SQL FETCH FROM execCursor INTO:Hm1,:Tn1, MsNV1,:ngaysinh1,:ach1,:Giitnh1,:Luong1,: MsNGS1,:MsV1 ; 10) if (NO_MORE_TUPLES) break; 11) if (Luong1 < 1000) 12) EXEC SQL DELETE FROM MovieExec WHERE CURRENT OF execCursor; 13) else 14) EXEC SQL UPDATE MovieExec SET Lng = 2 * Lng WHERE CURRENT OF execCursor 15) EXEC SQL CLOSE execCursor; } Hnh 3.5: Sa i lng ca nhn vin

3.1.8 Bo v khi s cp nht ng thi Gi s rng khi chng ta kim tra cc lng ca cc nhn vin v s dng hm HangLuong ca Hnh 3.4, mt vi tin trnh khc ang sa i quan h NHNVIN. Chng ta s ni nhiu hn v nhiu tin trnh truy cp ng thi dn mt c s d liu khi chng ta ni v giao tc trong phn 3.6. Tuy

108

nhin, lc ny, n gin l chng ta chp nhn kh nng c nhng tin trnh khc c th sa i mt quan h nh chng ta s dng n. Chng ta phi lm g v kh nng ny? Hin nhin l chng ta c th khng mun cho php nhng thay i cnh tranh lm nh hng n nhng b chng ta thy thng qua con tr ny. Ta mun l nhng thng k c ly trn quan h nh n tn ti mt thi im no . Chng ta khng th iu khin mt cch chnh xc nhng sa i no i vi quan h NHNVIN xy ra trc vic tp hp cc thng k ca chng ta, nhng chng ta c th mong ch rng nhng cu lnh sa i hoc xy ra hon ton trc hoc hon ton sau khi hm Hangluong chy, bt chp c bao nhiu nhn vin b nh hng bi mt cu lnh sa i. t c s m bo ny, chng ta c th khai bo con tr khng nhy cm (insensitive) cho nhng thay i cnh tranh. V d 3.6: Chng ta c th sa i dng (7) v (8) ca Hnh.4 thnh: 7) 8) EXEC SQL DECLARE execCursor INSENSITIVE CURSOR FOR SELECT Lng FROM NHNVIN;

Nu execCursor c khai bo nh vy, th h thng SQL s m bo rng nhng thay i i vi quan h NHNVIN thc hin gia mt m v ng execCursor s khng nh hng n tp nhng b c ly ra. Mt con tr insensitive c th t, theo ngha l h thng SQL c th s dng nhiu thi gian cho vic qun l truy cp d liu m bo rng con tr l insensitive. Mt ln na, mt tho lun v vic qun l nhng thao tc ng thi trn c s d liu c hon ti phn 3.6. Tuy nhin, mt cch n gin h tr mt con tr insensitive l cho h thng SQL tr hon li bt k tin trnh no c th truy cp cc quan h m cc truy vn ca con tr insensitive ca chng ta ang s dng. C nhng con tr no ang duyt qua mt quan h R m chng ta c th ni v chng mt cch chc chn rng chng s khng thay i R. Mt con tr nh vy c th chy ng thi vi mt con tr insensitive ca R, khng c s mo him lm thay i quan h R m con tr insensitive nhn 109

thy. Nu chng ta khai bo mt con tr FOR READ ONLY, th h c s d liu c th chc chn rng quan h nn s khng b sa i bi vic truy nhp ti quan h thng qua con tr ny. V d 3.7: Chng ta c th gn vo sau dng (8) ca Hnh 3.4 mt dng FOR READ ONLY;
Nu vy, th bt k n lc thc hin mt sa i thng qua con tr execCursor s gy ra mt li.

3.1.9 Con tr cun (Scrolling Cursor) Cc con tr cho chng ta mt s la chn v vic chng ta di chuyn qua cc b ca quan h nh th no. S la chn mc nh v ph bin nht l bt u t u v ly ra cc b theo th t, cho n khi kt thc. Tuy nhin, c nhng th t khc m theo cc b c th c ly ra v cc b c th c qut vi ln trc khi con tr b ng. li dng nhng t chn ny, chng ta cn lm hai vic sau: 1. Khi khai bo mt con tr, t t kha SCROLL ng trc t kha CURSOR. Thay i ny ni vi h thng SQL rng con tr c th c s dng mt kiu khc vi vic di chuyn v pha trc theo trt t ca cc b. Trong mt cu lnh FETCH, theo sau t kha FETCH l mt trong nhiu ty chn cho bit tm b mong mun u. Nhng ty chn l: (a) NEXT hoc PRIOR nhn bn ghi tip theo hoc pha trc theo th t. Nhc li rng bn ghi ny c lin quan vi v tr hin thi ca con tr. NEXT l mc nh nu khng ch ra ty chn no, v n l mt la chn thng xuyn. (b) FIRST hoc LAST nhn bn ghi u tin hoc bn ghi cui cng theo th t. (c) RELATIVE theo sau l mt s nguyn dng hoc m, n ch ra bao nhiu bn ghi c di chuyn ti (nu s nguyn l dng)

2.

110

hoc lui (nu m) theo th t. Cho v d, RELATIVE 1 ng ngha vi NEXT, v RELATIVE -1 ng ngha vi PRIOR. (d) ABSOLUTE theo sau l mt s nguyn dng hoc m, n ch ra v tr ca bn ghi mong mun m t pha trc (nu dng) hoc pha sau (nu m). Cho v d, ABSOLUTE 1 ng ngha vi FIRST v ABSOLUTE 1 ng ngha vi LAST. V d 3.8: Chng ta hy vit li hm trong Hnh 3.5 bt u bn ghi cui cng v di chuyn li li thng qua mt danh sch cc bn ghi. u tin, chng ta cn khai bo con tr execCursor l cun c, iu ny chng ta lm bng cch thm t kha SCOLL vo dng (6), nh sau: 6) EXEC SQL DECLARE execCursor SCROLL CURSOR FOR NHNVIN; Hn na, chng ta cn khi to vic ly v nhng bn ghi vi mt cu lnh FETCH LAST, v trong vng lp chng ta s dng FETCH PRIOR. Vng lp t dng (8) n dng (14) trong Hnh 3.5 c vit li nh trong Hnh 3.6. Ngi c s khng cho rng c u im no khi c nhng bn ghi theo th t ngc vi th t m chng c lu tr trong NHNVIN. EXEC SQL FETCH LAST FROM execCursor INTO:Hm1,:Tn1, : MsNV1,:ngaysinh1,:ach1,:Giitnh1,:Luong1,: MsNGS1,:MsV1 ; while (1) { /* ging nhcc dng t (10) n (14) */ EXEC SQL FETCH PRIOR FROM execCursor INTO:Luong1; } Hnh 8.6: c LI cc b NHNVIN 3.1.10 SQL ng M hnh SQL nhng trong mt ngn ng ch ca chng ta l ch ra nhng truy vn v cu lnh SQL bn trong mt chng trnh ngn ng ch. Mt kiu xen k ca SQL nhng c nhng cu lnh t chng c tnh ton bi ngn ng ch. Nhng cu lnh nh vy khng c bit thi gian bin dch, v do vy khng th c iu khin bi mt b tin x l SQL hoc mt b bin dch ngn ng ch. 111

Mt v d ca mt tnh trng nh vy l mt chng trnh nhc ngi s dng nhp mt truy vn SQL, c truy vn, v sau x l truy vn . Giao din chung ca nhng truy vn tc thi (ad-hoc)SQL m chng ta tha nhn trong Chng 1 l mt v d ca mt chng trnh nh vy; tt c mi h thng SQL thng mi cung cp kiu giao din SQL chung ny. Nu cc truy vn c c v thc hin thi gian chy, th khng c g c thc hin thi gian bin dch. Truy vn phi c phn tch c php v h thng SQL s tm ra mt cch ph hp thc hin truy vn, ngay lp tc sau khi truy vn c c. Chng trnh ngn ng ch phi ch th h thng SQL ly nhng xu k t va c c, chuyn n thnh cu lnh SQL thc hin c, v cui cng thc hin cu lnh ny. C hai cu lnh SQL ng thc hin hai bc ny. 1. EXEC SQL PREPARE, sau l bin V ca SQL, t kha FROM, v mt bin hoc biu thc ngn ng ch kiu xu k t. Cu lnh ny sinh ra mt xu c xem nh mt cu lnh SQL. C l, cu lnh SQL c phn tch c php v h thng SQL tm ra mt cch tt thc hin n, nhng cu lnh khng c thc thi. ng hn l, k hoch thc hin cu lnh SQL tr thnh gi tr ca V. 2. EXEC SQL EXECUTE c theo sau bi mt bin SQL chng hn nh V trong (1). Cu lnh ny lm cho cu lnh SQL c biu th bi V c thc thi. Hai bc c th c kt hp thnh mt, vi cu lnh: EXEC SQL EXECUTE IMMEDIATE sau l mt bin dng chung c gi tr kiu xu hoc mt biu thc c gi tr kiu xu. Nhc im ca vic kt hp hai thnh phn ny c nhn nhn nu chng ta chun b mt ln mt cu lnh v thc thi n nhiu ln. Vi EXECUTE IMMEDIATE gi ca vic chun b cu lnh phi chu mi ln cu lnh c thc thi, thay v ch chu mt ln, khi chng ta chun b n. V d 3.9: Trong Hnh 3.7 l mt phc ha ca mt chng trnh C c vn bn t u vo chun vo trong mt bin query, chun b n, v thc thi

112

n. Bin SQL SQLquery lu gi truy vn c chun b. V truy vn ch c thc thi mt ln duy nht, dng: EXEC SQL EXECUTE IMMEDIATE:query; C th thay th dng (6) v dng (7) ca Hnh 3.7. 1) void readQuery() { 2) 3) 4) 5) EXEC SQL BEGIN DECLARE SECTION; char *query; EXEC SQL END DECLARE SECTION; /* nhc ngi dng nhp truy vn vo, phn phi ch (tc l s dng mt malloc) v to bin dng chung:query ch n k t u tin ca truy vn */ 6) EXEC SQL PREPARE SQLquery FROM:query; 7) EXEC SQL EXECUTE SQLquery; } Hnh 3.7: Chun b v thc hin mt truy vn SQL ng.

3.2 CC TH TC C LU GI (stored procedure)


Trong phn ny, chng ta lm quen vi mt chun SQL gn y c gi l Persistent, Stored Modules (SQL/PSM, hoc ng hn l PSM, hoc PSM96). Mi h qun tr c s d liu cung cp cho ngi s dng mt cch lu tr cng vi mt lc c s d liu mt s hm hoc th tc c th c s dng trong cc truy vn SQL hoc cc cu lnh SQL khc. Nhng mu chng trnh c vit bng mt ngn ng n gin, a nng, v cho php chng ta thc hin nhng tnh ton khng th c biu din trong ngn ng truy vn SQL ngay bn trong c s d liu. Trong ti liu ny chng ta s m t chun SQL/PSM. Chun ny cho php ta vit c cc mu chng trnh theo tng trn v gip ta hiu ngn ng c lin kt vi h thng c th bt k. Trong PSM, chng ta nh ngha cc mdun (modules), l cc tp hp ca cc nh ngha hm v th tc, cc khai bo quan h tm thi, v nhiu

113

khai bo ty chn khc. Chng ta tho lun su hn v cc m un trong Phn 3.3.7; y chng ta s ch tho lun cc hm v cc th tc ca PSM. 3.2.1 To cc hm v cc th tc PSM Cc yu t chnh ca mt khai bo th tc l CREATE PROCEDURE <tn> (<danh sch tham s>) cc khai bo cc b thn th tc; Dng ny c quen bit t mt s ngn ng lp trnh; n bao gm mt tn th tc, mt danh sch cc tham s t trong du ngoc n, mt s khai bo bin cc b ty chn, v phn thn gm cc lnh thc hin c nh ngha th tc. Mt hm c nh ngha hu nh ging nh vy, ngoi tr t kha FUNCTION c s dng v phi ch r mt kiu gi tr tr li. Do vy, cc yu t ca mt nh ngha hm l: CREATE FUNCTION <tn> (<danh sch tham s>) RETURN <kiu> cc khai bo cc b thn hm; Cc tham s ca mt th tc l b ba ca ch - tn - kiu, rt ging danh sch tham s ca cc phng php ODL. Nh vy, mt tn tham s khng ch c kiu khai bo ca n i sau nh thng dng trong ngn ng lp trnh, m n cn c mt ch t trc, l IN, OUT, hoc INOUT. Ba t kha ny ch ra rng tham s ch l input, ch l output hoc c input v output mt cch tng ng. IN l mc nh, v c th c b qua. Mt khc, cc tham s hm c th ch l ch IN. Nh vy, PSM ngn cm hiu ng ph bn trong cc hm v vy cch duy nht ly thng tin t mt hm l thng qua gi tr tr v ca n. Chng ta s khng ch ra ch IN cho cc tham s hm, mc d chng ta s lm iu trong cc nh ngha th tc. Vd 3.10: Trong khi chng ta cha c hc nhng cu lnh khc nhau c th xut hin trong cc thn hm v th tc, ta xt mt loi lnh quen bit: l mt cu lnh SQL. Hn ch trn nhng cu lnh ny ging nh i vi SQL nhng: ch nhng cu lnh select n hng v nhng truy cp da vo 114

con tr mi c cho php nh l cc truy vn. Trong Hnh 3.8 l mt th tc PSM, th tc ny ly hai a ch - mt a ch c v mt a ch mi v i thuc tnh ach ca tt c nhn vin t a ch c sang a ch mi.

1) CREATE PROCEDURE Thaydoi( 2) 3) IN Diachicu VARCHAR[30], IN Diachimoi VARCHAR[30])

4) UPDATE NHNVIN 5) SET ach = iachimoi 6) WHERE ach = iachicu Hnh 3.8: Mt th tc thay i a ch Dng (1) gii thiu th tc v tn ca n: Thaydoi. Dng (2) v (3) cha hai tham s, c hai u l nhng tham s u vo, kiu ca chng l nhng xu k t di bin i c di 30. Ghi nh rng kiu ny ph hp vi kiu chng ta khai bo cho thuc tnh ach ca NHNVIN trong chng 1. Dng (4) n (6) l nhng cu lnh UPDATE thng thng. Tuy nhin, ghi nh rng cc tn ca tham s c th c s dng nh nu n l cc hng. Khng ging cc bin ngn ng ch, cc bin ny i hi c mt du hai chm t trc chng khi c s dng trong SQL (xem phn 3.1.2), cc tham s v cc bin a phng khc ca cc th tc v hm PSM i hi khng c du hai chm.

3.2.2 Mt vi dng cu lnh n gin trong PSM 1. Cu lnh gi: Dng ca mt li gi th tc l: CALL <tn th tc> (<danh sch tham s>); Tc l, t kha CALL theo sau l tn ca th tc v danh sch cc tham s c t trong du ngoc n, ging nh trong hu ht cc ngn ng. Tuy nhin, li gi ny c th c lm t nhiu v tr: 115

i. T mt chng trnh ngn ng ch. V d,n c th xut hin nh sau: EXEC SQL CALL Foo(:x, 3); ii. Nh mt cu lnh ca hm hoc th tc PSM khc iii. Nh mt cu lnh SQL c a ra cho giao din SQL chung. V d, chng ta c th a ra mt cu lnh chng hn nh CALL Foo(1, 3); mt giao din nh vy, v c th tc c lu gi Foo c thc hin vi hai tham s ca n c thit lp l 1 v 3 tng ng. Ghi nh rng khng c php gi mt hm. Ta yu cu mt hm trong PSM nh chng ta lm trong C: s dng tn hm v cc tham s ph hp nh l mt phn ca mt biu thc. 2. Cu lnh tr v: Dng ca n l RETURN <biu thc>; Cu lnh ny ch c th xut hin trong mt hm. N tnh gi tr biu thc v t gi tr tr v ca hm bng vi kt qu . Tuy nhin, khc vi cc ngn ng lp trnh thng thng, cu lnh tr v ca PSM khng kt thc hm. ng hn l iu khin tip tc vi cu lnh theo sau, v c th gi tr tr v s c thay i trc khi hm hon thnh. 3. Khai bo cc bin a phng: Dng cu lnh DECLARE <tn> <kiu>; khai bo mt bin vi mt tn cho trc c kiu cho trc. Bin ny l a phng, v gi tr ca n khng c h qun tr c s d liu duy tr sau mt qu trnh chy ca hm hay th tc. Cc khai bo phi t trc cc cu lnh thc hin c trong thn hm hoc th tc. 4. Cc cu lnh gn: Dng ca mt lnh gn l: SET <bin> = <biu thc>;

116

Tr t kha m u SET, lnh gn trong PSM kh ging vi lnh gn trong cc ngn ng khc. Biu thc bn phi ca du bng c tnh gi tr, v gi tr ca n tr thnh gi tr ca bin bn tri. NULL l mt biu thc c php. Thm ch biu thc c th l mt truy vn khi n tr v mt gi tr n. 5. Nhm cu lnh: Chng ta c th to lp mt danh sch nhng cu lnh kt thc bi du chm phy v c bao quanh bi t kha BEGIN v END. Cu trc ny c xem nh mt cu lnh n v c th xut hin bt c ch no m mt cu lnh n c th xut hin. c bit, v mt thn th tc hoc hm c mong mun l mt cu lnh n, chng ta c th t mt dy bt k ca cc cu lnh trong thn bng cch bao quanh chng bi BEGINEND. 6. Nhn cu lnh: Chng ta s thy trong phn 3.2.5 mt l do ti sao cc cu lnh no cn mt nhn. Chng ta gn nhn mt cu lnh bng vic t trc n mt tn (nhn) v mt du hai chm. 3.2.3 Cc cu lnh r nhnh. Vi kiu cu lnh phc tp u tin ca PSM, chng ta xt cu lnh IF. Dng lnh ny ch c cht t khc l. N khc vi C hoc cc ngn ng tng t ch: 1. Cu lnh kt thc vi cc t kha END IF. 2. Cu lnh if lng trong mnh else c m u bng t kha n ELSEIF. V vy, dng chung ca mt cu lnh if ging nh xut trong hnh 3.9. iu kin l mt biu thc c gi tr logic bt k, nh c th xut hin trong mnh WHERE ca cu lnh SQL. Mi danh sch cu lnh bao gm nhng cu lnh kt thc bi du chm phy, nhng khng cn c bao quanh bi BEGINEND. ELSE cui cng v nhng cu lnh ca n l ty chn. V d IFTHENEND IF mt mnh hoc cng vi ELSEIF u c th chp nhn.

117

IF < iu kin> THEN <danh sch cu lnh> ELSEIF <iu kin > THEN <danh sch cu lnh> ELSEIF ELSE <danh sch cu lnh> END IF; Hnh 3.9: Dng ca mt cu lnh if V d 3.11: Gi s chng ta c mt lc quan h PHIM (Tnphim, Nmsnxut, di, Mu, Tnxngphim, Tnodin) Trong thuc tnh Mu c kiu Logic, c gi tr TRUE nu l phim mu, FALSE nu l phim en trng. Chng ta hy vit mt hm ly nm y v mt xng phim s, v tr v mt gi tr boolean l TRUE khi v ch khi xng phim s sn xut t nht mt b phim en trng trong nm y hoc khng sn xut bt c b phim no tt c mi thi im trong nm . Chng trnh c trnh by hnh 3.10 1) 2) 3) 4) 5) 6) 7) 8) 9) CREATE FUNCTION Vd(y INT, s CHAR[15]) RETURNS BOOLEAN IF NOT EXISTS( SELECT * FROM PHIM WHERE Nmsnxut = y AND Tnxungphim = s) THEN RETURN TRUE; ELSEIF 1<= (SELECT COUNT(*) FROM PHIM WHERE Nmsnxut = y AND Tnxngphim = s AND NOT Mu) THEN RETURN TRUE; ELSE RETURN FALSE; END IF; Hnh 3.10: V d v cu lnh IF 118

Dng (1) gii thiu hm bao gm cc tham s ca n. Chng ta khng cn xc nh mt ch cho cc tham s, v n ch c th l IN cho mt hm. Dng (2) v (3) kim tra trng hp khi khng c bt c b phim no ca xng phim s trong nm y, trong trng hp chng ta thit lp gi tr tr v TRUE dng (4). Ghi nh rng dng (4) khng l nguyn nhn hm tr v. V k thut, mt dng iu khin tun theo cc cu lnh if sinh ra iu khin nhy t dng (4) ti dng (9), hm hon thnh v tr v. Nu xng phim s c lm phim trong nm y, th dng (5) v (6) kim tra xem c phi t nht mt trong s chng khng phi phim mu. Nu nh vy, gi tr tr v li c t l TRUE, ln ny dng (7). Trong trng hp ngc li, xng phim s c lm cc b phim nhng ch ton l phim mu, do vy chng ta thit lp gi tr tr v l FALSE dng (8). 3.2.4 Cc truy vn trong PSM C nhiu cch s dng cc truy vn select-from-where trong PSM. Cc truy vn con c th c s dng trong cc iu kin, hoc tng qut, bt c v tr no mt truy vn con c php trong SQL. minh ha, chng ta xem hai v d v nhng truy vn con trong dng (3) v (6) ca hnh 3.10. 1. Cc truy vn tr v mt gi tr n c th c s dng bn phi ca cc cu lnh gn. 2. Mt cu lnh select n hng l mt cu lnh hp l trong PSM. Nhc li l cu lnh ny c mt mnh INTO ch nh nhng bin vo ni t nhng thnh phn ca bn ghi c tr v n. Nhng bin ny c th l nhng bin a phng hoc cc tham s ca mt th tc PSM. Dng tng qut c tho lun trong ng cnh ca SQL nhng trong Phn 3.1.5. 3. Chng ta c th khai bo v s dng mt con tr, v c bn n c m t trong Phn 8.1.6 ca SQL nhng. Vic khai bo con tr, tt cc cu lnh OPEN, FETCH, v CLOSE nh c m t , vi cc ngoi l sau: (a) Khng c EXEC SQL xut hin trong cc cu lnh, v 119

(b) Cc bin (l cc b) khng s dng tin t hai chm CREATE PROCEDURE SomeProc(IN Tendonvi CHAR[15]) DECLARE LuongNQL INTEGER; SELECT Luong INTO LuongNQL FROM NHNVIN,NV WHERE NHNVIN.MsNV = NV.M sNQL AND TnV = Tendonvi; Hnh 3.11: Mt select n hng trong PSM V d 3.12: Trong Hnh 3.11 l mt select n hng ca Hnh 3.3, lm li bng PSM v t trong ng cnh mt nh ngha th tc gi thuyt. Ghi nh rng, bi v select n hng tr v mt bn ghi c mt thnh phn, chng ta cng c th nhn nh hng t mt cu lnh ch nh ging nh: SET LuongNQL = (SELECT Luong FROM NHNVIN,NV WHERE NHNVIN.MsNV = NV.M sNQL AND TnV = Tendonvi; Chng ta s hon nhng v d ca con tr cho n khi chng ta hc cc cu lnh lp PSM trong phn tip theo. 3.2.5 Vng lp trong PSM Cu trc vng lp c bn trong PSM l: LOOP <danh sch tham s> END LOOP; Ngi ta thng gn nhn cho cu lnh LOOP, v vy n c th ph v vng lp, bng vic s dng cu lnh: LEAVE <nhn vng lp>; Trong trng hp ph bin loop tham gia ly v nhng bn ghi bng mt con tr, chng ta thng mong mun ri b vng lp khi khng cn bn ghi. S hu ch nu khai bo mt tn trng thi ca gi tr SQLSTATE, n ch ra 120

rng khng tm thy bn ghi no (tc l khi SQLSTATE = 02000); chng ta lm vy vi: DECLARE Not_Found CONDITION FOR SQLSTATE 02000; Tng qut hn, chng ta c th khai bo mt trng thi vi bt k mt tn mong mun tng ng vi gi tr SQLSTATE bng DECLARE <tn> CONDITION FOR SQLSTATE <gi tr>; By gi chng ta sn sng dn ra mt v d tri buc nhng thao tc con tr v vng lp trong PSM. V d 3.13: Hnh 3.12 th hin mt th tc PSM n nhn mt tn xng quay s nh mt tham s u vo v a ra tham s u ra mean v variance gi tr trung bnh v s bin thin v di ca tt c nhng b phim ca xng s. Dng (1) n dng (4) khai bo th tc v cc tham s ca n. Dng (5) n dng (8) l cc khai bo cc b. Chng ta nh ngha Not_Found l tn ca trng thi c ngha l mt FETCH tht bi tr v mt bn ghi dng (5). Sau , dng (6), con tr MovieCursor c nh ngha tr v tp hp nhng di ca nhng b phim ca xng quay s. Dng (7) v (8) khai bo hai bin cc b m chng ta s cn. S nguyn newLength lu gi kt qu ca mt FETCH, trong khi movieCount m s lng nhng b phim ca xng quay s. Chng ta cn movieCount bi v, cui cng, chng ta c th chuyn mt tng cc di vo trong mt gi tr trung bnh ca cc di v mt tng ca cc bnh phng cc di vo trong mt s bin thin. Phn cn li ca cc dng l thn ca th tc. Chng ta s s dng mean v variance l cc bin tm thi, cng nh khi tr v nhng kt qu cui cng. Trong vng lp chnh, mean thc s nm gi tng ca cc di, v variance thc s nm gi tng cc bnh phng cc di. Do vy, cc dng (9) n (11) khi to cc bin ny v tng s cc b phim l 0. Dng (12) m con tr, v dng (13) n dng (19) thit lp vng lp c gn nhn movieLoop. Dng (14) biu din mt fetch, v dng (15) chng ta kim tra xem nhng bn ghi khc c oc tm thy khng. Nu khng, chng ta ri khi vng 121

lp. Dng (15) n (18) tch ly cc gi tr; chng ta thm 1 vo movieCount, thm di ti mean (nhc li, n tnh ton tng cc di), v chng ta thm bnh phng ca di ti variance. Khi tt nhng b phim ca xng quay s c tm thy, chng ta ri khi vng lp, v iu khin chuyn n dng (20). dng ny, chng ta tr chuyn mean thnh gi tr chnh xc ca n bng cch chia tng ca cc di cho tng s cc b phim. dng (21), chng ta to variance thc s lu gi s bin thin bng cch chia tng bnh phng ca di cho tng s cc b phim v tr cho bnh phng ca trung bnh. Xem Hnh 8.2.4 tho lun ti sao tnh ton ny chnh xc. Dng (22) ng con tr, v chng ta hon thnh. 1) CREATE PROCEDURE MeanVar( 2) IN s CHAR[[15]], 3) OUT mean REAL, 4) OUT variance REAL ) 5) DECLARE Not_Found CONDITION FOR SQLSTATE 02000; 6) DECLARE MovieCursor CURSOR FOR SELECT di FROM PHIM WHERE Tnxngphim = s; 7) DECLARE newLength INTEGER; 8) DECLARE movieCount INTEGER; BEGIN 9) SET mean = 0.0; 10) SET variance = 0.0; 11) SET movieCount = 0; 12) OPEN MovieCursor; 13) MovieLoop: LOOP 14) FETCH MovieCursor INTO newLength; 15) IF Not_Found THEN LEAVE MovieLoop END IF; 16) SET movieCount = movieCount + 1; 17) SET mean = mean + newLength; 18) SET variance = variance + newLength * newLength; 19) END LOOP; 20) SET mean = mean/movieCount; 122

21) SET variance = variance/movieCount mean * mean; 22) CLOSE MovieCursor; END; Hnh 3.12: Tnh gi tr trung bnh v bin thin ca di cc cun phim do mt xng phim sn xut 3.2.6 Vng lp for Trong PSM cng c mt cu trc for, nhng n cho mt mc ch quan trng duy nht: lp li thng qua mt con tr. Dng ca cu lnh l: FOR <tn vng lp> AS <tn con tr> CURSOR FOR <truy vn> DO <danh sch cu lnh> END FOR; Cu lnh ny khng ch khai bo mt con tr m n cn vn hnh cho chng ta mt s lng cc chi tit kh chu: vic m v ng con tr, vic ly v v kim tra xem c phi khng c bn ghi no c ly khng. Tuy nhin, v chng ta khng t ly cc bn ghi, chng ta khng th ch ra cc bin c t cho nhng thnh phn no ca bn ghi. Do vy, cc tn c s dng cho cc thuc tnh trong kt qu ca truy vn cng c PSM coi nh cc bin cc b c cng kiu. 1) CREATE PROCEDURE MeanVar( 2) IN s CHAR[[15]], 3) OUT mean REAL, 4) OUT variance REAL ) 5) DECLARE movieCount INTEGER; BEGIN 6) SET mean = 0.0; 7) SET variance = 0.0; 8) SET movieCount = 0; 9) FOR movieLoop AS MovieCursor CURSOR FOR SELECT di FROM PHIM WHERE studioName = s; 10) DO 123

11) SET movieCount = movieCount + 1; 12) SET mean = mean + length ; 13) SET variance = variance + length * length; 14) END FOR; 15) SET mean = mean/movieCount; 16) SET variance = variance/movieCount mean * mean; END; Hnh 3.13: Tnh gi tr trung bnh v bin thin ca di cc cun phim s dng vng lp for 3.2.7 Nhng cu lnh lp khc PSM cng cho php cc vng lp while v repeat, chng c ngha nh trong C. Nh vy chng ta c th vit mt vng lp di dng: WHILE <iu kin> DO <Danh sch cc lnh> END WHILE; Hoc mt vng lp di dng REPEAT <danh sch cc lnh> UNTIL < iu kin > END REPEAT; Tin y, nu chng ta gn nhn cho cc vng lp ny, hoc vng lp c mt lnh lp hoc mt lnh for to ra, th chng ta c th t nhn sau END LOOP hoc sau kt thc khc cng c. u im ca vic lm nh vy l ch n lm r kt thc ca mi vng lp u v n cho php b thng dch PSM bt c mt s li ng php bao hm vic b qua mt END. V d 3.14: Chng ta hy lm li th tc ca Hnh 3.12 bng vic s dng mt vng lp for. M lnh c th hin trong Hnh 3.13. Nhiu th khng thay i. Phn khai bo ca th tc t dng (1) n dng (4) ca Hnh 3.13 th tng t, cng nh vic khai bo bin cc b movieCount dng (5).

124

Tuy nhin, chng ta khng cn khai bo mt con tr trong phn khai bo ca th tc na, v chng ta khng cn nh ngha iu kin Not_Found. Dng (6) n dng (8) khi to cc bin, nh trc y. Sau , dng (9) chng ta xem vng lp for, n cng nh ngha con tr MovieCursor. Cc dng (11) n (13) l thn ca vng lp. Ghi nh rng trong cc dng (12) v (13), chng ta tham kho di ly v bi con tr bng thuc tnh tn l length, thay v bng bin cc b newLength, bin ny khng tn ti trong phin bn ny ca th tc. Cc dng (15) v (16) tnh ton cc gi tr chnh xc cho cc bin u ra, ng nh trong phin bn trc y ca th tc ny. 3.2.8 Nhng loi tr trong PSM Mt h thng PSM ch ra cc iu kin li bng cch thit lp mt chui khc khng cc con s trong mt xu nm k t SQLSTATE. Chng ta xem mt v d v nhng m ny: 02000 ca khng bn ghi no c tm thy. V d khc, 21000 ch ra rng mt select n hng tr v nhiu hn mt hng. PSM cho php chng ta khai bo mt phn m lnh, c gi l b iu khin loi tr, n c gi mi khi mt li trong danh sch nhng m li ny xut hin trong SQLSTATE trong khi thc thi mt cu lnh hoc mt danh sch cc cu lnh. Mi b iu khin loi tr c lin kt vi mt khi m lnh, c m t bi BEGINEND. B iu khin xut hin trong khi ny, v n ch p dng cho nhng cu lnh trong khi. Cc thnh phn ca b iu khin l: Mt danh sch cc iu kin loi tr gi n b iu khin khi c kch hot. M lnh c thi hnh khi mt trong nhng biu thc lin kt c kch hot. Mt ch th v tr n sau khi b iu khin kt thc cng vic ca n. Cch thc khai bo ca mt b iu khin l: DECLARE <v tr n> HANDLER FOR <danh sch iu kin> <cu lnh>

125

Cc la chn ca v tr n l: CONTINUE, n c ngha l sau khi thc hin cu lnh trong phn khai bo iu khin, chng ta thc hin cu lnh sau khi sau cu lnh pht sinh loi tr. EXIT, c ngha rng sau s thc thi cu lnh ca b iu khin, iu khin ri khi khi BEGINEND ch b iu khin c khai bo. Cu lnh sau khi ny s c thc hin tip. UNDO, n ging nh l EXIT, ngoi tr rng bt c thay i no ti c s d liu hoc cc bin cc b c lm bi cc cu lnh ca khi cho n by gi b tho b. Tc l, nhng nh hng ca n b xa b, v cng nh l nhng cu lnh ny cha h c thc hin. Danh sch iu kin l mt danh sch nhng iu kin c phn cch bi du phy, chng hoc l nhng iu kin c khai bo, ging nh Not_Found trong dng (5) ca Hnh 8.12, hoc nhng biu thc ca dng SQLSTATE v mt xu nm k t. V d 3.15: Chng ta hy vit mt hm PSM, hm ny ly mt tiu b phim lm tham s v tr v nm ca b phim. Nu khng c b phim no c tiu hoc c nhiu hn mt b phim c tiu , th NULL phi c tr v. M lnh c th hin trong Hnh 3.14. CREATE FUNCTION GetYear (t VARCHAR[255]) RETURN INTEGER DECLARE Not_Found CONDITION FOR SQLSTATE 02000 ; DECLARE Too_Many CONDITION FOR SQLSTATE 21000 ; BEGIN DECLARE EXIT HANDLER FOR Not_Found, Too_Many RETURN NULL ; RETURN (SELECT Nm FROM PHIM WHERE Tnphim = t); END; Hnh 3.14 iu khin ngoi tr trong mt SELECT n hng tr v nhng ci khc vi mt b 126

Dng (2) v (3) khai bo cc iu kin tiu biu; chng ta khng phi to nhng nh ngha ny, v cng c th s dng cc trng thi SQL vi vic chng t trong dng (4). Dng (4), (5) v (6) l mt khi, u tin chng ta khai bo mt b iu khin cho hai iu kin hoc l khng c bn ghi no c tr v, hoc c nhiu hn mt bn ghi c tr v. Hnh ng ca b iu khin, trn dng (5), n gin l thit lp gi tr tr v l NULL. Dng (6) l cu lnh lm cng vic ca hm GetYear. N l mt cu lnh SELECT mong mun tr v chnh xc mt s nguyn, v l hm gi tr hm GetYear tr v. Nu chnh xc c mt b phim c tiu t (tham s u vo ca hm), th gi tr ny s c tr v. Tuy nhin, nu mt loi tr pht sinh dng (6), hoc l bi v khng c b phim no c tiu t hoc vi b phim c tiu , th b iu khin c gi, mt NULL thay th, bi tr thnh gi tr tr v. Hn na, v b iu khin l mt b iu khin EXIT, iu khin chuyn tip ti con tr sau END. V con tr l cui ca hm, GetYear tr v thi im , vi gi tr tr v NULL. 3.2.9 S dng cc hm v cc th tc PSM Nh chng ta cp trong Phn 3.2.2, chng ta c th gi mt hm hoc th tc PSM t mt chng trnh vi SQL nhng, t chnh m lnh PSM, hoc t cc cu lnh SQL nguyn thy xut pht t giao din chung. Vic s dng nhng th tc v hm ny th ging nh trong cc ngn ng lp trnh hn c, vi cc th tc c gi bi CALL, v cc hm xut hin nh thnh phn ca mt biu thc. Chng ta s a ra mt v d ti sao mt hm c th b hy b t giao din chung. V d 3.16: Gi s rng lc ca chng ta bao gm mt m un vi hm GetYear ca Hnh 3.14. Tng tng rng chng ta ang giao din chung, v chng ta mun nhp vo s tht l Denzel Washington l mt ngi sao ca Remember the Titans. Tuy nhin, chng ta qun nm m b phim c lm. Bi v ch c duy nht mt b phim c tn nh vy, v n trong quan h Movie, chng ta khng phi tm kim trong mt truy vn m u. Hn na, chng ta c th a ti giao din SQL chung lnh chn sau y:

127

INSERT INTO StarsIn(Tnphim, Nm, Tndinvin) VALUES(Remember the Titans, GetYear(Remember the Titans), Dezel Washington); V GetYear tr v gi tr NULL nu khng c mt b phim duy nht c tn Remember the Titans, c th l vic chn ny s cho gi tr NULL trong thnh phn gia.

3.3 MI TRNG SQL


Trong phn ny chng ta s trnh by mt khung nhn rng ri nht c th v mt DBMS v cc c s d liu v cc chng trnh m n h tr. Chng ta s xem cc c s d liu c nh ngha v t chc vo trong cc cluster, cc danh mc, v cc lc nh th no. Chng ta cng s xem cc chng trnh c lin kt vi d liu chng cn thao tc nh th no. Nhiu chi tit ph thuc vo s ci t c th, v vy chng ta s tp trung vo nhng tng chung c trong chun SQL. 3.3.1 Mi trng Mt mi trng SQL l mt khung cnh m bn di n d liu c th tn ti v cc thao tc SQL trn d liu c th c thc hin. Trong thc tin, chng ta nn ngh v mt mi trng SQL nh l mt h qun tr c s d liu ang chy mt ci t no . V d, cng ty ABC mua mt bn quyn ca DBMS Megatron 2002 chy trn mt tp hp cc my ca ABC. H thng ang chy trn nhng my ny to thnh mt mi trng SQL. Tt c cc yu t ca c s d liu m chng ta tho lun cc bng, cc khung nhn, cc trigger, cc th tc c lu gi, v..v - c nh ngha trong mt mi trng SQL. Nhng yu t ny c t chc thnh mt phn cp ca cc cu trc, mi cu trc ng mt vai tr ring bit trong t chc. Nhng cu trc c chun SQL nh ngha c ch ra trong Hnh 3.15. Mt cch ngn gn, t chc bao gm nhng cu trc sau y: 1. Cc lc (schemas). l tp hp cc bng, cc khung nhn, cc khng nh nhn, cc trigger, cc m un PSM, v mt vi kiu thng 128

tin khc m chng ta khng tho lun trong quyn sch ny (nhng xem hp Thm v cc yu t lc na trong Phn 3.3.2). Cc lc l nhng n v t chc c bn, gn vi ci chng ta c th ngh nh mt c s d liu, nhng thc s c phn km hn mt c s d liu nh chng ta s thy trong im (3) di y. 2. Cc danh mc (catalog). l tp hp cc lc . Mi danh mc c mt hoc nhiu lc ; cc tn ca lc trong mt danh mc phi l duy nht, v mi danh mc bao cha mt lc c bit gi l lc thng tin, n bao cha thng tin v tt c cc lc trong danh mc. 3. Cc cluster. l tp hp cc danh mc. Mi ngi s dng c mt cluster lin kt: l tt c cc danh mc m ngi s dng c th truy cp ti (xem Phn 8.7 c l gii vic truy cp ti cc danh mc v cc yu t khc c iu khin nh th no). Mt cluster l phm vi ln nht m thng qua n mt truy vn c th c a ra; nh vy, theo mt ngha no , mt cluster l c s d liu nh mt ngi dng c th nhn thy.

Environment = Installation of DBMS

Catalog Catalog
Cluster = maximum scope of a DB operation

Schema Catalog Schema

H 3.15: S t nh chc ca cc y t cs dli trong mi tr ng u u

129

3.3.2 Lc Dng n gin ca khai bo lc bao gm: T kha CREATE SCHEMA Tn ca lc Mt danh sch cc khai bo ca cc yu t lc chng hn nh cc bng c s, cc khung nhn, v cc khng nh. Chng hn, mt lc c th c khai bo nh sau: CREATE <tn lc > <cc khai bo thnh phn> Cc khai bo thnh phn l nhng mu c tho lun trong nhng ni khc nhau, chng hn nh trong chng 1, chng 2 v u ca chng ny. V d: Chng ta c th khai bo mt lc bao gm nm quan h v cc b phim chng ta ang s dng trong v d ang chy ca chng ta, cng vi mt s cc yu t khc chng ta gii thiu, chng hn nh nhng khung nhn. Hnh 3.16 phc ha dng mt khai bo nh vy CREATE SCHEMA CNGTY CREATE TABLE NHNVIN . Cc lnh to bng i vi cc bng khc CREATE VIEW NVHANOI .. Cc lnh to khung nhn khc CREATE ASSERTION Quanly.. Hnh 3.16: Khai bo mt lc Vic khai bo lc tt c mt ln l khng cn thit. Ngi ta c th sa i hoc thm vo mt lc bng vic s dng cc cu lnh CREATE, DROP, hoc ALTER mt cch thch hp, v d CREATE TABLE theo sau bi vic khai bo mt bng mi cho mt lc . Mt vn l h thng SQL cn bit bng mi thuc v lc no. Nu chng ta sa i hoc xa mt bng hoc mt phn t lc khc, chng ta cng cn lm r tn ca

130

cc phn t, v hai hoc nhiu lc c th c nhng phn t khc nhau nhng cng tn. Chng ta thay i lc hin ti vi mt cu lnh SET SCHEMA. V d SET SCHEMA CONGTY; Lm cho lc c m t trong Hnh 3.16 tr thnh lc hin ti. Sau , cc m t ca cc phn t lc c thm vo lc , v cc cu lnh DROP hoc ALTER bt k tham kho n cc phn t c trong lc . 3.3.3 Cc danh mc (Catalog) Cng nh l cc yu t lc ging nh cc bng c to trong mt lc , cc lc c to v sa i trong mt danh mc. V nguyn tc, chng ta c th hy vng qu trnh to ra v ph bin cc danh mc cng tng t vi qu trnh to ra v ph bin cc lc . Tht ng tic, SQL khng nh ngha mt cch thc chun lm vic ny, chng hn nh mt cu lnh CREATE CATALOG <tn danh mc> Theo sau bi mt danh sch cc lc thuc v danh mc v cc khai bo ca cc lc ny. Tuy nhin, SQL quy nh mt cu lnh SET CATALOG <tn danh mc> Cu lnh ny cho php chng ta thit lp mt danh mc hin thi, nh vy cc lc mi s i vo trong danh mc v cc sa i lc s tham kho ti cc lc trong danh mc s c s nhp nhng v tn. Thm v cc phn t lc Mt s cc phn t lc chng ta cha k ra nhng i khi chng rt hu ch, l: Domains: l cc tp hp gi tr hoc cc kiu d liu n gin. Ngy nay chng t c s dng bi v cc h qun tr c s d liu quan h - i tng cung cp cc c cu to kiu mnh hn. 131

Tp cc k t: l cc tp k t v v cc phng php m ha chng. ASCII l tp k t ni ting nht, nhng mt ci t SQL c th h tr nhiu tp k t khc, chng hn nh tp hp cc ngn ng nc ngoi khc nhau. S i chiu (Collations): Cho php i chiu (so snh) cc xu k t. Mt collation ch r nhng k t no l nh hn cc k t khc Cc lnh Grant: Chng lin quan n nhng ngi truy cp n cc phn t ca lc . Chng ta s tho lun v vic cp c quyn trong phn 3.7 3.3.4 Client v Server trong mi trng SQL Mt mi trng SQL c nhiu hn mt tp hp cc danh mc v lc . N bao cha cc cc phn t c mc ch l h tr cc thao tc trn c s d liu hoc cc c s d liu c cc danh mc v lc miu t. Trong mt mi trng SQL c hai kiu tin trnh c bit: cc SQL client v cc SQL server. Mt server h tr cc thao tc trn cc phn t c s d liu, v mt client cho php mt ngi dng kt ni n mt server v thao tc trn c s d liu. C th tng tng rng server chy trn mt my ch ln, my ch ny lu tr cc c s d liu v client chy trn mt my ch khc, c th l mt my trm c nhn cch xa so vi server. Tuy nhin, cng c th c client v server chy trn cng mt my ch. 3.3.5 Kt ni Nu chng ta c mt chng trnh no c cha SQL ti mt my m c tn ti SQL client th chng ta c th m mt kt ni gia Client v Server bng cch thc hin lnh SQL: CONNECT TO < Tn Server> AS < Tn kt ni> AUTHORIZATION < Tn v mt khu > Tn ca Server ph thuc vo s ci t. T DEFAULT c th thay th cho mt tn v s kt ni ngi dng vi mt SQL Server no m s ci t xem nh mt Server mc nh. Tn v mt khu ca ngi dng l mt phng php cho Server c th xc nh c ngi dng. Ngi ta c th s dng mt xu khc i sau AUTHORIZATION.

132

Tn kt ni c th c s dng sau ny tham chiu n kt ni. Nguyn nhn ca vic chng ta phi tham chiu n kt ni l SQL cho php ngi s dng m nhiu kt ni nhng ti mt thi im ch c mt kt ni hot ng. chuyn qua chuyn li gia cc kt ni, chng ta c th s dng lnh SET CONNECTION conn1; Lnh ny kch hot kt ni c tn l conn1, kt ni hin thi tr thnh ng. Mt kt ni ng c kch hot li bng lnh SET CONNECTION c ghi r tn n. Chng ta cng s dng tn kt ni khi mun nh ch kt ni. V d, chng ta c th nh ch kt ni conn1 bng lnh: DISCONNECT conn1; By gi conn1 kt thc. N khng phi ng v khng th kch hot li c. Tuy nhin, nu chng ta s khng bao gi cn tham chiu n mt kt ni c to ra th AS v tn kt ni c th b b khi lnh CONNECT TO. Vic chuyn cc lnh kt ni lin tip nhau cng c cho php. Nu chng ta thc hin cc lnh SQL mt cch n gin ti mt trm c SQL Client th mt kt ni mc nh s c thit lp thay cho chng ta. 3.3.6 Phin (Session) Cc thao tc SQL c thc hin khi mt kt ni hot ng c gi l mt phin. Phin tn ti chung cng vi kt ni to ra n. V d, khi mt kt ni c lm ng, phin ca n cng tr thnh ng v vic hot ng tr li ca kt ni bng lnh SET CONNECTION cng lm cho phin hot ng. Nh vy, chng ta ch ra phin v kt ni nh hai mt ca kt ni gia Client v Server trong hnh 3.17

SQL-agent Module

Mi trng

Kt ni Phin

133

SQL CLIENT

SQL SERVER

Hnh 3.17: Cc giao tip SQL client-server Mi phin c mt danh mc hin ti v mt lc hin ti bn trong danh mc . Chng c th c thit lp bng cc lnh SET SCHEMA v SET CATALOG tng ng. Vi mi phin c mt ngi s dng c php. 3.3.7 Modules Mt module l mt thut ng SQL dnh cho mt chng trnh ng dng. Chun SQL xut ba loi module nhng ch nhn mnh rng mi ci t SQL cung cp cho ngi dng t nht l mt trong cc loi . 1. Giao din SQL chung (generic SQL interface): Ngi s dng g vo cc lnh SQL, cc lnh ny s c mt SQL erver thc hin. Trong ch ny, mt lnh hoc mt truy vn chnh l mt module. a s cc v d c cho trong ti liu ny l mt module thuc loi ny mc d trong thc t n rt t c s dng. 2. SQL nhng: Kiu ny c trnh by phn 3.1, trong cc lnh SQL xut hin bn trong cc chng trnh ngn ng ch v c gii thiu bng EXEC SQL. Trc tin, mt b tin x l s bin i cc lnh SQL nhng thnh cc li gi hm hoc th tc n h thng SQL thch hp. Chng trnh ngn ng ch c bin dch, bao gm c cc li gi ny, l mt module. 3. Cc module thc th (True Modules): Dng tng qut nht ca module do SQL cung cp l dng m trong c mt tp cc hm hoc cc th tc c lu gi, mt s trong chng l m ngn ng ch v mt s l cc cu lnh SQL. Chng trao i vi nhau bng cch chuyn tham s thng qua cc bin dng chung. Cc module PSM l cc v d v kiu module ny. Mt thc hin ca mt module c gi l mt SQL-agent. Trong hnh 3.17 chng ta ch ra c mt module v mt SQL-agent, nh l mt n v 134

yu cu mt SQL Client thit lp mt kt ni. Tuy nhin, chng ta cn nh rng s khc nhau gia mt module v mt SQL-agent tng t nh s khc nhau gia mt chng trnh v mt tin trnh. Chng trnh l m lnh cn tin trnh l s thc hin cc m lnh .

3.4 S DNG GIAO DIN MC GI (call-level interface)


Trong phn ny chng ta tr li vic kt hp cc thao tc SQL v cc chng trnh ngn ng ch. Chng ta nhn thy SQL nhng trong phn 3.1, chng ta cng ni n cc th tc c lu gi trong lc phn 3.2. Trong phn ny chng ta tho lun mt cch tip cn th ba, l vic s dng giao din mc gi. Khi s dng mt giao din mc gi (call-level interface CLI), chng ta vit mt chng trnh bng mt ngn ng ch thng thng v chng ta s dng mt th vin cc hm, cc hm ny cho php chng ta kt ni n v truy cp mt c s d liu bng cch chuyn cc lnh SQL cho c s d liu . S khc nhau gia cch tip cn ny v lp trnh SQL nhng, theo mt ngha no , l hnh thc. Nu chng ta quan st b tin x l lm g vi cc lnh SQL nhng, chng ta c th tm thy rng chng c thay th bng cc li gi n cc hm th vin ging nh cc hm trong chun SQL/CLI. Tuy nhin, khi cc hm CLI chuyn SQL mt cch trc tip n my ch c s d liu, c mt ch li v mt mc c lp h thng no y. Nh vy, v nguyn tc, chng ta c th chy cng mt chng trnh ngn ng ch ti nhiu trm c s dng cc h qun tr c s d liu khc nhau. Chng no cc h qun tr c s d liu ny chp nhn SQL chun th cng mt chng trnh c th chy ti mi trm m khng cn cc b tin x l c thit k c bit. Mt trong cc giao din mc gi l SQL/CLI chun. 3.4.1 Nhp mn SQL/CLI Mt chng trnh c vit trong ngn ng C v s dng SQL/CLI s cha file u sqlcli.h, t n nhn c mt s ln cc hm, cc nh ngha kiu, cc cu trc v cc hng k hiu. Sau chng trnh c th to ra v lm vic vi 4 loi bn ghi: 135

1. Mi trng: Mt bn ghi kiu ny c to ra bng mt chng trnh ng dng (client) trong vic chun b cho mt hoc nhiu kt ni n server c s d liu. 2. Kt ni: Mt trong cc bn ghi ny c to ra kt ni chng trnh ng dng vi c s d liu. Mi kt ni tn ti bn trong mt mi trng no y. 3. Cu lnh: Mt chng trnh ng dng c th to ra mt hoc nhiu bn ghi cu lnh. Mi bn ghi gi thng tin v mt lnh SQL n gin bao gm mt cursor mc nhin nu cu lnh l mt truy vn. cc thi im khc nhau, cng mt lnh CLI c th biu din cc lnh SQL khc nhau. Mi mt cu lnh CLI tn ti bn trong mt kt ni no y. 4. M t: Cc bn ghi ny gi thng tin v cc b hoc v cc tham s. Chng trnh ng dng hoc my ch c s d liu s thit lp cc thnh phn ca cc bn ghi m t ch ra tn v kiu ca cc thuc tnh v/hoc cc gi tr ca chng. Mi cu lnh c nhiu cc bn ghi c to ra mt cch khng tng minh nh vy, v ngi dng c th to ra nhiu hn nu cn. Trong trnh by ca chng ta v CLI, cc bn ghi m t ni chung l s khng nhn thy. Mi mt bn ghi ny c biu din trong chng trnh ng dng bng mt handle, l mt con tr n bn ghi. File sqlcli.h cung cp cc kiu i vi cc handle mi trng, kt ni, cu lnh, m t tng ng l: SQLHENV, SQLHDBC,SQLHSTMT, SQLHDESC, mc d chng ta c th ngh v chng nh cc bin tr hoc cc s nguyn. Chng ta s s dng cc kiu ny v mt s kiu c nh ngha khc vi cc gii thch r rng, nh l SQLCHAR v SQLINTEGER. Cc kiu ny c cung cp trong sqlcli.h. Chng ta s khng i n chi tit v vic cc m t c thit lp v c s dng nh th no. Tuy nhin ta s bn v ba kiu bn ghi khc c to ra bng cch s dng mt hm SQLAllocHandle(hType, hIn, hOut) Trong , ba tham s l: 136

1. hType l kiu ca handle mong mun. S dng SQL_HANDLE_ENV cho mt mi trng mi, SQL_HANDLE_DBC cho mt kt ni mi, hoc SQL_HANDLE_STMT cho mt cu lnh mi. 2. hIn l mt handle ca phn t mc cao trong d phn t va mi to ra sng. Tham s ny l SQL_NULL_HANDLE nu chng ta mun mt mi trng; tn ca mi trng l mt hng c nh ngha ni vi SQLAllocHandle rng khng c gi tr thch hp y. Nu chng ta mun mt handle kt ni th hIn l handle ca mi trng m trong c tn ti kt ni. Nu chng ta mun mt handle cu lnh, th hIn l handle ca kt ni m trong cu lnh s tn ti. 3. hOut l a ch ca handle do SQLAllocHandle to ra. SQLAllocHandle cng tr li mt gi tr thuc kiu SQLRETURN (mt s nguyn). Gi tr ny l 0 nu khng c li no xy ra, v l mt gi tr khc 0 nu c xut hin li. V d 3.18 Chng ta hy xem hm HangLuong ca hnh 3.4 s bt u trong CLI nh th no. (Hm HangLuong c chng ta s dng nh mt v d v SQL nhng). Nh li rng hm ny kho st tt c cc b ca NHNVIN v xp loi lng ca chng. Cc bc u tin c ch ra hnh 3.1.8: 1) #include sqlcli.h 2) SQLHENV MyEnv; 3) SQLHDBC MyCon; 4) SQLHSTMT execStat 5) SQLRETURN errorCode1, errorCode2, errorCode3; 6) ErrorCode1=SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &myEnv); 7) If(!errorCode1) 8) ErrorCode2 &myCon); = SQLAllocHandle(SQL_HANDLE_DCB, myEnv,

9) If(!errorCode2)

137

10) ErrorCode3=SQLAllocHandle(SQL_HANDLE_STMT,myCon,&exec Stat); Hnh 3.18: M t v to ra mt mi trng, mt kt ni v mt lnh.

Cc dng t 2) n 4) m t handle cho mt mi trng, kt ni v cu lnh tng ng. Tn ca chng l myEnv, myCon v execStat tng ng. Chng ta d kin rng execStat s biu din lnh SELECT Luong FROM NHNVIN ; ging nh con tr execCursor lm trong hnh 3.4 nhng vn cha c lnh SQL no c lin kt vi execStat. Dng 5) khai bo ba bin m cc li gi hm c th t tr li ca chng vo v ch ra mt li. Mt gi tr 0 ch ra khng c li no xy ra trong li gi v chng ta ang mong i trng hp . Dng 6) gi SQLAllocHandle, yu cu mt handle mi trng (i s th nht), cung cp mt handle null trong i s th hai (bi v khi chng ta ang yu cu mt handle mi trng th chng ta khng cn g c) v cung cp mt a ch ca myEnv nh l i s th ba; handle c to ra s c t y. Nu dng 6) l thnh cng, cc dng 7) v 8) s dng handle mi trng nhn mt handle kt ni trong myCon. Gi thit rng li gi cng thnh cng, cc dng 9) v 10) nhn mt handle lnh cho execStat. 3.4.2 X l cc lnh cui ca hnh 3.18, mt bn ghi lnh c handle l execStat c to ra. Tuy nhin bn ghi ny vn cha c ni vi lnh SQL. Tin trnh kt ni v thc hin cc lnh SQL vi cc handle tng t nh SQL ng c m t trong phn 3.1.10. y, chng ta kt hp vn bn ca mt lnh SQL vi mt bin SQL bng cch s dng PREPARE v sau thc hin n bng cch s dng EXECUTE. Tnh trng trong CLI hon ton tng t nu chng ta ngh v bin SQL nh l mt handle lnh. C mt hm SQLPrepare (sh, st, sl), n ly:

138

1. Mt handle lnh sh 2. Mt bin tr n mt lnh SQL st, v 3. Mt di sl cho xu k t do st ch n. Nu chng ta khng bit di, mt hng c nh ngha SQL_NTS yu cu SQLPrepare t tnh n t xu. C th l xu l mt xu kt thc bng null v SQLPrepare kim tra n cho n khi gp du ht xu \0 l . Hiu qu ca hm ny l chun b cho lnh c handle sh tr n by gi biu th lnh SQL c th st. Mt hm khc SQLExecute(sh) lm cho lnh do sh tr n c thc hin. i vi nhiu dng lnh SQL, chng hn nh cc lnh chn hoc cc lnh xa, hiu qu ca vic thc hin lnh ny trn c s d liu l hin nhin. Khi lnh SQL do sh tham chiu n l mt truy vn, hiu qu ca lnh km r rng hn. Nh chng ta s thy trong phn 8.4.3, c mt con tr n cho lnh ny, con tr l mt phn ca chnh bn ghi lnh. V nguyn tc lnh s c thc hin, v vy chng ta c th tng tng rng tt c cc b ca tr li ang nm mt ni no sn sng c truy cp. Chng ta c th ly ra cc b, mi ln mt b bng cch s dng con tr n ging nh chng ta lm vic vi cc con tr tht trong cc phn 3.1 v 3.2. V d 3.19: Chng ta hy tip tc vi hm HangLuong v d 3.18. Hai li gi hm sau y s lin kt truy vn SELECT Luong FROM NHANVIEN vi lnh do handle exectStat tr n: 11)SQLPrepare(execStat, SELECT Luong FROM NHANVIEN , SQL_NTS); 12)SQLExecute(execStat) ; Hai lnh ny c th xut hin ngay sau dng 10) ca hnh 3.18. Nh li rng SQL_NTS yu cu SQLPrepare xc nh di ca xu kt thc bng null do i s th hai ca n tham chiu n.

139

Ging nh vi SQL ng, cc bc chun b v thc hin c th c t hp thnh mt nu chng ta s dng hm SQLExecDirect. Mt v d v t hp cc dng 110 v 12) nh sau: SQLExecDirect(execStat, SELECT Luong FROM NHANVIEN , SQL_NTS);

3.4.3 Ly d liu ra t kt qu truy vn Hm tng ng vi lnh FETCH trong SQL nhng hoc PSM l SQLFetch(sh) trong sh l mt handle lnh. Chng ta gi thit lnh do sh tr n c thc hin, hoc vic ly ra s gy ra mt li. SQLFetch, ging nh tt c cc hm CLI, tr li mt gi tr kiu SQLRETURN ch ra hoc thnh cng hoc b li. Chng ta c th nhn thy rng gi tr tr v do hng k hiu SQL_NO_DATA biu th, ch ra rng khng b no cn li trong kt qu truy vn. Ging nh trong cc v d trc y ca chng ta v vic ly ra, gi tr ny s c s dng i ra khi vng lp m trong chng ta ly ra lin tip cc b t kt qu truy vn. Tuy nhin, nu chng ta t sau SQLExecute ca v d 8.19 mt hoc nhiu dng gi SQLFetch th b xut hin u? Cu tr li l cc thnh phn ca n i vo mt trong cc bn ghi m t lin kt vi lnh m handle ca n xut hin trong li gi SQLFetch. Chng ta c th rt ra cng mt thnh phn ti mi ln th bng cch lin kt thnh phn vi mt bin ngn ng ch trc khi chng ta bt u ly ra. Hm lm nhim v l SQLBindCol (sh, colNo, colType, pVar, varSize, varInfo) ngha ca su i s ny l: 1. sh l handle ca ca lnh lin quan 2. colNo l s ca thnh phn (bn trong b) m chng ta nhn gi tr ca n

140

3. colType l m ca kiu ca bin m gi tr ca thnh phn c t vo y. V d v cc m do sqlcli.h cung cp l SQL_CHAR i vi cc mng hoc cc xu k t, SQL_INTEGER i vi cc s nguyn. 4. pVar l mt bin tr ch n bin m gi tr c t vo . 5. varSize l di tnh bng byte ca gi tr ca bin c pVar ch n. 6. varInfor l bin tr ch n mt s nguyn c th c SQLBinCol s dng cung cp thng tin ph v gi tr c sn xut ra. V d 3.20. Chng ta lm li ton b hm HangLuong t hnh 3.4 bng cch s dng cc dng gi CLI thay cho SQL nhng. Chng ta bt u t Hnh 3.18 nhng ngn gn, chng ta b qua cc kim tra li tr kim tra xem c phi SQLFetch ch ra rng khng cn b no xut hin hay khng. Chng trnh c ch ra hnh 3.19: 1) 2) 3) 4) #include sqlcli.h void Hnglng () { int i, Chuso, em[15]; SQLHENV MyEnv;

5) SQLHDBC MyCon; 6) SQLHSTMT execStat 7) SQLINTEGER luong, luongInfo; 8) SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE, &myEnv); 9) SQLAllocHandle(SQL_HANDLE_DCB,myEnv, &myCon); 10) SQLAllocHandle(SQL_HANDLE_STMT,myCon,&execStat); 11) SQLPrepare(execStat, SELECT Luong FROM NHANVIEN , SQL_NTS); 12) SQLExecute(execStat) ; 13) SQLBindCol(execStat, 1, SQL_INTEGER, &luong, size(luong), &luongInfo); 14) While(SQLFetch (execStat != SQL_NO_DATA { 141

15) Chuso = 1; 16) while((LuongNV /= 10) > 0) Chuso++; 8) if(Chuso <= 14) Dem[Chuso]++; } 9) for (i=0; i<15; i++) 10) printf(chuso = %d: so nhan vien = %d\n, i, Dem[i]); } Hnh 3.19 m s nhn vin c lng gm 1 ch s, 2 ch s, . Dng 3) khai bo cc bin cc b ging nh trong SQL nhng v cc dng 4) v 7) khai bo cc bin cc b thm bng cch s dng cc kiu c sqlcli.h cung cp.

3.5 GIAO TC TRONG SQL


Cho n lc ny, m hnh cc thao tc trn c s d liu ca chng ta l mt ngi s dng ang truy vn v sa i c s d liu. Nh vy, cc thao tc trn c s d liu c thc hin mt ln mt thao tc, v trng thi c s d liu li sau mi thao tc l trng thi m thao tc tip theo s hnh ng trn . Hn na, chng ta hnh dung rng cc thao tc c thc hin trong trng thi nguyn vn ca n (mt cch nguyn t). Nh vy, chng ta gi thit rng phn cng v phn mm khng th hng gia thao tc, li c s d liu trong tnh trng khng th gii thch c nh l kt qu ca thao tc thc hin trn n. Cuc sng thc thng l phc tp hn nhiu. Trc tin chng ta xem xt ci c th xy ra lm cho c s d liu trng thi khng phn nh cc thao tc thc hin trn n v sau chng ta s xem xt cc cng c SQL cung cp cho cc ngi s dng m bo rng nhng vn khng xy ra. 3.5.1 Xp hng theo th t Trong cc ng dng nh l ngn hng hoc ng k v my bay, mi giy c hng trm thao tc c th thc hin trn c s d liu. Cc thao tc ny bt u ti mt trong hng trm hoc hng nghn trm nh l my trn bn

142

ca mt i l du lch, nhn vin hng khng, . Vic chng ta c th c hai thao tc thc hin trn cng mt ti khon hoc mt chuyn bay v chng cho thi gian l iu hon ton c th xy ra. Nu nh vy, chng c th tng tc theo cc cch l lng. y l mt v d v ci c th dn n sai lm nu nh h qun tr c s d liu khng b rng buc y i vi th t m theo th t n thao tc trn c s d liu. Chng ta nhn mnh rng cc h c s d liu khng x s mt cch bnh thng theo cch ny. V rng ngi ta c th i ra theo cch ca ngi ta lm cho cc loi sai lm ny xy ra khi s dng mt h qun tr thng mi. 1) EXEC SQL BEGIN DECLARE SECTION; 2) int flight ; /* s chuyn bay */ 3) char date[10] ; /*ngy bay theo khun dng SQL */ 4) char seat[3] ; /* hai ch s v mt ch ci biu th mt ch ngi */ 5) int occ ; /* bin logic xem ch c b bn hay cha */ 6) EXEC SQL END DECLARE SECTION ; 7) void chooseSeat () { 8) /* M chng trnh C nhc ngi s dng nhp vo chuyn bay, ngy v ch ngi v lu gi chng vo ba bin c tn trn */ 9) EXEC SQL UPDATE occupied INTO:occ 10) FROM Flights 11) WHERE fltNum =:flight AND fltDate =: date AND fltSeat =:seat; 12) if (!occ) { 13) EXEC SQL UPDATE Flights 14) SET occupied = TRUE 15) WHERE fltNum =: flight AND fltDate =:date AND fltSeat = seat ; 16) /* M chng trnh C ghi li vic phn ch v thng bo cho ngi s dng vic phn ch */ }

143

17) else /* m chng trnh C thng bo cho ngi s dng vic ch b chim v yu cu chn ch khc */ } Hnh 3.22: Chn mt ch V d 3.26: Gi s rng chng ta vit mt hm chooseSeat() trong C vi SQL nhng c mt quan h v cc chuyn bay v cc ch sn c, tm mt ch sn c c th v lm cho n tr thnh b chim. Quan h m chng ta thao tc trn c gi l Flights v n c cc thuc tnh fltNum, ftlDate, ftlSeat, v occ vi ngha r rng. T dng (9) n dng (11) l mt select n hng v lm cho bin chia s occ tr thnh true hoc false (1 hoc 0) ph thuc vo vic ch c ch ra b chim hay cha. Dng (12) kim tra xem ch b chim hay cha, v nu cha, b gi tr i vi ch ny s c cp nht lm cho n tr thnh b chim. Vic cp nht c thc hin t dng (13) n dng (15) v ti dng (16) ch c phn cho ngi s dng yu cu n. Trn thc t, chng ta c th lu tr thng tin phn ch vo mt quan h khc. Cui cng, dng (17), nu ch b chim th ngi s dng cng c thng bo nh vy. By gi hy nh rng hm chooseSeat () c th c hai hay nhiu khch hng thc hin mt cch ng thi. Gi s rng hai i l ang c gng mua cng mt ch i vi cng chuyn bay v cng ngy ti cng mt thi im nh gi trong hnh 3.23. C hai i n dng (9) cng mt lc v c hai bn sao bin cc b occ nhn gi tr 0; nh vy, ch ngi hin ang cn trng. Ti dng (12), mi thc hin ca chooseSeat() quyt nh sa i occ thnh TRUE v nh vy l lm cho ch thnh b chim. Cc cp nht ny thc hin ng thi v mi thc hin ni vi khnh hng dng (16) rng ch thuc v h. Nh chng ta thy t v d 3.26, c th tha nhn rng hai thao tc c th c thc hin mt cch ng n nhng kt qu cui cng l khng ng n: c hai khch hng tin rng ng k c ch theo yu cu. Vn c th c gii quyt bng nhiu c cu SQL phc v cho xp hng c th

144

t s thc hin ca hai thc hin hm. Chng ta ni v cc hm thao tc trn cng mt c s d liu l c th t nu mt mt hm thc hin hon thnh trc khi mt hm khc bt u. Chng ta ni thc hin l lm c th t c nu chng chng x s nh l chng chy mt cch c th t d rng vic thc hin ca chng l c thi gian gi ln nhau. R rng l nu hai li gi hm chooSeat() chy c th t th sai st m chng ta nhn thy khng th xy ra. Mt li gi ca khch hng xut hin u tin. Khch hng ny nhn thy mt ch trng v mua ch . Sau li gi ca khch hng th hai bt u v nhn thy ch b chim. N c th quan trng i vi nhng khch hng ng k ch nhng i vi c s d liu, iu quan trng l mt ch ch c bn mt ln. 3.5.2 Atomicity Bn cnh cch x s khng c th t c th xy ra nu c hai hoc nhiu php ton c s d liu thc hin cng mt thi im, cn c th c kh nng mt php ton n t c s d liu vo mt trng thi khng chp nhn c nu c mt s sp phn cng hoc phn mm khi php ton ang thc hin. y l mt v d khc gi ci g c th xy ra. Cng nh trong v d 8.26, chng ta phi nh rng cc h c s d liu thc s khng cho php kiu sai st ny xy ra trong cc chng trnh ng dng c thit k ng n. V d 3.27: Chng ta xt mt loi c s d liu khc: cc bn ghi ti khon ca ngn hng. Chng ta c th trnh by tnh hung bng mt quan h Accounts vi cc thuc tnh accNo v balance. Cp trong quan h ny l m s ti khon v s d trong ti khon . Chng ta mun vit mt hm transfer() a vo hai ti khon v mt s tin, kim tra xem ti khon th nht c t nht l s tin hay khng v nu c th chuyn tin t ti khon th nht sang ti khon th hai. Hnh 8.24 l phc tho ca hm transfer(): 1) EXEC SQL BEGIN DECLARE SECTION; 2) int acct, acct2 ; /* hai ti khon */ 3) int balance1 ; /* s tin trong ti khon th nht */ 145

4) int amount ; /* s tin cn chuyn */ 5) EXEC SQL END DECLARE SECTION; 6) void transfer() { 7) /* M C nhc ngi s dng nhp vo cc ti khon 1 v 2 v mt s tin cn chuyn, vo cc bin acct1, acct2, v amount. */ 8) EXEC SQL SELECT balance INTO:balance1 9) FROM Accounts 10) WHERE acctNo =:acct1 ; 11) if (balance1 >= amount) { 12) EXEC SQL UPDATE Accounts 13) SET balance = balance +:amount 14) WHERE acctNo =:acct2 ; 15) EXEC SQL UPDATE Accounts 16) SET balance = balance -:amount 17) WHERE acctNo =:acct1 ; } 18) else /* M C in thng bo rng khng c tin chuyn */ Hnh 3.24 Chuyn tin t ti khon ny sang ti khon khc. Cng vic ca hnh 3.24 l k tip. Cc dng (8) n dng (10) ly ra s d ca ti khon th nht. dng (11), kim tra xem s d c rt s tin mong mun ra hay khng. Nu , th cc dng t (12) n (14) thm s tin vo ti khon th hai v cc dng t (15) n (17) tr s tin ra khi ti khon th nht. Nu s tin trong ti khon th nht l khng th khng c s chuyn tin xy ra v mt li cnh bo s c in ra dng (18). By gi chng ta xem ci g xy ra nu c mt h hng sau dng (14). C th l mt s h hng ca my tnh hoc mng ni c s d liu vi b x l ang thc hin vic chuyn tin hng. Khi c s d liu c t 146

trng thi tin c chuyn vo ti khon th hai nhng cha c tr i khi ti khon th nht. Kt qu l ngn hng mt s tin c chuyn i. Vn c minh ha bi v d 3.27 l mt t hp cc php ton c s d liu, nh l hai php update hnh 3.24, cn c thc hin mt cch nguyn t, ngha l c hai u c thc hin hoc khng php ton no c thc hin. V d, mt li gii n gin lm tt c cc thay i trong mt ni cc b v ch sau khi tt c cng vic c thc hin chng ta s ghi cc thay i vo c s d liu, v sau tt c cc thay i tr thnh mt phn ca c s d liu v nhn thy c i vi cc php ton khc. 3.5.3 Giao tc (Transaction) Cch gii quyt cc vn v xp hng th t v nguyn t ha t ra trong cc phn 3.6.1 v 3.6.2 l nhm cc php ton vo cc giao tc. Mt giao tc l mt tp hp ca mt hoc nhiu php ton trn c s d liu sao cho chng c thc hin mt cch nguyn t; ngha l hoc tt c cc php ton c thc hin hoc khng php ton no c thc hin. Hn na, SQL i hi rng, nh ngm nh, cc giao tc c thc hin theo cch xp hng th t. Mt h qun tr c s d liu c th cho php ngi s dng ch ra mt rng buc t nghim ngt hn trn vic chn ca cc php ton t hai hoc nhiu giao tc. Chng ta s tho lun nhng sa i i vi iu kin xp hng th t ny trong cc phn sau. Khi s dng giao din SQL chung, mi mt lnh chnh l mt giao tc. Tuy nhin, Khi vit chng trnh vi SQL nhng hoc chng trnh s dng SQL/CLI hoc JDBC, chng ta thng mun kim tra cc giao tc mt cch r rng. Cc giao tc bt u mt cch t ng khi mt lnh SQL bt u truy vn hoc thao tc c s d liu hoc lc . Nu mun chng ta c th s dng lnh SQL START TRANSACTION. Trong giao din chung, tr phi c bt u bng lnh START TRANSACTION giao tc kt thc cng vi lnh. Trong cc trng hp khc, c hai cch kt thc mt giao tc: 1. Lnh SQL COMMIT khin giao tc kt thc mt cch thnh cng. Bt c ci g lm thay i i vi c s d liu c gy ra do mt lnh hoc cc lnh SQL t khi giao tc hin ti bt u s c t thng 147

trc vo c s d liu (tc l chng c gi li). Trc khi lnh COMMIT c thc hin, cc thay i l khng dt khot v c th hoc khng th nhn thy c i vi cc giao tc khc. 2. Lnh SQL ROLLBACK khin giao tc kt thc khng thnh cng (hoc b d). Mi thay i trong vic tr li cho cc lnh SQL ca giao tc l khng thc hin (tc l chng c rolled back), v vy chng khng cn xut hin trong c s d liu. C mt ngoi l i vi hai im trn. Nu chng ta c gng lu gi mt giao tc nhng c cc rng buc chm v cc rng buc by gi b vi phm th giao tc khng c ghi li d chng ta ni vi n bng lnh COMMIT. Hn na, giao tc s b b d v mt ch dn trong SQLSTATE ni vi ng dng rng giao tc b b d do nguyn nhn ny. V d 3.28: Gi s chng ta mun mt thc hin ca hm transfer() ca hnh 8.24 l mt giao tc n. Giao tc bt u dng (8) khi chng ta c s d ca ti khon th nht. Nu kim tra dng (11) l ng v chng ta thc hin vic chuyn tin th chng ta c th mun ghi li cc thay i c lm. Nh vy, chng ta t cui khi if t dng (12) n dng (17) thm lnh SQL EXEC SQL COMMIT ; Nu kim tra dng (11) l sai ngha l khng c tin thc hin vic chuyn th chng ta c th b d vic chuyn. Chng ta c th lm nh vy bng cch t EXEC SQL ROLLBACK cui khi else dng (18). Hin ti, bi v trong nhnh ny khng c lnh sa i c s d liu no c thc hin, chng ta lu gi (commit) hoc b d (abort) cng khng vn g bi v khng c thay i no c lu gi c. 3.5.4 Read-Only Transaction Mi v d 3.26 v 3.27 ko theo mt giao tc c v sau c th ghi mt vi d liu vo c s d liu. Loi giao tc ny nghing v cc vn xp hng th t. Nh chng ta nhn thy trong v d 3.26 ci g c th xy ra nu hai thc hin ca hm c gng mua cng mt ch ti cng mt thi im, v chng ta cng nhn thy trong v d 3.27 ci g c th xy ra nu 148

c mt s hng hc trong thi gian thc hin hm. Tuy nhin, khi mt giao tc ch c d liu v khng ghi d liu chng ta s t do hn trong vic cho mt giao tc thc hin song song vi cc giao tc khc. V d 3.29: Gi s chng ta vit mt hm c cc d liu xc nh xem mt ch no c sn sng hay khng, hm ny s x s ging nh dng (1) n dng (11) ca hnh 8.22. Chng ta c th thc hin nhiu ln gi hm ny cng mt lc m khng s lm hi n c s d liu. iu t hi nht c th xy ra l khi chng ta ang c v s sn sng ca mt ch no th ch c th b mua hoc c gii phng bi s thc hin ca mt hm khc no . Nh vy, chng ta c th nhn c cu tr li sn sng hoc b chim trong khon thi gian rt ngn khi ta thc hin truy vn nhng cu tr li s c ngha trong mt lc. Nu chng ta bo vi h thng thc hin SQL rng giao tc hin ti ca chng ta l read-only ngha l chng s chng bao gi lm thay i c s d liu th h thng SQL hon ton c th c kh nng nhn u im ca kin thc . Ni chung, vic nhiu giao tc read-only truy cp n cng mt d liu chy song song l c th c, trong khi chng s khng cho php chy song song vi mt giao tc ghi cng mt d liu. Chng ta bo cho h thng SQL rng giao tc tip theo sau l read-only bng lnh SET TRANSACTION READ ONLY ; Lnh ny phi c thc hin trc khi giao tc bt u. V d, nu chng ta c mt hm bao gm cc dng t (1) n dng (11) ca hnh 3.22, chng ta c th khai bo n l read only bng cch t EXEC SQL SET TRANSACTION READ ONLY ; ngay trc dng (9), ni bt u giao tc. Nu khai bo read-only sau dng (9) th s qu mun. Chng ta cng c th thng bo cho SQL rng giao tc sp ti c th ghi d liu bng lnh SET TRANSACTION READ WRITE ;

149

Tuy nhin, ty chn ny l ngm nh v lm iu l khng cn thit. 3.5.5 Dirty Read Cc d liu bn (dirty data) l mt thut ng chung ch cc d liu c ghi bng mt giao tc nhng cn cha c lu gi li (committed). Mt dirty read dng c cc d liu bn. iu nguy him ca vic c cc d liu bn l ch mt giao tc ghi n c th b b d. Nu vy th cc d liu bn s b y ra khi c s d liu v mi ngi c php x s nh l cc d liu cha bao gi tn ti. Nu mt giao tc khc no c cc d liu bn th giao tc c th lu gi hoc thc hin mt hnh ng no phn nh s hiu bit ca n v d liu bn. i lc dirty read c ngha, i lc n khng c ngha. Lc khc n c ngha rt nh to ngha v nguy c ca mt dirty read ph ng v nh vy lm ngn cn: 1. Cng vic tn thi gian ca h qun tr c s d liu cn ngn nga dirty read v 2. Mt tnh song song gy ra t s ch i cho n khi khng c th c mt dirty read. Sau y l mt s v d v nhng ci c th xy ra khi cho php c cc dirty read. V d 3.30: Chng ta hy xem xt vic chuyn ti khon ca v d 8.27. Tuy nhin, gi s rng cc v chuyn c thc hin bng mt chng trnh P thc hin dy cc bc sau y: 1. Thm tin vo ti khon 2 2. Kim tra nu ti khon 1 c tin a) Nu khng c tin, ly tin ra khi ti khon 2 v kt thc b) Nu c tin, tr s tin t ti khon 1 v kt thc. Nu chng trnh P c thc hin mt cch c th t th vic chng ta thm tin tm thi vo ti khon 2 s khng c ngha g. Khng ai s nhn thy s tin v v n s b loi b nu vic chuyn tin l khng thc hin c. 150

Tuy nhin, gi s rng c cc dirty read. Hy tng tng c 3 ti khon A1, A2, A3 vi 100$, 200$ v 300$ tng ng. Gi s rng giao tc T1 thc hin chng trnh P chuyn 150$ t A1 n A2. Cng mt thi gian, giao tc T2 chy chng trnh P chuyn 250$ t A2 n A3. C kh nng c cc dy s kin sau: 1. T2 thc hin bc 1 v thm 250$ vo A3 v by gi A3 c 550$ 2. T1 thc hin bc 1 v thm 150$ v A2 v by gi A2 c 350$ 3. T2 thc hin kim tra ca bc 2 v tm ra rng A2 c tin (350$) cho php chuyn 250$ t A2 sang A3. 4. T1 thc hin kim tra ca bc 2 v tm ra rng T1 khng c tin (100$) cho php chuyn 150$ t A1 sang A2. 5. T2 thc hin bc 2b. N tr i 250$ khi A2 v by gi A2 c 100$ v kt thc. 6. T1 thc hin bc 2a. N tr 150$ khi A2, by gi A2 c 50$ v kt thc. Tng s tin khng thay i; trong ba ti khon vn cn 600$. Nhng bi v T2 c d liu bn bc 3 trong 6 bc trn, chng ta khng bo v c vic mt ti khon tr nn m, l mc ch ca vic kim tra ti khon th nht xem ti khon ny c s tin thch hp hay khng. V d 3.31: Chng ta hy xt mt thay i trn hm seat-choosing ca v d 8.26. Trong cch tip cn mi: 1. Chng ta tm thy mt ch sn sng v ng k n bng cch lm cho occ thnh TRUE i vi ch . 2. Chng ta hi khch hng c ng vi ch. Nu khch hng ng , ta gi (commit). Nu khng, ta gii phng ch bng cch lm cho occ thnh FALSE v lp li bc 1 ly ch khc. Nu hai giao tc ang thc hin thut ton ti cng mt thi im, mt giao tc c th ng k ch S v sau li gii phng n do khch hng. Nu giao tc th hai thc hin bc 1 ti thi im khi ch S c nh du b

151

chim, khch hng i vi giao tc ny s khng c cho la chn ly ch S. Cng nh trong v d 8.30, vn l ch c dirty read. Giao tc th hai nhn thy b gi tr (vi S c nh du l b chim) c giao tc th nht ghi v sau c giao tc th nht sa i. S kin mt read l dirty l quan trng nh th no? Trong v d 30 n rt quan trng; n gy ra mt ti khon tr thnh m mc d b phn an ton chng li iu . Trong v d 8.31, vn khng n ni quan trng lm. Hin nhin, khch hng th hai c th khng chn c ch ng hoc ngay c c tr li l khng cn ch no. Tuy nhin, trong trng hp sau, khi chy li giao tc mt ln na th hu nh s nhn c s sn sng ca ch S. Vic ci t hm seat-choosing ny theo cch cho php cc dirty read lm nhanh thi gian x l trung bnh i vi cc yu cu mua v l mt iu c ngha. SQL cho php chng ta ch ra rng cc dirty read l chp nhn c vi mt giao tc cho trc. Chng ta s dng lnh SET TRANSACTION. Dng thch hp cho mt giao tc ging nh m t trong v d 8.31 l: 1) SET TRANSACTION READ WRITE 2) ISOLATION LEVEL READ UNCOMMITTED ; Lnh trn lm hai vic: 1. Dng (1) khai bo rng giao tc c th ghi d liu 2. Dng (2) khai bo rng giao tc c th chy vi isolation level read-uncomitted. iu c ngha l giao tc c cho php c cc d liu bn. Ch rng, nu giao tc khng phi l read-only (tc l c th sa i c s d liu) v chng ta ch ra mc c lp (isolation level) READ UNCOMMITED th chng ta cng phi ch ra READ WRITE. Nhc li t phn 8.6.4 rng gi thit ngm nh l cc giao tc l read-write. Tuy nhin SQL c mt ngoi l i vi trng hp c cho php cc dirty-read. Trong trng hp , gi thit ngm nh l giao tc l read-only, bi v cc giao

152

tc read-write vi dirty read gy ra cc nguy him ng k nh chng ta thy. Nu chng ta mun mt giao tc read-write chy vi read-uncommited nh l mc c lp th chng ta cn ch ra READ WRITE mt cch r rng nh trn. 3.5.6 Cc mc c lp khc SQL cung cp mt tng gm bn mc c lp. Hai mc c xem xt: xp hng th t v read-uncommitted (cho php cc dirty read). Hai mc cn li l read-commited v repeatable- read. Chng c th c ch ra i vi mt giao tc cho trc bng SET TRANSACTION ISOLATION LEVEL READ COMMITED ; hoc SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; Tng ng, i vi mi mc, ngm nh l cc giao tc l read write, v vy chng ta c th thm vo READ ONLY cho chng nu mun. Nhn tin, chng ta cng c th c ty chn ch ra SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; Tuy nhin, l iu m SQL ngm nh v khng cn phi ch r. Mc c lp read-committed, nh tn ca n ch ra, ngn cm vic c cc d liu bn (uncommited). Tuy nhin, n cho php mt giao tc a ra cng mt truy vn nhiu ln v nhn cc tr li khc nhau min l cc cu tr li phn nh cc d liu c cc giao tc ghi v c lu gi. V d 3.32: Chng ta hy xem li hm seat-choosing ca v d 8.31. Nhng gi thit rng chng ta khai bo n chy vi mc c lp readcommitted. Nh vy, khi chng ta tm mt ch bc 1, n s khng nhn thy ch nh l c mua nu mt vi giao tc no ang ng k chng nhng cha lu gi. Tuy nhin, nu khch hng khng chp thun ch v mt thc hin ca hm truy vn cc ch sn sng nhiu ln, n c th nhn thy mt tp hp khc cc ch sn sng, nh l cc giao tc khc mua ch thnh cng hoc b ch song song vi giao tc ca chng ta.

153

By gi chng ta xem mc c lp repeatable read. Thut ng i khi dng sai bi v cng mt truy vn c a ra nhiu hn mt ln khng hon ton c m bo l nhn c cng mt cu tr li. Di c lp repeatableread, nu mt b c ly ra ln u tin th chng ta c th tin chc l b s c ly ra li nu truy vn c lp li. Tuy nhin, c kh nng l mt thc hin th hai hoc mt thc hin xy ra sau ca cng mt truy vn s ly ra cc b ma. Cc b l cc b l kt qu ca cc php chn vo c s d liu khi giao tc ca chng ta ang c thc hin. V d 3.33: Chng ta hy tip tc vi vn seat-choosing ca cc v d 3.31 v 3.32. Nu chng ta thc hin hm ny di mc c lp repeatableread th mt ch sn sng trn truy vn u tin bc 1 s cn sn sng cc truy vn xy ra sau. Tuy nhin, gi s rng c mt s b mi c chn vo quan h Flights. V d, cng ty hng khng c th di chuyn t ngt chuyn bay sang mt my bay ln hn, to ra mt s b mi m trc cha c. Khi , di mc c lp repeatable-read, mt truy vn xy ra sau i vi cc ch sn sng cng c th ly ra cc ch mi.

3.6 AN TON V CP QUYN TRONG SQL


SQL i hi s tn ti ca cc authorization ID, chnh l cc tn ngi s dng. SQL cng c mt authorization ID ring, PUBLIC, bao gm mi ngi s dng. Cc authorization ID c th c m bo cc c quyn, ging nh chng trong mi trng h thng file do h iu hnh qun l. V d, mt h thng UNIX ni chung kim sot ba loi c quyn: read, write v excute. Danh sch cc c quyn l c ngha bi v cc i tng c bo v ca h thng UNIX l cc file v ba php ton ny c trng tt cho nhng g ngi ta lm vic vi cc file. Tuy nhin, cc c s d liu phc tp hn cc h thng file nhiu v cc loi c quyn c s dng trong SQL cng phc tp hn tng ng. Trong phn ny, chng ta s xem cc c quyn no SQL cho php trn cc phn t c s d liu. Sau chng ta s xem lm th no cc ngi s

154

dng c th nhn c cc c quyn. Cui cng, chng ta s xem cc c quyn c th b tc i nh th no. 3.6.1 Cc quyn SQL nh ngha 9 loi quyn: SELECT, INSERT, DELETE, UPDATE, REFERENCES, USAGE, TRIGGER, EXECUTE, v UNDER. Bn quyn u p dng i vi quan h, c th l mt bng c s hoc mt view. Nh tn ca n ch ra, cc quyn ny cho ngi c quyn quyn c truy vn (select from) quan h, chn vo quan h, xa khi quan h v sa i cc b gi tr ca quan h. Mt modul cha mt lnh SQl khng th c thc hin m khng c c quyn thch hp i vi lnh ; tc l lnh select-from-where i hi c quyn SELECT trn mi bng m n truy cp n. Chng ta s xem mt modul nhn cc quyn truy cp nh th no mt cch ngn gn. SELECT, INSERT, v UPDATE cng c th c mt danh sch cc thuc tnh lin kt, v d, SELECT(Tn, a ch). Nu c danh sch nh vy th ch c cc thuc tnh ny mi c th c nhn thy trong php la chn, c ch ra trong php chn hoc c thay i trong php sa i. Ch rng, khi c cp, cc c quyn ny s lin kt vi mt quan h c th. Quyn REFERENCES trn mt quan h l quyn tham chiu n mt quan h trong mt rng buc tham chiu. Cc rng buc ny c th c mt trong cc dng c ch ra trong chng 2, nh l cc khng nh, cc kim tra da trn b hoc thuc tnh, hoc cc rng buc ton vn tham chiu. Quyn REFERENCES cng c th c mt danh sch cc thuc tnh km theo, trong trng hp ny, ch cc thuc tnh mi c th c tham chiu trong cc rng buc. Mt rng buc khng th c kim tra tr phi ngi ch ca lc trong xut hin rng buc c c quyn REFERENCES trn tt c cc d liu c trong rng cuc. USAGE l mt quyn p dng cho nhiu loi phn t lc khc vi cc quan h v cc khng nh. (xem 3.3.2); N l quyn s dng phn t trong cc m t ca ngi ch. c quyn TRIGGER trn mt quan h l quyn c nh ngha trigger trn quan h . EXECUTE l quyn thc

155

hin mt mu chng trnh, chng hn nh mt th tc PSM hoc mt hm. Cui cng, UNDER l quyn to ra cc kiu con ca mt kiu cho trc. V d 3.34: Chng ta hy xem xt cc c quyn no l cn thit thc hin lnh chn hnh 8.25 1) INSERT INTO Studio(name) 2) SELECT DISTINCT studioName 3) FROM Movie 4) WHERE studioName NOT IN 5) 6) (SELECT name FROM Studio);

Hnh 3.25 Thm vo cc studio mi. Trc tin, n l mt php chn vo quan h Studio, v vy chng ta i hi mt quyn INSERT trn Studio. Tuy nhin, bi v php chn ch ch ra mt thnh phn i vi thuc tnh name, vic c quyn INSERT hoc c c quyn INSERT(name) trn quan h Studio l chp nhn c. Quyn INSERT(name) cho php chng ta chn cc b ch ch ra thnh phn name v t chi cc thnh phn khc ly cc gi tr ngm nh ca chng hoc NULL. Tuy nhin, lnh chn hnh 3.25 ko theo hai truy vn con, bt u ti cc dng (2) v (5). thc hin hai php chon nychng ta i hi cc quyn cn thit cho cc truy vn con. Nh vy, chng ta cn quyn SELECT trn c hai quan h c trong cc mnh FROM: Studio v Movie. Ch rng vic chng ta va c quyn INSERT trn Studio khng c ngha l chng ta c quyn SELECT trn Studio v ngc li. Bi v ch c cc thuc tnh c th ca Movie v Studio c la chn nn ch cn c c quyn SELECT(studioName) trn Movie v quyn SELECT(name) trn Studio hoc cc c quyn cha cc thuc tnh ny trong danh sch cc thuc tnh l .

156

3.6.2 To cc quyn Chng ta va nhn thy cc c quyn SQL l g v quan st rng chng c yu cu thc hin cc php ton SQL. By gi chng ta cn bit lm th no nhn c cc c quyn cn thit thc hin mt php ton. C hai kha cnh nhn cc c quyn: chng c to ra t u nh th no v chng c chuyn t ngi dng ny sang ngi dng khc nh th no. y chng ta s tho lun v v vic ci t cc c quyn (vic chuyn cc c quyn s c tho lun sau). Trc tin, cc phn t SQL chng hn nh cc lc hoc cc module c mt ch (owner). Ch ca ci g th c tt c cc c quyn i vi ci . C ba im m ch quyn c xc lp trong SQL. 1. Khi mt lc c to ra, n v tt c cc bng, cc phn t lc khc trong n c gi thit l do ngi s dng to ra n lm ch. V vy ngi s dng ny c tt c cc quyn c th trn cc phn t ca lc . 2. Khi mt kt ni c bt u bng mt lnh CONNECT, c mt c hi ch ra ngi s dng vi mt mnh AUTHOIATION. V d, lnh kt ni CONNECT TO Starfleet-sql-server AS conn1 AUTHOIATION nam; s to ra mt kt ni c tn l conn1 n mt server SQL c tn l Starfleet-sql-server nhn danh ngi s dng Nam. 3. Khi mt module c to ra, c mt ty chn cho n mt ch bng cch s dng mnh AUTHOIATION. V d, mnh AUTHOIATION thanh ; trong lnh to module s lm cho ngi s dng Thanh tr thnh ch ca module. Vic khng ch ra ch cho mt module l chp nhn c, trong trng hp module c thc hin mt cch cng cng nhng cc c quyn cn thit thc hin cc php ton trong module phi n t mt

157

ngun khc no , chng hn nh ngi s dng lin kt vi kt ni v phin m trong module c thc hin. 3.6.3 Tin trnh kim tra c quyn Nh chng ta nhn thy trn, mi module, lc v phin c mt ngi s dng lin kt. Trong thut ng SQL, c mt authorizationID lin kt i vi mi i tng. Php ton no ca SQL cng c hai phn: 1. Cc phn t ca c s d liu m php ton c thc hin trn chng v 2. Agen gy nn php ton. Cc c quyn sn sng cho agent rt ra t mt authorizationID c th gi l authorizationID hin ti. ID ny hoc l a) authorizationID ca module nu module m agent ny ang thc hin c mt authorizationID, hoc b) authorizationID ca phin nu module m agent ny ang thc hin khng c mt authorizationID Chng ta ch c th thc hin php ton SQL nu authorizationID hin ti c tt c cc quyn cn thit thc hin php ton trn cc phn t c s d liu c bao hm. V d 3.35: xem c cu kim tra cc quyn, ta xem li v d 3.34. Chng ta c th gi thit rng cc bng c tham chiu Movie v Studiol mt phn ca lc gi l MovieSchema c Hoa to ra v lm ch. im ny, ngi s dng Hoa c tt c cc quyn trn cc bng v tt c cc phn t ca lc MovieSchems ny. Anh ta c th chn v phn mt vi quyn cho ngi khc. C nhiu cch php chn trong v d 8.34 c th c thc hin. 1. Php chn c th c thc hin nh l mt phn ca module do Hoa to ra v cha mnh AUTHOIATION Hoa. AuthorizationID ca module nu c lun lun tr thnh authorizationID hin ti. Khi , module v lnh chn SQL ca n c cng cc quyn nh ngi s dng Hoa c, bao gm tt c cc quyn trn cc bng Movie v Studio.

158

2. Php chn c th l mt phn ca mt module khng c ch. Ngi s dng Hoa m mt kt ni vi mt mnh AUTHOIATION hoa trong lnh CONNECT. By gi Hoa authorizationID hin ti, v vy lnh chn c tt c cc quyn cn thit. 3. Ngi s dng cp tt c cc c quyn trn cc bng Movie v Studio cho ngi s dng Huy hoc cho mt ngi s dng c bit PUBLIC (thay th cho tt c cc ngi s dng). Lnh chn trong mt module vi mnh AUTHOIATION huy Bi v by gi authorizationID hin ti l Huy, v ngi s dng ny c cc c quyn cn thit, php chn mt ln na c chp nhn. 4. Ging nh trong (3), ngi s dng Hoa cho ngi s dng Huy cc quyn cn thit. Lnh chn trong mt module khng c ch; n c thc hin mt phin m authorizationID ca n c thit lp bng mnh AUTHOIATION sisko. AuthorizationID hin ti l Huy v ID ny c cc quyn cn thit.

3.6.4 Cp cc quyn Chng ta thy trong v d 8.35, iu quan trng i vi mt ngi s dng (tc l mt authoriationID) l c cc quyn cn thit. Nhng trn, chng ta va thy cch duy nht c cc quyn trn mt phn t c s d liu l phi l ngi to ra v l ch ca phn t . SQL cung cp lnh GRANT cho php mt ngi s dng cp mt uyn cho ngi khc. Ngi s dng th nht vn tip tc c c quyn c cho i ; nh vy, GRANT c th c hiu nh l copy quyn. C mt im khc nhau quan trng gia cp cc c quyn v sao chp. Mi mt quyn c mt grant option lin kt. Nh vy, mt ngi s dng c th c mt quyn nh l SELECT trn bng Movie with grant option, trong khi ngi s dng th hai c cng c quyn nhng khng c grant option. Khi , ngi s dng th nht c th cp quyn SELECT trn Movie cho mt ngi s dng th ba v vic cp ny c th vi hoc khng 159

vi grant option. Tuy nhin, ngi s dng th hai khng th cp quyn SELECT trn Movie cho bt c ai. Lnh Grant bao gm cc phn t sau: 1. T kha GRANT 2. Mt danh sch gm mt hoc nhiu quyn, chng hn SELECT hoc INSERT(name). Mt cch ty chn, cc t kha ALL PRIVILEGES c th xut hin y (ch tt c cc quyn i vi c s d liu ang xt) 3. T kha ON 4. Mt phn t c s d liu. Phn t ny thng l mt quan h, hoc mt bng c s hoc mt khung nhn. N cng c th l mt min hoc l cc phn t khc. 5. T kha TO 6. Mt danh sch gm mt hoc nhiu ngi s dng (cc authorization ID). 7. Cc t kha WITH GRANT OPTION (ty chn). Nh vy, dng ca lnh GRANT l GRANT < danh sch quyn> ON < phn t ca CSDL> TO < danh sch ngi s dng>[< WITH GRANT OPTION>]. thc hin lnh grant ny mt cch hp php, ngi thc hin n phi c quyn c cp v cc quyn ny phi c gi vi grant option. Tuy nhin, ngi cp quyn c th c quyn tng qut hn (with grant option) quyn c em cp. V d, quyn INSERT(name) trn bng Studio c th c em cp trong khi ngi cp gi quyn tng qut hn INSERT vo bng Studio vi grant option. V d 3.36: Ngi s dng Hoa l ch ca lc MovieSchema cha cc bng Movie(title, year, length, inColor, studioName, producerC#) Studio(name, address, pres#)

160

cp cc quyn INSERT v SELECT trn bng Studio v quyn SELECT trn Movie cho cc ngi s dng Nam v Thanh. Hn na, anh ta km theo grant option i vi cc quyn ny. Cc lnh Grant l: GRANT SELECT, INSERT ON Studio TO nam,thanh WITH GRANT OPTION; GRANT SELECT ON Movie TO nam,thanh WITH GRANT OPTION; By gi, Thanh cp cho ngi s dng Huy cc quyn nhng khng c grant option. Thanh thc hin cc lnh: GRANT SELECT, INSERT ON Studio TO huy; GRANT SELECT ON Movie TO huy ; Cng nh vy, Nam cp cho Huy cc quyn ti thiu i vi php chn INSERT(name) trn Studio v SELECT trn Movie. Cc lnh l: GRANT SELECT, INSERT(name) ON Studio TO huy ; GRANT SELECT ON Movie TO huy ; Ch rng huy nhn quyn SELECT trn Movie v Studio t hai ngi s dng khc nhau. Anh ta cng nhn quyn Insert(name) trn Studio hai ln: mt cch trc tip t Nam v thng qua quyn tng qut hn INSERT t Thanh. 3.6.5 Biu grant V mt dy grant c th to nn mt mng cc cp quyn v cc c quyn phc tp nn ngi ta biu din cc cp quyn bng mt th gi l grant diagram (biu grant) cho tin. Mt h thng SQL duy tr mt biu din ca biu ny gi du vt ca cc quyn v nguyn gc ca n. Cc nh trong biu grant tng ng vi mt ngi s dng v mt quyn. Ch rng mt quyn vi hoc khng c grant option phi c biu din bng hai nh khc nhau. Nu ngi s dng U cp quyn P cho ngi s dng V v vic cp c th da trn s kin l U gi quyn Q (Q c th

161

l P vi ty chn grant, hoc c th l mt quyn tng qut hn P) th chng ta v mt cnh t nh i vi U/Q n nh i vi V/P. V d 3.37: Hnh 3.26 biu din biu grant ca dy cc lnh cp quyn v d 3.36. Chng ta s dng quy c rng du * i sau t hp ngi s dng - quyn ch ra rng quyn bao gm grant option. Du ** i sau t hp ngi s dng-quyn ch ra rng quyn ly ra t quyn s hu phn t c s d liu ang xt v khng phi do mt s cp quyn u c. S phn bit ny s c tm quan trng khi chng ta tho lun v vic hy b cc quyn trong phn 8.7.6. Mt quyn vi hai du sao cha grant option mt cch t ng. 3.6.6 Hy b cc quyn Mt quyn c cp c th b hy b bt c lc no. Trn thc t, vic hy b cc quyn c th c yu cu theo kiu dy chuyn (cascade) theo ngha l vic hy b mt quyn vi grant option c chuyn cho cc ngi s dng khc c th yu cu cc quyn cng b hy b. Dng n gin ca lnh hy l: 1. T kha REVOKE 2. Mt danh sch gm mt hoc nhiu quyn 3. T kha ON 4. Mt phn t c s d liu 5. T kha FROM 6. Mt danh sch gm mt hoc nhiu ngi s dng (cc authorization ID). Nh vy, dng ca mt lnh hy quyn l nh sau: REVOKE < danh sch quyn> ON < phn t c s d liu> FROM < danh sch ngi s dng> Tuy nhin, Mt trong cc mc sau y cng phi c trong lnh: 1. Mt lnh c th kt thc vi t CASCADE. Nu nh vy th khi cc quyn c ch ra b hy b, chng ta cng hy b mi quyn c cc

162

quyn b hy b cp. Chnh xc hn, nu ngi s dng U hy b quyn P ca ngi s dng V da trn quyn Q thuc vo U th chng ta loi b cnh t U/Q n V/P trong biu cp quyn. By gi nh no khng truy cp c t mt nh ch cng b loi b. 2. Mt lnh c th kt thc bng RESTRICT, iu c ngha l lnh hy khng th c thc hin nu lut lan truyn c m t trong mc trc c th dn n vic loi b cc quyn do cc quyn b hy chuyn cho quyn khc.
Hoa SELECT on Movie ** Hoa INSERT on Movie ** Hoa SELECT on Studio ** Hoa INSERT on Studio **

Nam SELECT on Movie *

Thanh SELECT on Movie *

Nam SELECT on Studio *

Thanh SELECT on Studio *

Nam INSERT on Studio *

Thanh INSERT on Studio *

Huy SELECT(name) on Studio

Huy SELECT on Movie

Huy SELECT on Studio

Huy INSERT on Studio

Hnh 3.26 Biu cp quyn

163

Vic thay th REVOKE bng REVOKE GRANT OPTION FOR l c php, trong trng hp ny cc quyn ct li vn cn nhng ty chn cp chng cho ngi khc b loi b. Chng ta c th sa i mt nh, nh hng li cc cnh, hoc to ra mt nh mi phn nh cc thay i i vi cc ngi s dng b nh hng. Dng ny ca REVOKE cng c th c thc hin cng vi CASCADE hoc RESTRICT. V d 3.38: Tip tc vi v d 3.36, gi s rng Hoa hy b cc quyn m anh ta cp cho Thanh vi cc lnh: REVOKE SELECT, INSERT ON Studio FROM thanh CASCADE ; REVOKE SELECT ON Movie FROM thanh CASCADE ; Chng ta loi b cc cnh ca hnh 3.26 t cc quyn ny ca Hoa n cc quyn tng ng ca Thanh. Bi v CASCADE c quy nh, chng ta cng phi nhn xem c nhng quyn no khng th i n c t mt quyn c hai du sao. Kho st hnh 8.26 chng ta thy rng cc quyn ca Thanh khng cn i n c t mt nh c hai du sao. Cng nh vy, quyn INSERT vo Studio ca Huy cng khng i n c. Vy chng ta khng ch b cc quyn ca Thanh ra khi biu cp quyn m cn b quyn INSERT ca Huy. Ch rng chng ta khng loi b cc quyn SELECT trn Movie v Studio ca Huy hoc quyn INSERT(name) trn Studio ca Huy v nhng quyn ny c th i n c t cc quyn s hu ca Hoa thng qua cc quyn ca Nam. Biu cp quyn kt qu c ch ra hnh 3.27.

164

Hoa SELECT on Movie **

Hoa INSERT on Movie **

Hoa SELECT on Studio **

Hoa INSERT on Studio **

Nam SELECT on Movie *

Nam SELECT on Studio *

Hnh 3.27: Biu cp quyn sau khi loi b cc quyn ca Thanh


Nam INSERT on Studio *

Huy SELECT(name) on Studio

Huy SELECT on Movie

Huy SELECT on Studio

V d 3.39: C mt vi iu tinh t m chng ta s minh ha bng cc v d tru tng. u tin, khi chng ta hy b mt quyn tng qut p, chng 165

ta khng hy b mt quyn l trng hp ring ca p. V d, xt dy cc bc sau y, ngi s dng U no l ch ca quan h R cp quyn INSERT trn quan h R cho V, v cng cp quyn INSERT(A) trn cng mt quan h 1. U GRANT INSERT ON R TO V 2. U GRANT INSERT(A) ON R TO V 3. U REVOKE INSERT ON R FROM V RESTRICT Khi U hy b INSERT ra khi V, quyn INSERT(A) vn cn. Cc biu cp quyn sau bc (2) v bc (3) c cho hnh 3.28.

U INSERT on R **

V INSERT on R

U INSERT on R **

V INSERT( A) on R

V INSERT( A) on R

Hnh 3.28 Hy b mt quyn tng qut li mt quyn ring Ch rng sau bc (2) c hai nh ri nhau cho hai quyn khc nhau ca V. Cng ch rng ty chn RESTRICT trong bc (3) khng ngn nga s hy b bi v V khng cp cp ty chn cho cc ngi s dng khc. Trn thc t, V khng th cp quyn bi v V nhn c quyn khng c ty chn cp quyn. V d 3.40 By gi chng ta hy xt mt v d tng t trong U cp cho V quyn p vi ty chn cp quyn v sau ch hy ty chn cp quyn. Trong trng hp ny, chng ta phi thay i nh ca V phn nh vic mt ty chn cp quyn v cc cp quyn p do V thc hin phi c hy b bng cch loi cc cnh i ra t nh V/p. Dy cc bc nh sau:

166

1. U GRANT p TO V WITH GRANT OPTION 2. V GRANT p TO W 3. U REVOKE OPTION FOR p FROM V CASCADE Trong bc (1), U cp quyn p cho V vi ty chn cp quyn. Trong bc (2), V s dng ty chn cp quyn cp p cho W. Biu c a ra hnh 3.29(a). Sau trong bc (3), U hy b ty chuyn cp quyn i vi quyn p t V nhng khng hy b quyn p. Nh vy, du sao s b loi b khi nh i vi V v p. Tuy hin, mt nh khng c du * khng th c mt cnh i ra v mt nh nh vy khng th l ngun ca mt vic cp quyn. V th chng ta cng phi loi b cnh i ra t nh V/p i n nh W/p. By gi nh W/p khng c mt ng i n n t mt nh c hai du sao biu th ngun gc ca quyn p. Kt qu l nh W/p b loi b khi biu . Tuy nhin nh V/p vn cn nhng b b mt du sao biu th ty chn cp quyn. Biu cp quyn kt qu c cho hnh 3.29(b).
U p ** V p * W p U p ** V p

Sau bc (2)

Sau bc (3)

Hnh 3.29 Hy b mt ty chn cp quyn li quyn c bn

3.7 TNG KT CHNG III


SQL nhng: Thay v s dng giao din truy vn chung biu th cc truy vn v cc sa i, vic vit chng trnh nhng cc truy vn SQL vo trong mt ngn ng ch thch hp thng l hiu qu hn. Mt b tin dch s chuyn i cc lnh SQL nhng thnh ra cc li gi hm thch hp ca ngn ng ch. Tr ngi khng ph hp: M hnh d liu SQL hon ton khc vi cc m hnh d liu ca cc ngn ng ch thng thng. V vy, thng tin trao i gia SQL v ngn ng ch thng qua cc bin dng chung c th biu din cc thnh phn ca cc b trong phn SQL ca chng trnh.

167

Con tr: Mt con tr l mt bin ca SQL ch mt trong cc b gi tr ca quan h. Vic kt ni gia ngn ng ch v SQL c lm d dng nh c con tr i cng vi mi b ca quan h trong khi cc thnh phn ca b hin ti c ly a vo cc bin dng chung v c x l bng cch s dng ngn ng ch. SQL ng: Thay v nhng cc lnh c th ca SQL vo mt chng trnh ngn ng ch, chng trnh ch c th to ra cc chui k t c h thng SQL dch nh l cc lnh SQL v thc hin. Cc module c lu gi thng trc: Chng ta c th to ra mt tp hp cc th tc v cc hm nh l mt phn ca lc c s d liu. Chng c vit trong mt ngn ng c bit c tt c cc cu trc kim tra quen bit cng nh cc lnh SQL. N c th c gi t SQL nhng hoc thng qua mt giao din truy vn chung. Mi trng c s d liu: Mt ci t s dng mt h qun tr c s d liu SQL to ra mt mi trng SQL. Bn trong mi trng, cc phn t c s d liu nh l cc quan h c nhm thnh cc lc c s d liu, catalog, cc cluster. Mt catalog l mt tp hp cc lc v mt cluster l mt tp hp ln nht cc phn t m mt ngi s dng c th nhn thy. Cc h thng Client/Server: Mt SQL Client kt ni vi mt SQL Server to nn mt kt ni (kt ni gia hai tin trnh) v mt phin (dy cc thao tc). Cc chng trnh c thc hin trong mt phin i n t mt module v s thc hin ca mt module c gi l mt agent SQL. Giao din mc gi: l mt th vin chun cc hm gi l SQL/CLI hoc ODBC, n c th c kt ni vo chng trnh C bt k. Cc hm cho cc kh nng tng t nh SQL nhng nhng khng cn c mt b tin x l. Kim tra cnh tranh: SQL cung cp hai c cu nhm ngn nga cc thao tc cnh tranh khi s gy phin phc ln nhau: cc giao tc v s hn ch trn cc con tr. S hn ch trn cc con tr bao gm kh 168

nng khai bo mt con tr l khng nhy cm(insensitive), trong trng hp con tr khng nhn thy cc thay i i vi quan h ca n. Cc giao tc: SQL cho php ngi lp trnh nhm cc lnh SQL vo cc giao tc, chng c th c ghi (committed) hoc khi phc li (rolled back hoc aborted). Cc giao tc c th c rolled back bng chng trnh ng dng xa b cc thay i hoc bng h thng m bo tnh nguyn t v s c lp. Cc mc c lp: SQL cho php cc giao tc chy vi bn mc c lp, t t cht ch n cht ch nht: xp hng th t (serializable)(giao tc phi chy trc hoc sau mt giao tc khc hon thnh), repeatable read (mi b c c trong tr li cho mt truy vn s xut hin li nu truy vn c lp li), read-commited (ch c cc b c ghi bng cc giao tc v c ghi mi c th c giao tc ny nhn thy) v read uncommited (khng c rng buc no trn nhng ci m giao tc c th nhn thy). Con tr v cc giao tc ch c: Mt con tr hoc mt giao tc c th c khai bo l ch c (read-only). Khai bo ny l mt m bo rng con tr v giao tc s khng lm thay i c s d liu, do thng bo cho h thng SQL rng n s khng lm nh hng cc giao tc hoc cc con tr khc theo cch c th vi phm s khng nhy cm, vic xp hng c th t, hoc cc yu cu khc. Quyn: Vi mc ch an ton, cc h thng SQL cho php nhiu loi quyn khc nhau c th nhn c trn cc phn t c s d liu. Cc quyn ny bao gm quyn select (c), insert, delete hoc update cc quan h, quyn tham chiu cc quan h (tham chiu n chng trong mt rng buc) v quyn c to ra cc trigger. Biu cp quyn: Cc quyn c th ngi s hu cp cho cc ngi s dng khc hoc cho mt ngi s dng tng qut PUBLIC. Nu c cp vi ty chn cp quyn th cc quyn c th 169

c chuyn cho nhng ngi khc. Cc quyn cng c th b hy b. Biu cp quyn l mt cch hu ch nh li v lch s cp quyn v hy b quyn, gi du vt ai c quyn g v h nhn c cc quyn t ngi no. MT S BI TP I. Vi c s d liu nh trn, hy vit cc chng trnh nhng (ngn ng ch l C++) sau: 1) Yu cu ngi dng v gi v tm PC c gi gn nht vi gi mong mun. In ra nh sn xut, model number v tc ca PC. 2) Hi ngi dng v cc gi tr ti thiu ca tc , RAM, kch c a cng m h s chp nhn. Tm tt c cc laptop tha mn cc i hi . In ra cc c trng ca n (tt c cc thuc tnh ca Laptop) v nh sn xut ca n. 3) Hi ngi s dng v nh sn xut. In ra cc c trng ca tt c cc sn phm do nh sn xut lm ra. iu c ngha l s model, kiu sn phm, v tt c cc thuc tnh ca nhng quan h no thch hp i vi kiu . 4) Hi ngi dng v ti tin (tng gi ca mt PC v printer), v mt tc ti thiu ca PC. Tm h thng r nht (PC cng vi printer) 5) Hi ngi dng v nh sn xut, s model, tc , RAM, kch thc a cng, hoc loi a CD v gi ca mt PC mi. Hy kim tra rng khng c PC no c s model nh vy. a ra li cnh bo nu ng th, ngc li chn thng tin vo cc bng Product v PC. 6) H gi tt c cc PC c $100. Hy m bo rng bt k mt PC mi no c chn vo trong thi gian chng trnh ca bn chy s khng b h gi. II. Vit cc chng trnh con Da trn c s d liu trn, hy vit cc hm v cc th tc sau: 1) Ly gi lm i s v tr li s model ca PC c gi gn nht.

170

2) Ly nh sn xut, model, v gi lm i s v tr li gi ca ca kiu sn phm no c model cho. 3) Ly thng tin v model, tc , ram, a cd v gi lm cc i s v chn thng tin ny vo quan h PC. Tuy nhin nu c mt PC vi model ny (ngha l vi phm ton vn thc th, SQLSTATE = 23000) th thm 1 vo s model cho n khi tm c s model cha tn ti trong bng. 4) Cho trc mt gi, hy a ra s ca cc PC, s ca cc Laptop v s ca cc Printer ang c bn trn gi .

PH LC 1: C S D LIU CNG TY

NHNVIN
MsNV NV001 NV002 NV010 NV014 NV016 NV018 NV025 NV061 Hm L Trn c Hong Phm Nguyn V Hng Trn L Hong Tn Vn Nam Thanh Bng Sn Giang Hoa Gip Ngy sinh 1979-12-02 1966-02-14 1979-05-08 1952-06-26 1973-08-14 1983-03-26 1980-03-15 1947-02-05 ach H ni H ni Ngh an Bc ninh Hnam Nam nh Phth H tnh Giitnh Nam Nam N Nam Nam N N Nam Lng 3000 4000 2500 4300 3800 2500 2500 5500 MsNGS NV002 NV061 NV014 NV061 NV002 NV002 NV014 Null MsV 5 5 4 4 5 5 4 1

NV
MsV 5 4 1 TnV Nghin cu Hnhchnh Lnho M sNQL NV002 NV014 NV061 Ngybtdu 2000-09-15 1997-06-24 1992-01-25

171

DN
TnDA DA01 DA02 DA03 DA04 DA05 DA06 M sDA 1 2 3 10 20 30 aimDA H nI Nam nh Bc Ninh H ni H ni H ni M sV 5 5 5 4 1 4

NHNVIN_DN
M sNV NV001 NV001 NV016 NV018 NV018 NV002 NV002 NV002 NV002 NV010 NV010 NV025 NV025 NV014 NV014 NVO61 M sDA 1 2 3 1 2 2 3 10 20 30 10 10 30 30 20 20 Sgi 32 7 40 20 20 10 10 10 10 30 10 35 5 20 15 null

Trong , cc thuc tnh c ngha nh sau: 1.Trong bng NHNVIN - Hm, Tn: ch h m v tn ca nhn vin - MsNV: M s ca nhn vin

172

- Ngysinh, ach, Lung, Gii tnh: cc thuc tnh ca nhn vin - MsNGS: M s ca ngi gim st nhn vin (mt nhn vin c th c mt ngi gim st). Thuc tnh ny c cng kiu vi MsNV - MsV: m s ca n v m nhn vin lm vic cho 2. Trong bng NV - TnV: tn ca n v - MsV: m s ca n v - MsNQL: m s ca ngi qun l n v (n v trng). Ngi qun l cng l mt nhn vin. Thuc tnh ny c cng kiu vi MsNV. - Ngybtu: ch ngy ngi qun l bt u qun l n v. 3. Trong bng DN - MsDA: m s ca d n - TnDA: tn ca d n - aimDA: a im ca d n - MsV: M s ca n v qun l d n * Trong bng NHNVIN_DN - MsDA: m s ca d n - MsNV: m s ca nhn vin - Sgi: s gi nhn vin lm vic cho d n.

173

174

PH LC 2: C S D LIU MY TNH Lc c s d liu gm 4 quan h: Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price); Printer(model, color, type, price) Quan h Product cho nh sn xut, s model, v kiu(PC,laptop,hoc printer) ca cc sn phm khc nhau. tin li chng ta gi thit rng s model l duy nht trn tt c cc nh sn xut v kiu sn phm. (gi thit nh vy l khng thc tin v mt c s d liu thc phi cha mt m ca nh sn xut nh l mt phn ca s model). Quan h PC c cc thuc tnh model, speed (tc ca b x l, tnh bng megahertz), dung lng RAM (tnh bng megabyte), dung lng a cng (tnh bng gigabyte), tc v kiu ca a c th di chuyn c (CD hoc DVD) v ga. Quan h Laptop cng tng t, ch c thuc tnh mi l Screen, cho kch thc mn hnh (tnh bng inch). Quan h Printer c cc thuc tnh model, color (my in mu hay en trng, c gi tr lgic), type (kiu x l: laze, in phun hay bt) v price (gi tnh bng dolar) PC
model 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 speed 700 1500 866 866 1000 1300 1400 700 1200 750 1100 350 733 ram 64 128 128 64 128 256 128 64 128 64 128 64 256 hd 10 60 20 10 20 40 80 30 80 30 60 7 60 rd 48xCD 12xDVD 8xDVD 12xDVD 12xDVD 16xDVD 12xDVD 24xCD 16xDVD 40xCD 16xDVD 48xCD 12xDVD price 799 2499 1999 999 1499 2119 2299 999 1699 699 1299 799 2499

175

PRODUCT
maker A A A A A A B B B B B B C C C C C C C D D D D E E E F F G H model 1001 1002 1003 2004 2005 2006 1004 1005 1006 2001 2002 2003 1007 1008 2008 2009 3002 3003 3006 1009 1010 1011 2007 1012 1013 2010 3001 3004 3005 3007 type pc pc pc laptop laptop laptop pc pc pc laptop laptop laptop pc pc laptop laptop printer printer printer pc pc pc laptop pc pc laptop printer printer printer printer

LAPTOP
mode l 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 speed 700 800 850 550 600 800 850 650 750 366 ram 64 96 64 32 64 96 128 64 256 64 hd 5 10 10 6 5 20 20 10 20 10 screen 12.1 15.1 15.1 12.1 12.1 15.7 15.0 12.1 15.1 12.1 price 1448 2584 2738 999 2399 2999 3099 1249 2599 1499

PRINTER
mode l 3001 3002 3003 3004 3005 3006 3007 color true true false true true true false type ink-jet ink-jet lazer ink-jet bubble lazer lazer price 231 267 390 439 200 1999 350

176

TI LIU THAM KHO 1) C.J.Date, Hug Darwen, A guide to the SQL standard, Addison-Wesley Publishing company, 1993. 2) Hector Garcia-Molina, Jeffrey D.Ulman, Jennifer Widom, Database Systems: The Complete Book (Chapters: 6,7,8), Prentice Hall, 2002 . 3) Peter Gulutzan, Trudy Pelzer, Optimzing SQL, R&D Publication, Inc,1994. 4) Christian Maree. Guy Ledant, SQL2. Initiation Programmation, Armand Colin, Paris 1994.

177