SQL Server 2000 - Vietnam

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Bài 1: Gi i Thi u SQL Server 2000

! " ! #! !$! %! !
& #'% ( ( ) * +, !" +- ! . " /
" / 0% #'% $!" 1 ! !$! 2 ! !$! 3 4 * 5 +,
3 $ / 6 7 3 ! " #'% 0

+89 8 +, : , ;& < = 8> ? @ A & !


#! !$! B " ( < +C D !E'& : ,/ * * 5 F " 0
: , 7C 9/ GH G @ 3 7 3 8 % " "I J J I" ! " JJ (2 BE."
2 K "L& 0000

:M " N

• N . 4! +O& +P 3 +Q 8 P! : , ;& < < +C


R .KS TU V' W%0 D < " +: : : 3 X * F/ " / Y Z ?
W !& (
• N ? Z 9/ " 3 = & [! \ ] 3 ^ _ " @ B /
B " 2 8 ; $X @ ; 4 H !" ?/ ! ! I ! (7 3 2 " & :
, ;& < < +C U .KS V' W%0
• N +89 8 :! +, ;& < K. < : , +Q < O C 3 / < $ ` "`
7, a "` bc0
• N .: +O& +P 3 Z H P! B / B " 8 +89 C ;" +Q $ 8 @
; 89 8> 7C " 5 F 0000 dY& " 3 $; e
O :0 . F ! f 5 " & " 7 :! e 0 B " & : , <
a "` K "I " ! !& a gD a" 7 ! " 0
• % #B(N dY& h ;& < 7 "/ 7 = : I!
!" (0 D Z 9/ " , 7 ! 4 * 3& 0 iZ 8@ ! !$! $X @ ;
7 " V'0
• N #5 " 3 4 * ;& < a "` .B
• N .: 3 Z H P! B / B " 2 "` "! I 2 8 @ ; > ! ) * 0

1.1. Cài Ð t SQL Server 2000 (Installation)

.3 $; O : Z ? TU %' W%2 j %' ! 7 +, : , !


0 '; : , ! < a "` !& a "` kK K "I " ! 2 a "`
K "I " ! !& gD a" 7 ! " 8 7 = , ! < a bc I! &0

A] " +Q + , P! 3 / l % " "I m ! < F = 7 = ]


$ & C _ 3 ! !& 3 $8@ ! h ] $ & 3 +, O 8 7 ! = 0 gC
3 $; Q/ ; " ! ] : , +8! < I" +, \ < 0i ! $; O 8 3
+, ! N

n ] 4 ! $; e 0 ! 7 ! L" $; :
, ! < W !& C $; Z 0

n ] $; e 0

! +: $; < e 7, ! " # 3 $ / 6 P! 0g " ! <


! $ $ % & '% I! (

n ] ( ) @ e )* ) 0 8 ] : , 5
C + $ " 6 &( @ a gD o : , 5 C + $ " 6 < P! :0 D "

[email protected] Page 1 of 44
K" " 8> ! 8> 5 a "` W ! " ] + ! " !" m "
8> ; " 8> ) * 0 g p! 7 $; +89 ?/ 6 ! ! ( 7C
" " ! ] $; : &_ & 6/ ! ! !( " 0D & < ! < e %L
%" +, m " e 6/0

! 7 ! $; f ?& " q : / $< 8@ ] 2 +Y& Z


%! ! 0 '; : , ! 2 "/ 3 m $q 3 " $ E 7 " " &0

1.2. M t chút ki n th c v các Version c a SQL Server

P! % " "I +89 X 8> ?/ 6 r 7, [ " T0j0 ! +: % " "I


+r C O 8 C ; @ " M0 0 . " < : , : [ " T0j <
" M0 $8@ & e 0 .: +Q Z P! M0 7 = 8 Z @ "
T0j0 D " 7 +: [ A " M0 < " c0 ( ] C P &C
3 Z H _` $ " +3 6& 0

% + , +Q $ +3 8 " ) 0 g: " m , $; :
, ! " @ 3 " 8@ 7 = O / ! F 0 g p! $; : ,
;& " " " T0j "Q M0 @ " < 5 3& + _ &7 = ,L & ! @
3 " 8@ +Y&(0 i +: " o < 3& $; s " @ [!
! f + 0

1.3. Các thành ph n quan tr ng trong SQL Server 2000

+89 ? ;" $ _ / O 8 ! " ! #! !$! B 2 W !&


B &0000 .3 / O &7 / 9/ @ ! ;" / 3/ " h
F/ " 8 / Y Z 3 m 0

[email protected] Page 2 of 44
1.3.1. Relational Database Engine E .3 t P! N

dY& :7 H 4! ! ! 3 & =7 3 ! 8@ ; !$
//" ? 3 7 , 7C !! " " ( = * P! % " "I 8W k #! !
u$v W#u(2 u B #'2 ! u/ #! !$! ." & u#'.(0 g " ! : s : 7 H w
+_ h /( Z * 8 ) * < 3 &< " ( P! 3& 7 O ;
&< " +_ 7 " "II0

1.3.2. Replication E . C ;" $ !" / !(N

V ) $; : ! !$! 5 +, 4! +89 3 4 * 8> L &< 6/


60% & +x/ > $; : 3 ! !$! & 8 C < 7 3
+, ;& $3" 3" /" ! !$! ( 3 & 8> 5 +, 3 8 +C
/ I" ! P! Z (0 A? +_ /" P! $; o O / +89 6/ 6
8> L &< +, + $ " Z Z L3 P! 3 $3" 3"0 '; 7 = , 5 C $! 7 /
! " " 8> 9/ &0 D C ] $; / !"y F +: C / ! " P!
f +89 ) * +, $ " + " ! !$! +89 +1 $ & " z (0
/ ! " f +89 $ 7{ " $ |

1.3.3. Data Transformation Service (DTS, E % X * &, X !! = 5

gC $; " = & @ " +: ! ! +89 4! " _ 7 3


! 3 ; 7 3 ! * , 8 4! " u ! 2 #' P! J'%(2 2
% " "I W 0000'; } } f : O &, ! ! ! 3 & ! !&
! I ( 7 = h &, $; s +X ; I" ! ( : 8@ 7 8 " ! !$!
7 3 2 7 +: $; f ?& #D F/ $; &C = < m 8 C "0 #D f
+89 $ 7{ " $ c0

1.3.4. Analysis Service - % X */ Y Z ? !& P! % " "I

# #! !( 4! " ! !$! f ~ : p! ] _ C 8 $; 7 = ,
?& +89 = J I" ! " ( $- Z [ +:0 #" +: % " "I ?/ " $; =
* ? ; F/ " / Y Z < m $q 3 5 7 3
] 7 _ _ E " $ ( 7{ 6 G+ " \ G !! ( f +89
F = @ " $ |R0

1.3.5. English Query - % X * 8> A g! } Z 5 NE( y(

dY& X * F/ " & ! ! $q C W G G /! B (0

1.3.6. Meta Data Service:

#X * & F/ " 4! +w GL " ? G % ! ! ! m 0D C ]% !


#! ! 3 ] 6&y % ! ! ! = = _ ? F P! !! " ! !$! 8
!! "; " !& J 0002 " +: : / K ! & 7 & !& 7 = 0000' ]
= & o +89 4! " ! !$! < o ; ! ! 8 +, / Y
$ @ !!G Z G 8> ! e : % ! #! !0 K O & } $; / L <
" / O 7 3 P! }/ @ ! +Y& - . ]
7 = :$ " " "; $ & : t _ X * & 0

1.3.7. SQL Server Books Online - &, i D 3 7 = , C N

[email protected] Page 3 of 44
. " 5 $; : +e 3 3 7 3 ! ;& _ ] $; o f ?& $""7
" & ? * 7 = , C +89 " < % " "I @ " / : +Z 7• "
(0

1.3.8. SQL Server Tools - dY& $ +1 _ P! 8> X #'W (

€ C 7, C ! ] _ +?& " < $; O +e < " $""7 " 0


n +Y& 8> C h 7, ! = * = * =0

dO < / 7, +C ) 0 dY& = * " ! ?& "


3 ? w ! 0 g: ? Z +Q $ " 8> @ e 7 = =
;" } _ 0
iC +C /( /0 0 d @ #'W \ ] O 8 h O = * & : ,
! !$! 7 = O +C 4 7 3 0 dY& = 8>
7 3 ] ! : , +3 $? 7• Y " ;& !& 6/ 4 +Q $ : F/ " !
$ ?& 3 " /" m 0
.= * 4 $! O / 7, +C 0 g: : 7 H G */G !/ ( ? 3 w
7 !& "; + m ! < 8 ; 8@ ; L I ? * "
7, "3 "; + P! 0
g " = * w ! 8 < F ! o 8> !& 5 1 $ 7
"/&( " " ! /" /0

[email protected] Page 4 of 44
Bài 2: Gi i Thi u S L c V Transact SQL (T-SQL)
D! ! E = w! < l P! J u J ! " ! u ! z! "
I" ! ! z! " ( Wg J W ! g! " ! ! ! J ( +89 ) * " 7 3
@ KE K" !E ( 5 " u! 0

D" $ & F ! f ] , ! _ DE 0. F +89 ! R : N

2.1. Data Definition Language (DDL):

dY& 5 +, 3 Z P! ! !$! 8 +X p! 3 "Q


P! !$ 2 !& X Z !!I P! ! !$! 000 8> : ;

• . ! "$v ‚g!
• W "$v ‚g!
• # "/ "$v ‚g!

D" +: "$v ‚g! : , !$ 2 `2 " /" 2 L 000

AZ *N

. ! ! f ;" ! !$ < J /" @ R ." /! &J#2." /! &g! 2." !

USE Northwind

CREATE TABLE Importers(


CompanyID int NOT NULL,
CompanyName varchar(40) NOT NULL,
Contact varchar(40) NOT NULL
)

W ! +Y& " / ƒ/ ! !& +- +X p! P! !$ 8 < !& $@ ( !&


." ! 000D " Z * & ! f < ." ! D " !$ J /"

USE Northwind

ALTER TABLE Importers


ADD ContactTitle varchar(20) NULL

# "/ ! +Y& f " " L:! !$ 7 \ ! !$! 2 3 # '% 2 4 3 5


'6 7 " 7 3 @ # h L:! ! ! 8 !$ „ 1 ; (0

USE Northwind

DROP TABLE Importers

2.2. Data Control Language (DCL):

dY& 3 &_ & 6/ < [ "$v !$ 2 `2 " /" 000(0 D 8> :


; ! N

• V!
• "7
• # &

[email protected] Page 5 of 44
AZ *N

! f " / ƒ/ " K $ " +89 &_ + @ !$ . " " ! !$!


g" ` 8 7 3 8 a "` V " / f +89 $ 7{ " / O &(

USE Northwind
GRANT SELECT
ON Customers
TO PUBLIC

! f [ &_ + @ !$ . " " ! !$! g" ` P! 3 "


K $ "

USE Northwind
DENY SELECT
ON Customers
TO PUBLIC

! f L:! $\ 3 * P! 3 &_ +89 " / ƒ/ !& [ 8@ +:

USE Northwind
REVOKE SELECT
ON Customers
TO PUBLIC

2.3. Manipulation Language (DML):

dY& / - $C 5 +, L) !! 8 2 S/ ! 2 J 2#

AZ *N

USE Northwind
SELECT CustomerID, CompanyName, ContactName
FROM Customers
WHERE (CustomerID = 'alfki' OR CustomerID = 'anatr')
ORDER BY ContactName

USE Northwind
INSERT INTO Territories
VALUES (98101, 'Seattle', 2)

USE Northwind
UPDATE Territories
SET TerritoryDescription = 'Downtown Seattle'
WHERE TerritoryID = 98101

USE Northwind
DELETE FROM Territories

[email protected] Page 6 of 44
WHERE TerritoryID = 98101

A] / O &7 3 H $ < F = C p7 = O Z _ 0. F " # $;


: , : …" !& 7 = +_ +89 0

+ : 3 ; < =/ > $ ? @ 5 A 'B C

d, ;& 3 Y Z * < $; O ) * @ /( /

; $ 4 D

K O & F ! f$ _ 3 / O ;" < F / 3/ P! DE

dY& Z <
P! 3 ! !$! "$v 0 g: 5 +, L3 +X "$v 0 . F 7 : +C u$v "
F ! +_ 6/ +C !$ 2 `2 " /" 2 L00000A] O 8 e 4 "
+_ +89 C 7C " 7 , 8@ + 89 "$v E" ((0 D " Z * ! D!$ k2
i &." 2 # / " I

CREATE TABLE TableX


(KeyCol INT PRIMARY KEY, Description NVARCHAR(80))

.: !
"; J I "; = 8> 8 ( "; e
2 "; & O : ? GG !& ? †‡ +, H 3 0 "; # +89 5 + @ 3 5
@ [ 7 :! P! 7 &`" ( !& 3 :7 " 0

[email protected] Page 7 of 44
AZ *N

SELECT * FROM [My Table]


WHERE [Order] = 10

D" Z * < u 5 @ 7 &`" u < O +Q " ? "Q = †‡0

E F- 7 ,

'C " DE o : 4 H 8 w 8 " 3 = 6/ ] 7 3 p! O 7 ! $3"


8@ "; 8@ 7 ) * 0 ' C +89 $} +O $q ? ˆ d @ 3 "$! ! !$ ] : !
? ˆˆ(

AZ *N

USE Northwind
DECLARE @EmpIDVar INT
SET @EmpIDVar = 3
SELECT * FROM Employees
WHERE EmployeeID = @EmpIDVar + 1

G FH3 ,

.: "; "; $ E "; E I

.3 - +89 ! R : N

• 8 I G N "; & 8> _ "$v +89 + L) 8 !$ 0 AZ * 8


uKBg SB ‰ f _ " : , +4 X Z P! !$ " Y
0
• ( G N "; & < 3 X _ 3 X+ !& 3
3 X - 0 AZ * 8 WAV f _ 3 X $] P! 0
• G N "; & < 3 X+ _ 3 X+ 0D" "; &
; ! _ "; \ 8 3 _ "3 e 2 _ > ! 2 L) 7 , 0000AZ *
8 %ugDŠ ‹ E bER ‹( f _ 3 b0

.3 9 +89 ;" ! $ Y . BWDB …Sg.DJug / O $" & 8> +89 : "


Q/ 'BVJg000Bg#( o +89 ! 3 : 8 ! N

• G N "; & o _ 3 X+ $q Y BDS g 0


• G N "; & _ !$

/ F = J K ,

.3 "; " f +89 $ 7{ " 3 $ !

.3 BL/ " : ; Œ. 8 Œ2E2•2Ž2•000( Œ E

$ 3 L . G I

g 8 'BVJg000Bg#2 ' BWi2 .ugDJgSB2 VuDu2 J…000B B2 BDS g2 aŠJ B0000 k L < '""7 u +,
$C < _ 3 / O &0

[email protected] Page 8 of 44
F ; ? ,

DE 5 ? EE +, +3 ? / O F Z " Y + 5 Ž•000•Ž +, F Z "


:

D w D .3 .Y

M N B K 'O D

% Y +89 / Y ! 3 / O F / 3/ 8 < $ /! 2 ! +:
u/ z $ / 6 ! e P! ( f/ Y Z ] 3 w BL K! ( 8
? Z * 8 3 " ! Z &< P! 3& ? 000 ! +: B f w
_ 7C 0

M N P K F- ,

i w : f/ Y Z ] $ / 3/ 8 " 3 Y 8 Y
+ 4! L " / ! +r +89 $ < X " / ( " $ @ ! +: C : <
+89 e ; O ! ] 7 = O $< X : , w !& + _ & F/ "
$! ;& ! 0

K Q.

& h 5 +, Z " $ C +r 7C F $! v"$ &< O w 0


g: 7 = / " DE 0

[email protected] Page 9 of 44
Bài t p 2 SQL Server 2000
Manipulate Data and Stored Procedure

K ! I" "` " / "/! N


|0 S " L/" ! ! ! I " u !$ "I K ! #' " N•u 0L " " N•u 0 (
0 . ! " !! N•u 0L ! ! 0 D /" " u !$ I " L I
$ /
R0 J /" u 0L "u !$ -9 R + 8
U0 . ! ! ‘ 7 K! #'’ ` 7 " ! W ! !$! ‘K ! #'0 $’
I & &" ! " ! ! W ! !$! ! "K! #' ! / "
! !(0 D "! !! I" E/! ! ! . + 9 8S
j0 S ` . +8. ! . + ( ( .98 I " "
! ! I " ‘K ! #'0 $’
T0 . ! I" "`

#B. W B ˆ! ‚ ! ! ! U (2 ˆ! ‚I ! ! ! (
#B. W B B / "& ‚. " .S u …u

B B.D ! g! 2… g! … u% g" ` 0 $"0B / "&

uKBg B / "& ‚. "

…BD.Š gBkD … u% B / "& ‚. " JgDu ˆ! ‚ ! 2 ˆ! ‚I !


aŠJ B ˆˆ…BD.Š‚ DWDS •
'BVJg
K JgD ‹W " N‹ Œ ˆ! ‚I ! Œ ‹ ‹ Œ ˆ! ‚ !
…BD.Š gBkD … u% B / "& ‚. " JgDu ˆ! ‚ ! 2 ˆ! ‚I !
Bg#
. u B B / "& ‚. "
#BW u.WDB B / "& ‚. "

M0 . ! I" "` ! & " L ` " !

. BWDB K u.B#S B W g `u
ˆu J# ! 2
ˆK " g! ! ! j (2
ˆ. " g! ! ! j (2
ˆ ! •| u /
W
#B. W B ˆ. " J# !
'BVJg D Wg W.DJug
JI " BL B B.D . " J# … u% . " aŠB B †g! ‡•ˆ. " g! (
'BVJg
BD ˆ. " J#• B B.D %!L . " J#( … u% . " (
BD ˆ. " J#•ˆ. " J#Œ|
Jg B D JgDu . " AW SB ˆ. " J#2ˆ. " g! (
JI BL B B.D u J# … u% †u ‡ aŠB B u J#•ˆu J#(
'BVJg
B B.D ˆ •|
u 'W.i D Wg W.DJug
Bg#
B
'BVJg
Jg B D JgDu †u ‡u J#2K " g! 2. " J#(
AW SB ˆu J#2ˆK " g! 2ˆ. " J#(
B B.D ˆ •
.u%%JD D Wg W.DJug

[email protected] Page 10 of 44
Bg#
Bg#
B
'BVJg
JI BL B B.D u J# … u% †u ‡ aŠB B u J#•ˆu J#(
'BVJg
B B.D ˆ •|
u 'W.i D Wg W.DJug
Bg#
B
'BVJg
Jg B D JgDu †u ‡u J#2K " g! 2. " J#(
AW SB ˆu J#2ˆK " g! 2ˆ. " J#(
B B.D ˆ •
.u%%JD D Wg W.DJug
Bg#
Bg#
K ˆ

b0 S E- T " A'0gBD " L ‘W g `u ’ " /"


| 0S * T L " /" " ! ! L/‚ ‘ Š "’(

[email protected] Page 11 of 44
Bài 3: Design and Implement a SQL Server Database
3.1. C u Trúc C a SQL Server

g 8 +r ] $ & 3 $ 8@ " +Q + , P! )
< 7 : +C ( " +: ! : +C J ! P! 2 =
8> +: # I! J ! 0% J ! P! :U & ! !$! !& _
! !$! 0 .3 & ! !$! $!" 1 N

• ) N . 4! ? = ?/ & E I" ! " ( $!" 1 =


_ 3 ! !$! 7 3 " 8 X Z P! 3 ! ! I 2 3 " ! " 3 C +Q
? ] P! & " I ! " (0
• N . 4! ? !$ !& " /" +89 ; > ;" ! " 3 ]
$ !& " $ Y 0 .3 !$ !& " /" & f $C ?
7 7 + ; !& 7 ! " 0
• ) N #! !$! & +: ! s 8 $ 7^ / ! ( " 3 ! !$! 7 3 0 g p!
7 ! !$! +89 ;" ! ] f "/& " $ 3 & "$v !$ 2
" /" 000( [ %" ! !$! ! ! !$! @ [! ;"0
• ) N #! !$! & +89 W ) * +, "; +X 3 $3" + 3 =
O ! ! v"$ (0

3.2. C u Trúc V t Lý C a M t SQL Server Database

%“ ! !$! " +_ 4! Z ? !!I Z / ! &(2 : , : < !&


_ !!I / * " ! &( ! ! " " I 0

• / 8> :/ O U ( N +Y& I Z 4! ! ! &


!$ 0
• / 8> :/ O U ( N +Y& I / * 8> h ) * 7
! !$! +89 / Y ! +, 4! < _ p!0
• 8> :/ O U ( N +Y& I ; ? !& +- m
! " ! !$! 4! +O& +P = +, : , " $! 7 !& " I" `! 7 O 0

#! ! " +89 4! [ ci' c /! < * ;" * 8 ]


f 8@ +Y&N

[email protected] Page 12 of 44
D 8@ 7 8 !! " !$ : O / < 7 " "
!!I " !$ +:0 g 7 " +: Z 3 L 0 .: "; BL N)* * ";
“ 9/( 5 +, 4! ! ! P! _ !$ " 5 BL 9 * "; O ?(
5 +, 4! ! ! P! !$ 0 dO < 3 K! " % L BL +, 4! ! ! "
!$ ! +: 7 !! H 8 ] ~ S I" BL " !$ +:0

3.3. Nguyên T c Ho t Ð ng C a Transaction Log Trong SQL Server

D! ! " " I " 5 +, ; 3 !& +- L & ! " ! !$! 0 3 ] & m


! 8 ! N +O < 7 : w !& +- ! ! 8 J 2 S/ ! 2 # +89 &< O [ 3 4 * 2
f "! ( ! ! /! 8 4 < "& 5 $ @ & e !! ! (2 ! +: ! !
" !! ! +89 !& +- ! $X !& +- s e &E/! (0 D C/ " e w !& +-
+_ +89 " ! ! " " I " < 8> ! e ` E! ! " 0 . 5 ] 3 ]
e f 7, ! C ? ! ! " +r +89 " " ?( "
p! 4 I /! (0

[email protected] Page 13 of 44
g " . 7 K" K" &E/! s +89 +8! " p! $ 0/ I 0 dY&
! Y O h 4 ? ƒ !/ O !! ! " 7• ? +X !
+: ; P &< > O ƒ @0

k Z < F _7 3 ! ! " " ! !$! 0 % ! ! " !&


!" X "; 3 "; + L & ! +89 L 8 = + "I `" 7( p! "Q
= " $ "Q 7 = ] ! " " (0 ! +Y& Z * - +, _ ! ! " N

. F ! &, _ ”j [! " W !
! " ' 8 6& = & O 3 $8@ ! N
|0 D [ ”j [! " W
0 . ”j "! " '

D & < &, _ < / +89 w 8@ ; ! ! " p! !"


X h +89 L " ? " (7 ! $8@ < +_ w = 0 gC ] "
" +: ! h : , w +89 $8@ | ~ ; 8 [! L" $8@ | ] + F/ !& 3& $X "(
]L 8 !" X 7 = " ? O / +89 / * 1 ; ; 3 $! +O " $! 7(0

D C ]. 7 K" K " "; + 8 C " +, : ,+ $ " ! ! " +89 w


7 = G G ! !$! 0

[email protected] Page 14 of 44
D" ] f < 2 ! ! " +89 $ , m $q o < 0D* q ! * >
! 0V ) . 7 K" +89 +3 ? " > +, ! ! ! " R 8 ] f ! +:
w Lr& ! 8@ 7 Q/ . 7 /" 7C C/0 g 8 6& 7 +89 ! : f w! <
] " ! ! " " I +, / * 1 ! ! L ] f(0

d _ +: : p! f7 = O ] + @ ! ! " | ] ; > +, . 7
/" ! ! +r +89 8 " p! 1 0 D " 7 +: ! ! " U f +89 " I" `! ] & +r +89
" 8 " w L & ! 8@ > +, 7 /" 7C C/ < ! ! 8! 7X/ 8 " p!0 D4
w! < = +89 < " I " " : +O& +P +, C " p! 4 0
.s ! ! " R j ] 8! +89 " " $X "` $? >( " < f " $! 7 !
! ! " & w! < ] +89 < " I 0

3.4. C u Trúc Logic C a M t SQL Server Database

ŠO 8 e 4 " +89 - 4 "$v Z * 8 !$ 2 ` 2


" /" 2 L 2 " ! 0000 g & "$v " 8> : $} +O $q
& !& /0 .3 "$v < f +89 < 4 O 89 " 3 $ ! " +: " / O & F
! h$ ! & "$v = * " ! !$! =0

% & "$v 8> 5 N

& " K" • *


. ?/ = _ ! !$! "$v !$ 2 `000( !&
/‚ / †‹"$v ‹‡
! ! &/ 0
/‚ / $ †‹ ! !$! ‹‡ . ?/ = _ ! !$! * , " +:0
/‚ " " . " $ C + $6 P!
/‚ /! †‹"$v ‹2 . ?/ = _ 3 7 " +r +89 ) * "

[email protected] Page 15 of 44
& " K" • *
‹ / ! ! ‹‡ "$v " +:
/‚` " †‹ " ‹‡ . " $C = _

AZ *N

/‚ / $ ‹g" ` ‹ f " 7C : ; 8$ 8@ +Y&

! $‚ z "` $ ! ! 00000
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE EEE
EEEE
g" ` R0bU %' ! T W T ! •ug JgB2 S/ ! !$ &• BW#‚a JDB2 00000

" /" /‚ /! 8 Z * !

S B g" `
V"
/‚ /! ‹. " ‹

f " $C = _ !$ . " N

! "` !! L‚ z
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
. " b| | U i' U i' c i' i'

3.5. Cách t o m t User Database

. F ! : , ;" ! !$! m 5 B / $q 3 E 7 <


< G ! !$! G e Gg ` #! !$! G 8 ] f ! N

[email protected] Page 16 of 44
! +: F ! h +3 < P! ! !$! 7 ui0

g " ! += 7 F ! o 5 / +, ;" ! !$! 0 i +: ! / h t X Z P! / !&


!!I ! ! " " I 0

AZ *N

S B !
Vu
. BWDB #WDW'W B K "
ug
gW%B • / " ‚ ! 2
…J BgW%B • ‹ N•/ " ! I • " "I • • ! !•/ " 0 I‹2
J–B • U2
%Wk J–B • | 2
…J BV uaDŠ • |
(
Vu

D" Z * < ! ;" ! !$! < K" @ " ! I ! /" ‚ ! / & !


I ! / " 0 I2 7Z 8@ $! +O U %' !!I f w+ H < “ O | %' " @
+! | %'0 gC ! 7 = h +X ! ! " " I ] f w + ;" ! | " I @ 7Z
8@ $! +O | %'0

: VD

[email protected] Page 17 of 44
i ;" ! ! !$! F ! o / 8 +, ! Nd @ 3 \ +:
? +_ + P! 7 = "; ;& ] F ! 8> e 3 3 X Q +X I! (
" 0 2( / I 0g 8 < /" " P! 3 @
7Z 8@ P! ! !$! / +89 8> #'W 8@ 89 8@ 5& " O — P! $ 2 =
8> 8> ! 7 = e W " "` w + H 8 ( W " 7 w+ ƒ (0 .Y \ +89
+Q ! +Y& ] !" ! 7 = +, e 3 X7 +O " ! !I ! +: 7 O ] :
f w+ @ ! ; / 8@ 89 8@ y g &< Y C e W " "` !& W " 7(
] F ! : , f Q/ ? +_ ! N

• D˜ 8 +3 7, +C 7 H P! 0 #" : / 8>
L &< 7 , !L : +P 7 " O C !& 7 = C 7 = +P : f /
$q 3 < 7 " [ p! 4 Z 3 ] & f 6 + "; +
P! 0
• NA < o f " !!7 = +89 < * 4!
_ 7 3 ! " p! 4 +_ & o Y& 8 < + P!
0

D" 3 @ 8> ! : , w +"3 8@ 7Z 8@ P! ! !$! $q 3 Z "3


7Z 8@ P! 3 !$ 2 +Y& o h 7Z 8@ 8@ +"3 = L L GB ! z "I !
! !$! G " '""7 u +, $ C < _ 3 Z ( ! +: 8> L &< 5 Y
8> 5 3 Y $} +O $q - 0K O & f +89 $ ! " 3 $ ! (
7, !L : +P 7 " !& 7 = C 7 = +P ! : , e > +, Z
$6 ? 8 $! +< !& ! > ( +, @ !! I 8 C f7 = 8 @
/ I" ! P! 0

. F ) ! ™ V' " ! !I 2 7 5 a "` BL/ " +, L ! f ?& 7Z 8@ P! I


V' 8 !! w C : , h C * %' =0

3.6. Nh ng Ði m C n L u Ý Khi Thi t K M t Database

D" / ; $ & F !7 = , : Y _ &C C 7C ! !$! h +8! !


> 7 &< $; < Y "7 C 7C0

D 8@ C $; / } _ 3 "; / 0 AZ * $; / $C t w 7 3 $ !
FWX,2 FWX, FWX,2 FWX,0 "; . ! "; : 7Z 8@
+X p! " Z * < C ! ! +8! " GD ! ! & " !! G @ | 7 w( ]
f w+ } / O + = ! h s GD !G0 D8 w C +8! " \ | ]
f < 7 " " / Z! ! " +P | 7 w0 g 89 ; "; ! ! f 7 = < 3 7 "
/ Z! ! 7 +8! " Z | 0 .s "; ! ! $} +O $q 4! ; " 0

% 8 7 3 " ! : 3 "; J 8N / 2 2 2 0
D" +: 7Z 8@ [ "; 8 4 |2 2U2c $& 0 g p! "; 8 +8 @
"; 8 +8 @ " A'0

i C 7C !$ < N

• .: Z ? "; 5 +, L3 +X " m 0
• . h 4! ! ! P! & w ,(

D" Z * ! = _ 3 g k ? ' +89 4! " 5 !$

'""7J# D K $ K $ ! K $. & K $." &

[email protected] Page 18 of 44
| J % " "I K .W ' 7 & S W
a "` g ` %W '" " S W
' A ! '!
R a "L .W ' 7 & S W
T0

D! < 3 ! !$ '""7 !$ K $ 8 ! N

'""7J# D K $ J#
| J K|
a "` K
R ' A ! '! T0 KR

K $ J# K $ K $ ! K $. & K $." &


K| % " "I K .W ' 7 & S W
K g ` %W '" " S W
KR a "L .W ' 7 & S W

• D3 5 : 4! +9 < = : 3 X " 3
• D3 6/ ; 3 X !& " +:

AZ * 3 : , +89 C $ 3 8 C ! : , 5 "
3 ! +, 4! ! !N

'""7J# D W "
| J š" ' "`
a "` %! ` '" 7 2 7 š" "
K a 2 š! %"" 2 š"
R ' A ! '! T0
' "`

!&

'""7J# D W "| W " W "R


| J š" ' "` g g
%! `
a "` 7 š" " g
'" 7
' A ! '!
R K a š! %"" š" ' "`
T0

[email protected] Page 19 of 44
D & < 6/ + 6/ ; W " f ;" _ ? +_ ! &0 . ~ ; 8 C 3
: _ R 3 ] F ! f Q/ / _ / 4 !&0000D " Z * & ! < Q ! R !$
8 ! N

'""7J# D
| J
a "`
R ' A ! '! T0

W J# … g! ! g!
W| š" ' "`
W %! ` '" 7
WR 7 š" "
WU K a
Wj š! %""

( -

'""7J# W J#
| W|
W
WR
R WU
R Wj
R W|

g " ! " +_ ! e / $C t ! 8 ( ! 3 !$ N

• . . 8 N " ! & ] $< !$ W 7 = , < 7C


@ | $< !$ ' 89 ; 0
• . ) /8 N " ! & ] $< !$ W : , < 7C
@ _ $< !$ '0
• ) / ) /8 N " ! & ] $< !$ W : , < 7C
@ _ $< !$ ' $< !$ ' o : , < 7C @ _ $< !$
W0 g 8 ! ?& " Z * < 3 : , +89 C $ _ 3 3
o : , C _ 3 0 #" +: ! ! '""7 W " ! %! & "
%! &0 D " 8> 9/ & 8> ! 8> 5 !$ ! +, &C ? +_
!$ W " '""7(0

d, : ! !$! 8 + " " p! C 7C !" " ! ! 4! " ! !$! 7 =


[! 7 = C $; O $ C < _ 3 P 6 + 0 0D & < " / ; 7 :! e
& F = 7 = $ Y _ +_ &2 $; : , L < " 3 3 ;& &C
0

[email protected] Page 20 of 44
Bài 4: Backup And Restore SQL Server
4.1. Chi n L c Ph c H i D Li u (Data Restoration Strategy)

.: +_ F !/ F O 8 $? 7• ! !$! " o O +89 / * 1 "


F " +: " 7• P! :0 8> #! !$! W ! " $; O /
+! O / / * 1 2 = " t2 7, ! 8> L &< +, / 3 3 * Q 8@ 7 :
L & !0 K w / s 3 $C : , L & ! $ " + q : , ! : / * 1
" > ! @ ? : , +89 0

.3 ; $C !& ! e! : ,L & ! N

• dp! 4! ! ! I !& D ! ! " " … !& & I $X ?


• $X 8 \
• g e! w < 8 $r" * 2 + +? 2 \! ";
• D" $ $X +3 }/ "Q / 3 P&
• .3 C $X 5 +, $! 7 / E " $X +3 }/ !& 8 \
• g “ " = P! 8 — !& " $ !$ ~ ;
• g ! Z / 3 "; P! Y < 8 +8! " = ! ; 0
• 'X ! 7 C : 7C @ (0

'; / w \ 7 3 ? +_ < L & ! ] $; f ] / = :$ / 3/ +_ / s *


, " [ 8> 9/ * ,0 g " ! $; / L3 +X > ! , O / * 1 +8!
; "; + $] 8> 0

4.2. Các Lo i Backup

d, : , , 3 7, / * 1 7 3 ! $; / $C ! 3 "; $! 7 / "

• G - N ."/& ? !!I " ! !$! 0 D? !!


! !$! "$v 8 & !$ 2 L 2 E I !$ +_ +89 $! 7 /0
• - N ."/& !& +- " ? !! I 7, [ O I
$! 7 / O ?0
• G G Q - N ."/& !!I + !& I " /0
• G G Q - N D8 w 8 II ! ! !$! $! 7 / 8 h
"/& !& +- " !!I + !& I " /0
• - N V 6 3 4 w ? 3 ! ! " 4! "
! ! " " I 7, [ O ! ! " " $! 7 / O ? 0 "; $! 7 / & " / ƒ/ ! / *
1 89 ; " > +, " +: " 37 4 „ + $ " Z +1 ?
" (0

D" F $! 7 / o "/& ? 3 "; + P! ! !$! 7, "; + L & !


" 3 ] $! 7 / " < ! : , $! 7 / " 7 +! ;& 7 = O / 8 ;0

4.3. Recovery Models

• G 8 / ) N dY& " " / ƒ/ / * 1 @ Z P " ? 0 gC


! !$! " " & ] ? 3 "; + 7 = h 2 / ! 2 7,
$q - 2 !& +_ +89 " " ! ! " " I 0i : w ] ! : ,
/ * 1 ; 89 ; @ > +, " 3 7 40 i ! ! I $X 8 C ! : ,
$! 7 / +89 ! ! " " I ] ! : , / * 1 ! !$! +C > +, ! ! " O
? +89 " 0

[email protected] Page 21 of 44
• - 8 / ) N n " & 3 "; + ! Z "; 8 ' 7
J 2 $ /2 . ! J L2 a D L 2 S/ ! D L h +89 " " ! ! " " I +P
+, " $ C 3 "; + & : m ! 7 = " " $ C 8 " … " &
%" 0 .3 "; + 7 3 8J 2 S/ ! 2 # „ +89 " +O& +P +, 5 " / *
1 ! &0
• 8 /) Nn " & ] D ! ! " " … +89 ! 8> L &<
7 = O $! 7 /0 A@ " & $; h : , / * 1 @ > + , $! 7 / O ? 7 =
,/ * 1 @ > +, " 3 7 40

% $C ! !$! P! $; +! " " $; : ,8 5 N 3 'P "


B / %! ! e Y. Y8 /

D & < : , @ +Y& $; ?& ? 7 : , _ +_ ] $ & < 0. F ! r& 5


Z * ! +, t ? +_0

AZ *N

. F ! : ! !$! +89 3/ * C 89 $! 7 / 8 ] f ! N

D" Z * & ! … #! !$! '! 7 / " &. Pg 6 # II ! '! 7 /


" 3 & 4 '! D 4 gH 0 D ! ! " " '! 7 / +89 q &0 A " & D 4
3 G+ G w L & ! +: +p! 4! ! ! I P! ! !$! $X 8 #'W $; +89 &< O
/ / * 1 +8! ! !$! ; "; + $] 8> 0 '; / !"y

D 8@ C $; / $! 7 / !& D ! ! " " … D" Z * & D ! ! " " … +89


4! " +p! 7 3 @ +p! 4! #! ! … < 7 = $X 8 „ s "; + (0 g 8> ! s e
I $! 7 / " 8> 9/ & G ! "I " G 3 + = (0 gC " … +89 4! < 5
+p! @ #! ! I ] $; : , f 7 = $! 7 / +89 G 3 + = G 8 6& $; / 5 +C " I
$! 7 / O ? 0 i $! 7 / G 3 + = G & $; O / 5 "/ " +.T 89+ ( $ ] = 8>
3 D ! ! " " '! 7 / f ! L"3( / O 7 = O 5 +C " ! ! " " I 2 +:
! ! " +r +89 " +r +89 C " ! !$! s e ! /" " "I
! ! " " ( +, 7Z 8@ P! " I 0 D & < 7 $! 7 / / O + = 7 = +89 ! +,
+ $ " Z " ? 3 ( P! ! !$! 0

[email protected] Page 22 of 44
iC +C $; / " ! !$! [ … '! 7 / … P! & . P g 6 0 g: f & N
"/& ! !2 " 2 L000 [ +p! $! 7 / " #! ! … ! +: f O 89 w 3 ! ! " "
! ! " " 0 8 !/ 5 "/ " H +.8 .E 8S " 8> 9/ & 4 "/ " 4
G G " B /
%! ! (0 g p! 3 ! ! " 8! " ? " / ! ! " ( f Z ':[ 0g 8
6& ! !$! F & f " ] ; Z \ ] ':[ 0 gC ! e H
8 .E 8S !& G U+ G
" B / %! ! ( ] 3 " / ! ! " f +89 " $! 7 ! !$! ; 3
: Z \ 3 $ ':[ J 0

D C/ " $; / " # II ! '! 7 / P! & D 4 gH 0 ! +: O 89 " 3


D ! ! " " '! 7 / 7, [ ! O # II ! '! 7 / 5 p! " D ! ! " " '! 7 /
P! & D 4 gH G.3 d = G0 g 8 6& ! : , / * 1 ! ! _ ; 3 8@ 7 $ C L & !0
3 ] & e 8 /0

.o L t 3 5 [ 8 8 / "
0 Š! [ & C X ! C A +_ : p! / * 1 8 7 +e 3 C
W / l 6 ] : : p! 7 3 ! 0

g 8 " Z * < i ! ! !$! [ I $! 7 / p! h+ 3 ;" ;


! !$! [ I $! 7 / w ; ! ! " +r +89 " 8 ! !$! : ,
" ; 3 " 7 = ) * +89 0 g 8 7 : +C p! ! 7 = h
/ * 1 ; !! s $ "+ " : ; 3 " ) * +89 !$ (0

.: , $; f \ C "y K O & f +89 : t " $ ! _ #! !


J &0 g 8 o L 5 Z *+ +, Z 0D" Z * _ C " ! ! "
$ R N V ) _ ”j +89 [ 7 \ ! " W 8 ; 7 = +89 " ! " ' C
! !$! 7 = +89 3 ] 7 = / * w+ ! " ! " &/" ( P! " $! 7 ]
: f ; 3 " 0 gC ! !$! ; 3 8 8@ 7 [ _ "Q ! 7 +r
”j = "! " ' ] e " 0

. " < $! 7 / D ! ! " " … f F/ " " & ! ! @ $? 7• > +, "


" 3 7 40 d @ / " & %" ! h : , " @ O $! 7 / O ? =0

g 8 6& 7 " ! !$! ! : , e "/ " aJDŠ B.uAB ‰ +, " $! 7 3


! ! " 8! +89 " ! !$! : , "; + $] 8> 8 ! 7 = , "
< $! 7 / I " !2 8> "/ " & +89 e 7 " I $! 7 / 5 " “
$! 7 /0 gC e "/ " aJDŠ gu B.uAB ‰ 3 ! ! " 8! +89 " f 7 = +89 " $! 7
" +: f7 = " / ƒ/ ! ) * ! !$! 8 ! : , C/ * " 3 I $! 7 /
7C C/2 8> "/ " & +89 e 7 ! +: ! s / " 3 I $! 7 / 7 3 0

i = f h : , e " ! "/ " < = !& !"y i = " " 8 6&


! : , e "/ " 6/ "/ " aJDŠ DWg#'‰ 4 "/ " R G
/ G " B / %! ! (0 A@ "/ " & ! f
: = +Q Z P! ! "/ " < N 3 " / ! ! " f +89 " $! 7 +, + $ " ! !$!
" : , ) * +89 8 h 8@ ; ! E" & = 2 +1 > ! +: ! : , C/
* " 3 I $! 7 / s ; f " 3 ! ! " +89 " $! 7 " " " I
7 ! " $! 7 / I 7C C/ f ; ; 3 " " & [ ] < " I (0
g 8> ! 5 "/ " &7 " ! !$! ; > +, " +: ! /" ( 8
7 = t +: : / > +, e 7 = 2 " < e f " [ $! 7 / I ;
! $& 7, 4 !! L +: : / > +, e " !& 7 = ~
; 8 8@ 7 $X !& 8@ 7 ! ! " " +: +89 w ( 8@ 7 &, !
" & "/ " 0

[email protected] Page 23 of 44
4.4. Backup Database

D" / O & F ! f $ _ 3 $! 7 / ! !$! 0 g 8 8@ C F ! r&


@ 6 5 " 3 ] $! 7 / " 0 .: [ ! f +, &< C W
7 = X 0

^ + J Q# ?

3 ] "/& " $ !& / O P! ! !$! 2 ! ! " " 2I !& I " / ] $! 7 /


'! 7 /
4! < $! 7 / ! !/ " 7( $q 3 ) * $! 7 / !/ ! !& / & ! I

% I 6 8 .N• '! 7 / •… 0$!7( !& !/ * , 8 ••0•D!/ ( 5 +, " $! 7


'! 7 / #
!0
'! 7 / … … 4! $! 7 /

# 7 !& !/ +89 ) * +, 4! $! 7 / 0 '! 7 / ! : , 4! _ $! 7 / Z * 8


'! 7 / % !
$! 7 / [ _ a "` $! 7 / (0

'! 7 / % $ $! 7 / [ O $! 7 / + +89 4! < $! 7 / !0

. F ! : , ;" $! 7 / +X / ! ( !& ;" ! $! 7 / I @ "


“ O $! 7 /0 D = 8> F ! f ;" $! 7 / +X +, : , 5 + 5 ; +Q
$ " w+ :! = $! 7 /0 d, ;" $! 7 / 5 B / %! ! $; e
) Y- 1 8 Y+ I - 0 g " ! $; : , 5
T & " /" 8 Z * ! N

S B %!
V"
/‚! / ‹ 7‹ 2 ‹… '! 7 /# ‹ 2 ‹BN• '! 7 / •… 0$!7‹

d, $! 7 / ! !$! $; : , 5 '! 7 / a z! "Q 7 < < ! !$! $! 7 / ! +:


8 Y( Y- UUU f !` "` 8 ] f ! N

[email protected] Page 24 of 44
! +: w! 5& " &< O P! ! !$! e 3 "/ " Z 9/0 D! : , "
$! 7 / +X 7•0

4.5. Restore Database

D 8@ 7 " ! !$! !/ L3 +X +89 4 w I O " 0 .3 = & +89


4! " ! !$! f " ! $ C $! 7 / "2 ! $! 7 / " > +, "0
! +: ! C " 0 d, " $; 8 Y( Y8 UUU f ?&
` "` 8 ] f ! N

[email protected] Page 25 of 44
gC $; " [ ! 7 3 P! !& [ 7 3 $; : e G
"/ " e $! 7 / I $! 7 /( 8 4 0

8 C $; " ` ! !$! : ™ @ ! ! +89 $! 7 / $; : , e "/ "


G * 8 ] f ! N

[email protected] Page 26 of 44
'; : , e ! ! !$! "/ ! " ! !& " "/ ! " ! 5& " 8> 9/ 8 +r
Z < 0

[email protected] Page 27 of 44
Bài 5: Data Integrity and Advanced Query Technique

g: +C #! ! J & ! : +C Z " x P! ! !$! !& : 3 7 3 !!


4! " ! !$! / Z L3 +3 6&0 gC ! ! 4! " ! !$! 7 = Z L3 ! :
! !$! ? Z " x " !! &(0 D " $ & F ! f$ ! 3 / 8 / 3/ +,
" ! !$! +89 " x 0

5.1. Các Ph ng Pháp Ð m B o Data Integrity

5 3 +, + $ " #! ! J &0 % 3 8D !& J L f +89 $


+C " 3 $ ! & < " / ; $ & F ! o : ! 3 3 < 0

• / N #! ! &/ o : ,+ $ " Z " x P! ! ! Z * $; 7 ! $3" ! ! &/


P! J ] $; 7 = , +8! 3 X ; " +89 0
• + + Ng "; 3 X +Q $ 2 : 7 = 8 +8 @ z "2 $ ! 7 !&
/& GG : p! 7 = $ C 7 "` ( !& 8! +89 +X p! I (0 i
C 7C ! !$! ! < = l 6 " " / ƒ/ +89 g !& g" g ]
4! g !! : , " 4 * 7 = L)! ! ! 7{ 8— $X G ƒG0
• N gC +89 " 3 X I! ] 7 $; 7 = +8! "
3 X * , " ] f 5 3 X Q +X &0 '; / 5 # I! + @ g"
g I " 0
• / N #! ! ; J# f + $ " Z & ? P! ! ! " !$ 0
• N dY& f / O ! + " Y " $ &0 ." ! ; X
$ ! 5 +, + $ " Z " x P! ! !0 ." ! & 6 ! 3/ +Q
< +, + $ " Z Z L3 P! +89 6/ "0
• 8 N dY& "$v ! Z $! 7`! E " /! $ P &C +, 8 Z @ 3 "
8@ +Y& P! 0 8 +8 @ .ŠB.i ." ! " 8
8> ! : L 8@ ) * .ŠB.i ." ! ] : Z L3 : , +Q _
." ! < " 7 +: h : " =0 . F "$v
< ! +: < 7C @ " +: P! !$ " 7 .ŠB.i " ! Z
P! !$ < : , +89 ;" ! @ . BWDB DW' B0
• N % "; " /" +Q $ +89 w 3 w+ 7 !$ +89
S/ ! 2 J 2 !& # 0 AZ * ! 7 : +89 $3 ! ] - :!
" 7 "/ +89 L E|( ~ ; 7 +: ! : , 5 +, + $ " &
+:0 D f +89 $ 7{ " 3 $ ! 0
• * N f +89 $ +C " $ : _J L 0

5.2. Constraints

." ! Z / "/ &( ! 3/ +Q < !$ !& +, 3


8 7 = Z L3 " ! !$! ! ! !(0 D 6 ! guD gS !& #B…WS D o +89 L
; " ! 8 F ! 7 = $!" 1 ! "; & +Y& h ] $ & U "; " !
K ! & i & ." ! 2S ." ! 2 …" i & ." ! . 7 ." ! 0

/R / D

% !$ 8> : !& _ : 3 X ! Z & ? +, L3 +X $? 7• "


!$ 0 D! 8> e K !&i & +89 ;" ! 7 !. ! !& W !$ @ K !&i &
." ! 0

)N _ P \ P N /R / 0 .: , : _ ! ! " ;" <


K ! & i &2 3 &7 = , 4! g 3 X " 3 < : , 5 !
8 3 X P! ? 3 ;" < K ! & i & / ! Z & ?0

[email protected] Page 28 of 44
i K ! & i & +89 ;" ! S J L f +89 w+ ;" ! +, & ] Z & ? 0 gC
" !$ +: 8! : . J L ] S Œ. J L f +89 ;" !0

.: , ;" ! K ! & i & ." ! 8 ! N

CREATE TABLE Table1


(Col1 INT PRIMARY KEY,
Col2 VARCHAR(30)
)

!&

CREATE TABLE Table1


(Col1 INT,
Col2 VARCHAR(30),
CONSTRAINT table_pk PRIMARY KEY (Col1)
)

9 1

'; : , ;" S ." ! +, + $ " 3 X P! " +: 7 = $X 5 6/0 D & S


." ! K ! & i & ." ! +_ + $ " Z & ? 8 $; < 5 S ." !
" 8> 9/ ! N

• +7 N N F / N M 7 [ J 6 N , Z # 3 / /0 g< @ h
: K ! & i & ." ! " !$ " 7 ! : , : _ S ." ! <
!$ 0
• +7 N N ` a + 0S " ! : , 3/ +Q < 4! 3 Xg
" 7 / !&7 & " ! ]7 = 0

.3 ;" ! S ." ! o 8 w 8 K ! & i & ." ! h !& K !&i &


S 0 f w+ ;" ! " E L7 ! ;" S
." ! 0

G R /

…" i & !& w 7C 9/ P! _ +89 ) * +, 3/ +Q < 7C ! ! ! !


!$ 0 …" 7 & P! !$ f 3 X P! K ! & 7 & P! !$ 7 3 F ! : , ;" !
_ …" 7 & " !$ 0

…" 7 & : , I ! C ( "K !& i & !& : S ." ! 0 …" 7 & :


, 4! g 0 %Q 5 * +Z Z P! …" i & ." ! +, 7, "3 ! ! 4! " !$ :
…" 7 & 4 !$ " ( 8 w ? : o 7, "3 = !! " !$ 4! K ! & 7 &
4 !$ !(0 AZ * C ! !! " !$ ! ] !! " !$ " < G 1 =G " / ! (
]7 = , I 89 _ !$ !0 #" +: …" i & " ! f+ $ " + _ +: 7 = L & !0
gC $; !! " !$ ! ] 8@ C $; / "/ !& !$ …" 7 & " !$
" 8@ 0

.: , ;" ! …" i & ." ! 8 ! N

CREATE TABLE Table1


(Col1 INT PRIMARY KEY,
Col2 INT REFERENCES Employees(EmployeeID)
)

!&

[email protected] Page 29 of 44
CREATE TABLE Table1
(Col1 INT PRIMARY KEY,
Col2 INT,
CONSTRAINT col2_fk FOREIGN KEY (Col2)
REFERENCES Employees (EmployeeID)
)

d= 7 F ! o O # !$ …" i & ." ! " 8> 9/N

• /9 N gC ! ! " f / ; $ : ™ "! " ! (


!& " ! P! ! h 3/ * " !! > = 47 = / !! f 0
• 7 3 1 $ b 0 gC 7 = !$ …" i & ." ! 7 / ! !! ]
: , 3 ] "/& ! ! [ " !$ @ ! " !$ 3 7 = O C0

. 7 ." ! 5 +, @ ; !& 7 , "3 3 X +89 / ƒ/ " 0 . 7 ." !


…" i & ." ! “ : 7, "3 3 X +8! " 8 7 3 “ …" i &
." ! w! < 3 X !$ ! +, " / ƒ/ 3 X +89 ?/ 6 !& 7 = " 7 . 7
." ! w! < $, 4 " " L/ " ( +, 7 , !L 3 X : 9/ 7 = 0 AZ
* ! : , 3/ +Q . 7 ." ! < ! ! & +, h ?/ 6 _ 8 [ ”|j +C
”| Ž H 0

D! : , ;" ! _ . 7 ." ! < 0 g " ! ! : , ;" . 7 ." ! <


_ $q 3 ;" ! . 7 ." ! 4 !$ !$ (0

.: , ;" ! . 7 ." ! 8 ! N

CREATE TABLE Table1


(Col1 INT PRIMARY KEY,
Col2 INT
CONSTRAINT limit_amount CHECK (Col2 BETWEEN 0 AND 1000),
Col3 VARCHAR(30)
)

D" Z * & ! @ ; 3 X ?/ 6 +89 P! ." [ +C | 0 AZ * ! f ;" !


. 7 ." ! 8 < 8 !$ N

CREATE TABLE Table1


(Col1 INT PRIMARY KEY,
Col2 INT,
Col3 VARCHAR(30),
CONSTRAINT limit_amount CHECK (Col2 BETWEEN 0 AND 1000)
)

D8 w 8 …" i & ." ! += 7 ! o O !$ . 7 ." ! " 8> 9/ J


!& S/ ! 7, "3 Z 9/ P! ! ! 7 = 3/ * " !! ; 0 D 8> 9/ 4 !
/ ! " 0

% L !& ;" ! ." ! $q B / %! ! ] 8 ! N

. 7 < < !$ " +: ! Y 3 5 # c)


UUUc

[email protected] Page 30 of 44
5.3. Advanced Query Techniques

D" / O & F ! f+ " Y Y Y !" 8 B B.D2 Jg B D000

.: , : O 8 ! o $C ! Y H $ 7, 8 G B B.D • … u% DW' BgW%B aŠB B000G


8 : , : _ 8> 7 = $ C +C Z ? Y !" P! :0

.F / 3/ +O& +P P! Y B B.D ? / 4 ;/ & < +Y& h ] $ & ƒ Z P!


& =N

B B.D ‚
† JgDu `‚ !$ ‡
… u% !$ ‚ " † aŠB B ! ‚ " " ‡
† V uSK '‰ " /‚$&‚ L/ " ‡
† ŠWAJgV ! ‚ " " ‡
† u #B '‰ " ‚ L/ " † W . › #B . ‡ ‡

. F ! f O 89 < 4 [ ! +_( " Y &0

! 7 &`" [ 7 :!( B B.D ! f : ! 3 3 ! +89 3 ! $q


? G2G0 .: R i &`" O ? ; " / O B B.D0

• Ni : 7 &`" & " ] f " 7C 3 7 = 5 ! 0 AZ * " u


!$ P! g" ` ! !$! ! !$! „ 7• @ ( 4! 3 X 5 6/ / !
! ( " /. &0 gC ! ! 3 /. & " +: “ & hL ?
O " 7C 6 +89 ! 5 8 ! N

SELECT DISTINCT ShipCity, ShipRegion


FROM Orders
ORDER BY ShipCity

• N gC ! +O < = ! : , 5 D"/ 7 &`" 0 gC : <


u #B '‰ ] 7C f +89 " 8@ ! +: @ 0. F ! o : ,
w! < / O H $q 3 < i &`" K "0 AZ * ! f | +O <
" 4 wN

SELECT DISTINCT TOP 10 ShipCity, ShipRegion


FROM Orders
ORDER BY ShipCity

• ( N d= 7 F ! " B B.D ! m +e F ! : , 5 ! !
4 [ !& C !& [ C } ( @ 7 &`" W !& 7 = : 7 &`" W N !$ ‚ ! W
!$ ‚! ! !& !$ ‚ ! !$ ‚! ! 0 AZ *N

USE pubs
SELECT p.pub_id, p.pub_name AS PubName
FROM publishers AS p

g " ! " ! : , 8@ ; L/ " 8 ! N

SELECT FirstName + ' ' + LastName AS "Employee Name",


IDENTITYCOL AS "Employee ID",
HomePhone,

[email protected] Page 31 of 44
Region
FROM Northwind.dbo.Employees
ORDER BY LastName, FirstName ASC

D" Z * < ! GB / "& g! G / l ƒ/ ; P! … g! ! g! +89


3 ! $q 7 " } 0% 3 X "; & +, GB / "& J#G0 iC f
+89 }/ " 4 w [ \ @ @ W .( s #B . [ @ @ \( " +: ! g! +89 }/
8@ 1 @ @ … g! 0

+ .

JgDu . ! " / ƒ/ ! !! [ !& _ !$ ! +: 7C f +89 " !$


@ 0 D!$ & +89 ;" ! " 7C P! Y B B.D JgDu0 AZ *N

SELECT FirstName, LastName


INTO EmployeeNames
FROM Employers

.Y < f ;" ! !$ @ : < B / "& g! @ … g! ! g! ! +:


7C +89 [ !$ B / "& f +89 " !$ @ &0 gC !$ B / "& g! 1 ;
f $3" “ 0 .Y & 8> !& +89 ) * +, 89 !! @ [ _ !$
7 3 ! " !$ @ 8> 5 " * +Z ; > /" ! & !$ (( 7 \ / w
Y J _ O 0

% 3 7 3 o !! [ !& _ !$ " !$ 7 3 5 G
UUU UUUG0 g 8 Y &7 = ;" ! !$ @0 g p! ! !$ +: / 1 ; 8@ 0
AZ *N

INSERT INTO EmployeeNames


SELECT FirstName, LastName
FROM Employers

. F 7 = : GE G " Y J &0

Q8.9 -S H(E +Q

V uSK '‰ 5 +, ;" ! 3 3 X - ! ! ! ( " [ " 7C +89 0 . h


: " [ 3 X < $ ( P! [ 0 .3 +89 +_ / q "
V uSK '‰ . ! 0 Šr& L Z * / 4 ;/ ! N

SELECT OrdD1.OrderID AS OrderID,


SUM(OrdD1.Quantity) AS "Units Sold",
SUM(OrdD1.UnitPrice * OrdD1.Quantity) AS Revenue
FROM [Order Details] AS OrdD1
WHERE OrdD1.OrderID in (SELECT DISTINCT OrdD2.OrderID
FROM [Order Details] AS OrdD2
WHERE OrdD2.UnitPrice > $100)
GROUP BY OrdD1.OrderID
HAVING SUM(OrdD1.Quantity) > 100

D" Z * < +O < ! " < $ ( [u # ! !$ @ 3œ| 0 !


+: C/ * u J#2 GS " G2 [ 7C < " +: GS " G
! ! " " 3 X - P! : 5 u J#(0 ŠWAJgV . ! +: !
s 8 I 5 +, e ; 3 3 X O = 0 ŠWAJgV . ! 8> + @ V uSK
'‰ Q 5 : , L ? < ^0

9+ .+

[email protected] Page 32 of 44
S " 7 &`" : * ƒ/ 7C P! !& _ ; 7C 0

AZ *N

V ) : !$ | ." W ! ! | (2 ." ' ( !$ ." . ! ! | (2 ." # (0 D!


! ! [ !$ | ƒ/ @ ! ! [ !$ +, ;" 7C & ? ! 8 ! N

SELECT * FROM Table1


UNION ALL
SELECT * FROM Table2

gC 7 = : 7 &`" W ] ! [ !$ f hL ? O " 7C 0
.s 7 5 W ] 3 " !$ +_ : " 7C $? ?/ 6/ ; 0

i #5 S " / F ! & N / $q ! ! ! &/ P! 3


8 4 / " /! $ 8 Z (0

5.4. Using JOINS

D" / O & F ! f ] , _ 3 "; š" " 0 'q 3 ) * šuJg $; : ,


!! [ _ !$ w! < ! " ! 3 !$ " ! ! " / (0 .: , : }
3 "; š" = * $q 3 ] ! N

D 4 w [ 3 ! / NJ š" 2 I u š" 2 u š" 2 … u š"

#5 J š" +, !! [ !& _ !$ " +: 3 X P! 3 +89 v" / L ?


!$ 4 / O ; ƒ" < ] 0 AZ *N

SELECT t.Title, p.Pub_name


FROM Publishers AS p INNER JOIN Titles AS t
ON p.Pub_id = t.Pub_id
ORDER BY Title ASC

. d

#5 I u š" +, !! [ !& _ !$ " +: ? $< !$ 4 ? 7 =


1 ; $< !$ 4 ! f +89 @ 3 3 X P! 3 +89 v" 0 +89
f $q @ P! !$ 4 ? 0 D4 / O = +\ < ] 0 AZ *N

USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a LEFT OUTER JOIN Publishers p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC, a.Au_fname ASC

8 . d

[email protected] Page 33 of 44
#5 u š" +, !! [ !& _ !$ " +: ? $< !$ 4 ! 7 =
1 ; $< !$ 4 ? f +89 @ 3 3 X P! 3 +89 v" 0 +89
f $q @ P! !$ 4 ! 0 D4 / O = +\ < ] 0 AZ *N

USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a RIGHT OUTER JOIN Publishers p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC, a.Au_fname ASC

G . d

#5 … u š" +, !! [ !& _ !$ " +: ? $< !$ 4 ? 4 !


+_ +89 e 3 3 X $< ! !$ $q ! ] h ?& O 0 D4 / O = +\ < ] 0 AZ
*N

USE Pubs
SELECT a.Au_fname, a.Au_lname, p.Pub_name
FROM Authors a FULL OUTER JOIN Publishers p
ON a.City = p.City
ORDER BY p.Pub_name ASC, a.Au_lname ASC, a.Au_fname ASC

#5 ." š" ƒ/ ! ! [ ! !$ " +: +89 $q @ P! !$ 4 ?


Y @ P! !$ 4 ! 0 AZ *N

USE pubs
SELECT au_fname, au_lname, pub_name
FROM authors CROSS JOIN publishers
WHERE authors.city = publishers.city
ORDER BY au_lname DESC

d, " Y &7 = : 7 &`" Gu G0

% , t _ 3 "; v" $; " ;& ) < / O $ 6/ |0

D: ; " $ & F ! +r ] , !! & " $q 3 5 3 ";


." ! 0g " !. F ! o $C ! _ 7{ 6 & Y !"0 ! $ e & 3 $;
O $ 6/ | +, :! ; 7 C 4 +r e [ $ | +C $ j 8@ 7 $; e C/ $ T0
i $ 6/ @/ " 4 w Y P " 3 &< O P! $ 6/ +Q !0 i = < $\
! $8@ "0

[email protected] Page 34 of 44
Bài 6: Stored Procedure and Advance T-SQL
D" $ & F ! f ] , 3 /" L/" !! " 0 ! +: f
$ ! 3 "; " K" . "0

6.1. S d ng bcp và BULK INSERT import data

" ! /" / 5 +, /" !& L/" !! [ !! I D L I !&


BL … ( " !& 89 ; 0 D 8> 7 &, 89 @ !! [ ! !$!
& 7 3 8 u ! 2 #' 000 ! 8@ C ! f L/" !! ! L I ! +: /"
" 5 $ / " ! 0% 8> 9/ = * ! L/" !! [ !
% " "I BL I BL I & : , / " /" ! !& ! !$! & 7 3 0

. F ! o : , &, !! " 5 Y -9 R + 8 0 D & <


'S i Jg B D _ P \ " " 4 7 = , L/" !! ! !! I
8 $ /0

d, : , !! " #! !$! 2 ! ! I / : ; $ p! : ? F
0 . F 7 ! ! +89 $ 7 "/& "/& "; 5 $ / !& 'S i Jg B D( " !$
" ] !$ +: / 1 ; ! ! +89 < " !// (0 g 89 ; 7 L/" !!
! !!I ] I @ f +89 ;" ! "Q ! ! I f $X " ` C : 1 ;0

.F / 3/ +O& +P P! $ / : , L " '""7 u 0 n +Y& h ] $ &


Z *+ _ 3 ) * $ / " ! 'S i Jg B D0

E? @ WN V ) $; L/" ! ! [ !$ u " K! #' +Y& ! !$! +89


;" ! " $ 6/ | ( ! L I " +: 3 +89 / Y 3 $q ? G•G0 '; : ,
8 ! N #u " ! /" / +3 " s ! N

bcp PracticeDB..Orders out c:\Orders.txt -c –T –t;

D" Z * < ! $ 7 "/& !$ u ! L I " +: N

N "/& ! ! [ !$ !& ` ! !! I N•u 0 L (0 g 89 ; ! : , 5 ` +,


/" !! [ L I " 0

N $ 7 "/& 5 7, . !! . !( C 7 = h t ] f 5 GDW'G ! !
• ( +, / Y +X 3 5 ` !! • ( +, / Y +X 3 8 3 3 X I! (0

eN ? G•G + ! ` GG " $C ! 5 G•G +, / Y +X 3 C 7 = f 5 3 X


Q +X 8 < (

N 5 gD( D " " +, 7C @ ! !$! 0 g p! C +r ! ! " / ƒ/( "


+89 a "` & ] +8 < +89 ) * 7 = O 5 < !
/! `" "7 3 0

E? @ fN D !& ] "/& " $ !$ ! : , 5 & +, / O !! L/" ! L I 8


! N

bcp "Select * From practiceDB..Orders" queryout c:\Orders.txt -c -


SVinhtai -Usa -Pabc

D" Z * & ! " $ !! " u !$ ! L I 5 &


! ! " 0

[email protected] Page 35 of 44
1 / N " $ C +Y& & 47 = / !$ 0

N < P! !& < P! J ! (

9N ! 5 +, " "

N /! `" 5 +, " " 0

E? @ g N 5 'S i Jg B D +, $ 7 "/& ! ! [ L I " ! !$! 0 % & W !&


'S i Jg B D DE " ! 4 7 = / " ! /" / &( +3 " 3
s ! N

BULK INSERT PracticeDB..Orders FROM 'c:\Orders.txt ' WITH (DATAFILETYPE


= 'CHAR')

D" Z * < #WDW…J BD‰KB• ‹.ŠW ‹ " $ C ! ! +89 4! ; . ! ! ! &/ 0 g<F 5


! ! &/ ; " ] 5 ‹aJ#B.ŠW ‹

. F N .3 ` " " ! ! E 0 g p! "! 8> f : p!


7 3 ! 0

6.2. Distributed Queries

d= 7 F ! !! [ ! !$! & 7 3 8 % W 2 u! 2
#' 000 !& 6 Z [ 7 3 ! O / 5 $ &0 f 5 7{
6 u B#' 3 WKJ +, &, 3 & & @ 3 ! !$! & 7 3 0 .: 3 +, & 6/ "
3 ! !$! & 7 3 5 JgiB# B AB W Š" ." / g! 0

7 " +89 5 +, & 6/ " 3 ! !$! & 7 3 0% 7 +r


/ ] ! : , & !! 5 I" E/! ! N 7 ‚ ‚ ! 0 ! !" 0 !0"$v ‚ ! 0
D" +: ! ! " 8> 8 +8 @ ! !$! ! 2 ! 8 +8 @ ! !$! "`
"$v ‚ ! 8 +8 @ !$ !& `0

AZ *N V ) ! / 7 " W ! !$! GK ! #'0 $G " +: 3


!$ +_ 8 w 8K! #' ! !$! " +89 ;" ! " / O $ 6/ |(0

% ) Y ! / 4 Y 8 5
! + I 0 ! +: 6/ " < P! 7
7 K! #'2 " / O + e % " "I š U0 u B#' K " 0 D" / O
6/ " X Z P! W ! !$! .N•K ! #'0 $( 7 ui0

D! f : 7 < 7 K! #' L ? 8@ / O &Ž 7 0V )


! !! [ 7 & ! : , 5 & W !& 8 ! N

Select * from LinkedPracticeDB...Customers

D" Z * < ! 5 < P! 7 " ! $! ? ] +, & 6/ "


! !$! ! / 5 I" E/! ! 8 " 8> 9/ & ! 5 I! ! < 7 = O
" $ C < P! .! ! " ! 8 / 5 ? ? +, / Y $ [ / O (0

g " 3 < ! : , 5 /! E " & @ . + 9 8S I " 8 ! N

[email protected] Page 36 of 44
Select * from OPENQUERY(LinkedPracticeDB,'Select * from Customers')

D" Z * < ! ?& I " uKBg SB ‰ f _ !! : , q ! 7 &`" … u% 8


!$ 0 i 5 uKBg SB ‰ I " ! O " $C < P! 7 & ! w
0

8 NI " " +89 5 8 w 8 " /" 0

( H +

g " 35 7 8 +r ] $ & < ! : , 5 ! " " / ! ! " p!


Y >2 ; > (0 g p! + @ ! !$! & ! 8> L &< & ] 5 7
s + @ & Y Y @ 5 +C ] ! : , ! ! $q . +8. !&
. + ( ( .98 I "

AZ *N ! o f !! [W ! !$! 8 < 5 . +8.

Select * from OPENROWSET('Microsoft.jet.oledb.4.0','C:\PracticeDB.mdb';


'admin'; '', Customers)

D" Z * < 7 5 uKBg ua BD ! O / +8! " ? = O C +, "


" ! !$! 8 < P! K " 2 X Z P! I 2 ! 2 /! `" 8> 9/ & 7 = : /! `" (
< P! !$ ! 0 %“ O ! w Y < +_ 7 , ! & "
7 +: C 5 7 ] h 7, ! O = 0 uKBg ua BD 8 w 8 uKBg SB ‰
“ : _ "` : , +Q " X Z P! !$ " Y &0

g " 3 5 < ! o : , 5 uKBg#WDW uS .B +, & 8 ! N

Select * from OPENDATASOURCE('Microsoft.jet.oledb.4.0',


'Data Source = C:\PracticeDB.mdb; User ID =
Admin; Password = ')
...Customers

D" Z * < ! ?& uKBg#WDW uS .B _ / O P! I" E/! ! p! 8 +8 @


< P! 7 ( " < !/ 5 < $! ? ? 0

6.3. Cursors

gC Z 3 } e ] " 8 w 8 " !& !! "


/" ! 0 g p! ! !! " " & ! +: : , O 89 @ [ "
$q 3 %" g L 000

.: R "; " D! ! E . " 2 WKJ . " . . " 0 D" +: D ! ! E WKJ


"; . " p! " +89 "! < $< / Z! 0D" 7 = 7 -$
e & ! h < 4 D! ! E " 0

D! ! E " +89 ;" ! < $q 3 Y D! ! E P &C +89 5 "


" /" 0 D 8@ C r& L ! Z * _ "N

DECLARE @au_lname varchar(40), @au_fname varchar(20)

DECLARE Employee_Cursor CURSOR FOR


SELECT LastName, FirstName FROM Northwind.dbo.Employees

OPEN Employee_Cursor

[email protected] Page 37 of 44
FETCH NEXT FROM Employee_Cursor INTO @au_lname, @au_fname
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Author:' + @au_fname + ' ' + @au_lname
FETCH NEXT FROM Employee_Cursor INTO @au_lname, @au_fname
END

CLOSE Employee_Cursor

DEALLOCATE Employee_Cursor

D" Z * < ! f ! g! … g! [ B / "& !$ P! g" ` ! !$! "!


" B / "& ‚. " ! +: O 89 < P! 3 / "& ! ] 0 d, @ " !
O " 3 $8@ ! N

|0 #5 Y #B. W B .S u +, 7 ! $3" " 0 i 7 ! $3" ! o / " $C Y


B B.D f +89 w +, ?& ! !0
0 #5 Y uKBg +, +8! ! ! < " & /"/ ! ! !(0 dY& Z F w Y
B B.D +89 7 ! $3" < 0
R0 #5 Y …BD.Š +, ?& [ !! [ " 0 .* , !/ e Y …BD.Š
_ O 0 …BD.Š 8 w 8 %" " W#u " “ : : , &, @
$q Y …BD.Š …J D2 …BD.Š gBkD2 …BD.Š K Ju 2 …BD.Š W D2 …BD.Š W' u SDB 2
…BD.Š B WDJAB 8 7 3 “ : ?& ! ! $\ " " ! !$ …BD.Š000… u%000JgDu
! !$ ‚ ! (0 D = 8> ! …BD.Š ! ! 8@ ! +: ""/ " @ " P! . "
$q s Q/ aŠJ B $q 3 7, ! "$! ! !$ ˆˆ…BD.Š‚ DWDS • p!
= (0
U0 i ! C H [ " ! : , SK#WDB !& #B BDB 5& " O " Z * &
h 5 K JgD(
j0 #5 Y . u B +, +: "0% &< "& " ( f +89 / :
8 " „ s +89 7 ! $3" : , uKBg ;0
T0 #5 Y #BW u.WDB +, / : Z " " 3 &< " " 7, <
P! " (0

8 " Z * < 8@ 7 5 . " ! o ! ! !$ ˆ! ‚I !


ˆ! ‚ ! ( +, 4! 3 3 X ?& +89 [ " 0 '; : , 5 & W ! &z +, ;& ) Z * < 0

6.4. Stored Procedures

D" $ e 8@ +Y& 7 5 & W ! &z F ! : , +Q < ! 3 :


Y " I 8@ ; / +, : , ) * ; ! &0 D & < !& ] ! "
L I ! : , ! " " 8@ ; " K" 0 3 N
P B K 'h ':[ F 5 % , 3 a :i N
5 3 'P 3 ':[ *> j : N 'O % 47 = / _ Y < ^(0

6.4.1. u Ði m C a Stored Procedure

" K" : 8 +, Z 8 ! N

• Ni w Y ] / 7, !/ " L
Y +: : +89 / ƒ/ w Y !& 7 = +1 > 7, ! F / 3/ 1 @ ;"
! L /! w 0 gC : _ Y 8 6& ! `" 7 : ,
+ + P! 0 f C 5 " /" ]
8> h Y + h 7, ! O ! +: ;" ! L
/! w 0 gC " /" +89 e _ O ] L / ! : , +89 ) * ;
< f ! 0g " ! F / 3/ P! 3 Y +r +89 7, !
8@ 7 ! < :7 = O 7, ; 7 w 0

[email protected] Page 38 of 44
• G I N% 7 " /" +89 ;" ! : : , +89 ) * ;0
d_ & f " $ " ] ! ! !$ &( m " 3 > !$
4 " , $< " " /" ( ! !$! 0 AZ * C : w !&
+- " +: _ Q " ] ! h !& +- " $< " " /" =0 g
4 * 5 " /" & : , f 7 = O / !& +- „ 8 Z @
$ @ 0 .o 8 3 = 6/ ] 7 3 " /" " / ƒ/ ! +8!
" 3 / /! ! ! ( _ 3 " / /! ! +1 > : o : 7
H e 3 " /" 7 3 0
• /NV ) F ! @ ; &L ? w C/ P! " +: "
!$ 2 ! : , C " /" +, & L ? h " / ƒ/ +:
+89 ) * " /" +r C ™ = 47 = , G+* G +C 3 !$ +:
3 w C/0 g " ! " /" : , +89 &/ r :!( +, H 8> Z $ "
60

6.4.2. Các Lo i Stored Procedure

" /" : , +89 ! j : 8 ! N

|0 / N " /" 4! " %! ! !$! 8>


$} +O $q C/ +O T 0 .3 " /" & "; $ E P &C 5
" ! !$! ! ! " ( &0 AZ * $; : , 7, ! ? 3
/" +! +89 ) * $ #" ! g! •W ! " $; : , 5 sp_who
@loginame='DomainName\Administrators' . .: H & " /" "
0 '; : , L C " '""7 u 0
0 N dY& "; 8> 5 ? 0 . F +89 4! " ! !$!
8> +89 C +, w = " +:0 D = 8> 8> ! : +C "
/" : +C "; &0 " ! " /" 8> +89 C $ #'W "Q / " ! 0
. F ! f$ _ 3 ;" " / "; & " / O 7C C/0
R0 / N " /" 8 w 8 " ! " /"
8 h 1 ; " +C 7 " " +r ;" ! F $X +: ; "Q "` 0
.3 " /" & +89 ;" ! < D /#' P! < F f $X 7
" " ;" ! F $X } +4 !& 7 "` 0 /
':[ 3 g = N $} +O $q ž(2 $} +O $q žž( " /"
+89 = M 7 5 -0 "; " ! h +89 ) * $ " " +r ;" ! F
$X L:! 7 " 2 s "; "$! : , +89 ) * $ $? 7• " " "0
K " " "; "$! " e 8> / $ ( 7 = , !& +- 0 "; "
/" +89 ;" w C/ < D /#' 7 3 @ "; < “ ! P \ 2
F k = \ # = ; % l 'a _ 7 "
I 0
U0 * N dY& "; " /" ) * 8 ] ";
L ! /" ! ( +89 " / # +, 4 H "; + P!
0 "; & 8> $} +O $q C/ +O * T 0AZ *2 xp_sendmail 5 +_
! " 8> " +: !& xp_cmdshell 5 +, ;& #u " ! 000 AZ *
xp_cmdshell 'dir c:\' . g _ "; L " /" +89 L 8 & "
/" 89 ; 0
j0 8 Ng " /" e " /" 7 3 0

6.4.3. Vi t Stored Procedure

D< = _ " K" 7 +89 ;" ! f 4! " & u$v !$ s


/ O L P! : 4! " & ." !$ 0 A] " K" o +89 L 8 "$v <
! o : , 5 3 8 . BWDB2 W DB 2 # uK +, ;" @ 2 !& +- !& L:! $\ "
/" 0. F ! r& L Z * ! _ " K" N d, ;" " /" $; : ,
5 B / %! ! 5 5 Y+ I UUU0 D " Z *
& ! f ;" ! " /" +, ` " " u !$ " K! #'0 d,
" " ! !$! ! O +8! " / 8 u J#2 K" g! " :

[email protected] Page 39 of 44
"( . " g! ! " (0 ! +: ! _ 7C " $C +: : = !&
7 = 0 • = 0

CREATE PROCEDURE AddNewOrder


@OrderID smallint,
@ProductName varchar(50),
@CustomerName varchar(50),
@Result smallint=1 Output
AS

DECLARE @CustomerID smallint


BEGIN TRANSACTION
If not Exists(SELECT CustomerID FROM Customers WHERE
[Name]=@CustomerName)
--This is a new customer. Insert this customer to the database
BEGIN
SET @CustomerID= (SELECT Max(CustomerID) FROM
Customers)
SET @CustomerID=@CustomerID+1
INSERT INTO Customers
VALUES(@CustomerID,@CustomerName)
If Exists(SELECT OrderID FROM [Orders] WHERE
OrderID=@OrderID)
--This order exists and could not be added any more so
Roll back
BEGIN
SELECT @Result=1
ROLLBACK TRANSACTION
END
Else
--This is a new order insert it now
BEGIN
INSERT INTO
[Orders](OrderID,ProductName,CustomerID)
VALUES(@OrderID,@ProductName,@CustomerID)
SELECT @Result=0
COMMIT TRANSACTION
END
END
Else
--The customer exists in DB go ahead and insert the order
BEGIN
If Exists(SELECT OrderID FROM [Orders] WHERE
OrderID=@OrderID)
--This order exists and could not be added any more so
Roll back
BEGIN
SELECT @Result=1
ROLLBACK TRANSACTION
END
Else
--This is a new order insert it now
BEGIN
INSERT INTO
[Orders](OrderID,ProductName,CustomerID)
VALUES(@OrderID,@ProductName,@CustomerID)
SELECT @Result=0
COMMIT TRANSACTION
END
END
Print @Result
Return

[email protected] Page 40 of 44
d, ;" ! " /" ! 5 . BWDB K u.B#S B " ! < P! : C
/" ! & " /" ] < ? ž 8@ < P! / " 0 gC &/ ] 5 aJDŠ
Bg. ‰KDJug 8@ W ( 3 / "Q " / /! ! 0 gC " / ] < 7 &`" uSDKSD
+q ! /! ! 0 D! : , " 3 X I! 5 F @ 7 ! $3" ! ! &/ P! /! ! 0 i, [ !
W / O $" & P! " /" 0

D" Z * < 8@ C ! 7 ! $3" $ C ˆ. " J# ! +: $} +O ! ! "


$q 'BVJg D Wg W.DJug " $ = & +89 w " D! ! " p!
"Q = "Q 7 = ] E! " " (0 D 8@ C ! 7 , !L 8> 7 3
8> @ !& o0 gC 8> @ ] ! G !&G ". " !$ = s C 7 =
] h "u !$ = 0 gC 8> " @ ! ?& . " J# @ ? [. "
!$ $\ " $ C ˆ. " J# ! +: H < + X 5 " J . " s 7C
C/0

! 7 8> " @ ! C/ * = $q 7, ! L u
: 1 ; 8! ] C " +r 1 ; ] 7 f $3" “ " u J# K !&
7 &(0 gC 8" < ] " " +: +r : " #' ] ! " $! 7 7C •| s C 7 = ]
! " @ " " ! ! " @ 7C _• 0

D8 w 8 6& C 8> " +r 1 ; ! +O < ( ] ! h


" 8 < 0D" e 8> 9/ 7, < ! +_ ! 7C 0

AZ * < +Y& h ! Z e \ s < w C ! !$! : ,/ 4 ;/ _ <


C " /" +s \ 7 C 4 } _ 7{ H _/" ! 0

% , t _$ e & $; O $ 6/ 0

D: ; " $ & F ! +r ] , 7{ 6 /" L/" ! ! 0 d1 > $C


! 3 3 ! ! [ 3 ! !$! & 7 3 5 $ &0 g 8 ! e ?
8> 5 ? 3 " /" 0 '; O , t ! s P! " /" $C 3 ;" !
F 0

A] 7 C 4 _ ! !$! : : < 7 3 < " 7 = 7 - $


e F = 7 = , ] $ & Q 7f [ C += 7 : 1 ƒ/ " < $; O +e + +e
; _ O +, } +89 Z / L < 3 C 7 = : 3 ]/ L < '""7
u (0 ! $ e & 3 $; O $ 6/ +, :! ; 7 C 4 +r e 0 i $ 6/
< " 4 w Y P " 3 &< O P! $ 6/ +Q !0 i = < $\ ! $8@ "0

[email protected] Page 41 of 44
Bài 7: Triggers And Views

[email protected] Page 42 of 44
)@ @

' |N V @ D 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000|
|0|0 . dQ J ! ! " (000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000|
|0 0 % F 7C 4 _ 3 A " P! 0000000000000000000000000000000000000000000000000000000000000000000000000
|0R0 .3 / O ! e " 00000000000000000000000000000000000000000000000000000000000000000000000
|0R0|0 ! " ! #! !$! B 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0 0 / ! " 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0R0 #! ! D ! I" ! " #D 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0U0 W ! & E 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0j0 B & E 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0T0 % ! #! ! N 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0M0 '""7 u E 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000R
|0R0c0 D"" E 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000U
' N V@ D 89 A_ D ! ! DE ( 0000000000000000000000000000000000000000000000000000000000000000000000000000000000j
0|0 #! ! # I " ! ! ## (N00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000j
0 0 #! ! ." " ! ! #. (N000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000j
0R0 %! / ! " ! ! #% (N00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000T
' RN # ! J / ! #! !$! 00000000000000000000000000000000000000000000000000000000000000000000000000000 |
R0|0 .? D F .P! 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
R0 0 .? D F A6 .P! % #! !$! 000000000000000000000000000000000000000000000000000000000000000000000000000 |
R0R0 g &< D} Š"; d .P! D ! ! " " D" 000000000000000000000000000000000000000000000000000 |R
R0U0 .? D F " .P! % #! !$! 00000000000000000000000000000000000000000000000000000000000000000000000000000 |j
R0j0 .3 ;" S #! !$! 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |T
R0T0 g d, .O 8 Ÿi D C iC % #! !$! 000000000000000000000000000000000000000000000000000000000000000000000 |c
' UN '! 7 / W " 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
U0|0 . C 89 K * Š1 # #! ! "! " ! &(000000000000000000000000000000000000000000000000000000000000 |
U0 0 .3 "; '! 7 / 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
U0R0 " & %" 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
U0U0 '! 7 / #! !$! 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 U
U0j0 " #! !$! 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 j
' jN #! ! J &! W ! &D 00000000000000000000000000000000000000000000000000000000000000000000000000000 c
j0|0 .3 K 8 K 3/ d ' " #! ! J & 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 c
j0 0 ." ! 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 c
j0R0 W ! &D 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 R|
j0U0 S šuJg 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 RR
' TN " K" ! W ! DE 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Rj
T0|0 ) * $ / 'S i Jg B D +, /" ! ! 00000000000000000000000000000000000000000000000000000000000000000000000000000 Rj
T0 0 # $ 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 RT

[email protected] Page 43 of 44
T0R0 . " 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 RM
T0U0 " K" 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Rc
T0U0|0 d, .P! " K" 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Rc
T0U0 0 .3 "; " K" 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Rb
T0U0R0 A C " K" 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 Rb
' MN D W A ` 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 U

[email protected] Page 44 of 44

You might also like