!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
1
!"# 1. $%&'(!)(*%# Microsoft SQL Server. !"!#$%&'( ')*!+$&+,)' -'". /...
166 downloads
245 Views
3MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
1
!"# 1. $%&'(!)(*%# Microsoft SQL Server. !"!#$%&'( ')*!+$&+,)' -'". /'00.* !"#$%&'( Microsoft SQL Server )!&*+,#- ) .#/' .0.-#$12# /,"2 3,1124 (master.model, msdb, tempdb), .53#(6,70# .&86#/18* 0195($,:0*, 0 %5&;"5),-#&;.!0# /,"2 3,1124. 5 8$5&+,10* 9,=&2 3,1124 )453'- ) 5.15)18* C(8%%8 9,=&5). H,/&0:2 SQL Server 2005 0.%5&;"8*- 3&' 5(C,10",:00 04 .-(,10: 3,1124 ) (,"/0#100 5301 0" .&*704 3)84 $#-535):
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ !"#$%&'(% $")!*+( (3&' !5-5(24 .5"3,1 !&,.-#(12= 013#!., -(#/8*70= 90"0+#.!5C5 %#(#.-(5#10' 3,1124 ) .55-)#-.-)00 .5 .-(8!-8(5= 013#!.,); ,-* (Heaps) – -,/&0:2, 1# 0$#*70# !&,.-#(15C5 013#!.,. J13#!.2 ) SQL Server 5(C,10"5),12 ) )03# B-3#(#);#). [ , ...n ] ] <schema_def> ::= { !"_#$%!& | AUTHORIZATION !"_'()*%(+,) | !"_#$%!& AUTHORIZATION !"_'()*%(+,) } <schema_element> ::= { -./%*%(%0 %_1)2( ,& | -./%*%(%0 %_./%*#1)'(%0 " |
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
10
grant_-.%/)1-/ | revoke_-.%/)1-/ | deny_-.%/)1-/ } -5- SQL-5%#(,-5( 5315)(#$#115 . .5"3,10#$ 15)5= .4#$2 $56#35/,)&'-; ) 1## 15)2# -,/&0:, 0 %(#3.-,)
', , -,!6# 8.-,1,)&0),-; %5&15$5+0' 3,112$ 5/E#!-,$ %5.(#3.-)5$ 9(," GRANT, DENY 0 REVOKE. J$' )&,3#&;:, .4#$2 ')&'#-.' 0$#1#$ %5&;"5),-#&' /,"2 3,1124. A%(#3#
# -,/&0:2 ",3,#-.' 5%#(,-5(5$ CREATE TABLE (%(0 @-5$ 1#5/4530$5 1,&0+0# .55-)#-.-)8*704 %5&15$5+0=), , 5%(#3#
# %(#3.-,)
' – 5%#(,-5(5$ CREATE VIEW. I,%(0$#(: USE MyDb; CREATE SCHEMA Schema1 AUTHORIZATION dbo CREATE TABLE Tbl1 (id1 int, f1 int, f2 varchar(20)) GRANT SELECT TO user1 DENY SELECT TO user2; GO D&' %5&8+#10' .%0.!, ).#4 95($ $5615 0.%5&;"5),-; %(#3.-,)
# sys.schemas:
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
11
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
3-4$&+. -'". /'00.* N5C0+#.!0 3,112# ) /,"# 3,1124 4(,1'-.' ) )03# 5/E#!-5) /,"2 3,1124. A/E#!-2 3,1124 4(,1'-.' ) .4#$# /,"2 3,1124. SQL Server %(#35.-,)&'#- .&*70# 5/E#!-2 3,1124: -,/&0:2; %(#3.-,)
'; .01010$2; 013#!.2; 4(,10$2# %(5:#38(2; -(0CC#(2; %5&;"5),-#&;.!0# -0%2 3,1124; 981!:00 %5&;"5),-#&'; !&*+0, 5/#.%#+0),*70# ..2&5+18* :#&5.-15.-;; 5C(,10+#10' :#&5.-15.-0; 8$5&+,10' %(,)0&, (0.%5&;"8*-.' 3&' 5/(,-15= .5)$#.-0$5.-0) < 5/E#!-,$ /,"2 3,1124 -,!6# 5-15.'-.' .4#$2, %5&;"5),-#&0 0 (5&0. B SQL Server ))#3#12 15)2# 5/E#!-2, 0.%5&;"8#$2# Service Broker: -0%2 .55/7#10= (.-(8!-8(, .55/7#10', 5-%(,)&'#$5C5 5- 5315C5 .#()0., 3(8C5$8), !51-(,!-2 (.5C&,G#10' $#638 3)8$' .#()0.,$0), 5+#(#30 (.55/7#10', 1,%(,)p# .#()0.8), .#()0.2 (1,/5(2 ",3,+, C3# !,63,' ",3,+, %(#3.-,)&'#-.' !51-(,!-5$), .#()0.12# %(5C(,$$2.
15"/'0!$ -'". /'00.* F5"3,-; /,"8 3,1124 $5615 !,! %(5C(,$$12$ %8-#$, )2%5&10) SQL5%#(,-5( CREATE DATABASE, -,! 0 0.%5&;"8' .(#3.-), Micrisoft SQL Server Management Studio.
12
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
13
>(0 %53!&*+#100 ! SQL-.#()#(8 .&#- )2/(,-; -0% .#()#(,: Database Engine, Analysis Services, Reporting Services, SQL Server Mobile, Integration Services, 0 -0% ,8-#1-090!,:00 %5&;"5),-#&': Windows Authentication 0&0 SQL Server Authentication. D&' .5"3,10' 15)5= /,"2 3,1124 .&#- ) Micrisoft SQL Server Management Studio ) 5!1# Object Explorer )2%5&10-; 3&' .#!:00 Database !5$,138 !51-#!.-15C5 $#1* New Database 0 ) %(#3&56#115$ 3,# 30,&5C# ))#.-0 0$' .5"3,),#$5= %5&;"5),-#&;.!5= /,"2 3,1124. I, .-(,10:# Options 30,&5C, New Database .&#- ",3,-; %,(,$#-(2 /,"2 3,1124. ?#60$ (,/5-2 . !8(.5(5$ 90!.0(8#-.' 3)8$' %,(,$#-(,$0: Close Cursor on Commit Enabled – %,(,$#-( 5%(#3#&'#- /83#- &0 ",!(2- !8(.5( %(0 90!.,:00 -(,1",!:00, ) !5-5(5= 3,112= !8(.5( /2& 5-!(2-. >5 8$5&+,10* "1,+#10# %,(,$#-(, (,)15 false – !8(.5( 5.-,#-.' 5-!(2-2$ %5. 90!.,:00 -(,1",!:00, 0 %(0 5-!,-# -(,1",!:00 ",!(2),*-.' !8(.5(2, !5-5(2# 1# /2&0 5%(#3# !,! STATIC 0&0 INSENSITIVE. A-$#-0$, +-5 %(0 "1,+#100 True 90!.,:0' 0&0 5-!,- -(,1",!:00 5"1,+,#- ",!(2-0# !8(.5(,; Default Cursor – %,(,$#-(, 5%(#3#&'*70= %5 8$5&+,10* %5)#3#10# !8(.5(,, !,! LOCAL 0&0 GLOBAL ("1,+#10# %5 8$5&+,10*). D&' 5C(,10+#10' 35.-8%, ! /,"# 3,1124 "1,+#10# %,(,$#-(, Restrict Access $5615 ",3,-; !,!: Multiple – ! /,"# 3,1124 5315)(#$#115 (,"(#G#1 35.-8% 1#.!5&;!0$ %5&;"5),-#&'$ ("1,+#10# %5 8$5&+,10*); Single – ) !,632= $5$#1- )(#$#10 ! /,"# 3,1124 $56#- /2-; %53!&*+#1 -5&;!5 5301 %5&;"5),-#&;; Restricted - ! /,"# 3,1124 $5C8- /2-; %53!&*+#12 -5&;!5 %5&;"5),-#&0, 0$#*70# %5&15$5+0', 5%(#3#&'#$2# (5&'$0 db_owner, dbcreator 0&0 sysadmin.
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
14
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
15
!"# 2. +!#,'-#.'/ 012(*3# ) 4#-#" 0#556& 2%!02(7#"' /-6)# SQL. 6".& SQL O"2! SQL %(#31,"1,+#1 3&' 35.-8%, ! 0195($,:00 0 8%(,)
' (#&':05115= /,"5= 3,1124. O"2! SQL 5%(#3#&'#-: 5%#(,-5(2 '"2!,, 1,"2),#$2# 015C3, !5$,13,$0 '"2!, SQL; -0%2 3,1124; 1,/5( ).-(5#1124 981!:0=. >5 .)5#$8 &5C0+#.!5$8 1,"1,+#10* 5%#(,-5(2 '"2!, SQL +,.-5 (,"/0),*-.' 1, .&*70# C(8%%2: '"2! 5%(#3#
' 3,1124 DDL (Data Definition Language); '"2! $,10%8&0(5),10' 3,112$0 DML (Data Manipulation Language). O"2! 5%(#3#
' 3,1124 )!&*+,#- 5%#(,-5(2, 8%(,)&'*70# 5/E#!-,$0 /,"2 3,1124. < 5/E#!-,$ /,"2 3,1124 5-15.'-.' -,/&0:2, 013#!.2, %(#3.-,)
'. O"2! $,10%8&0(5),10' 3,112$0 )!&*+,#- 5%#(,-5(2, 8%(,)&'*70# .53#(6,10#$ -,/&0: /,"2 3,1124 0 0")!,*70$0 0195($,:0* 0" @-04 -,/&0:. O"2! DML 5%(#3#&'#- .&*70# 5%#(,-5(2: SELECT – 0")+#10# 3,1124 0" 5315= 0&0 1#.!5&;!04 -,/&0:; INSERT – 35/,)
# .-(5! ) -,/&0:8; DELETE – 83,
# .-(5! 0" -,/&0:2; UPDATE – 0"$#1#10# "1,+#10= %5= ) -,/&0:#.
7$)$&89#$0!$ 2$:/, -'"'2! /'00.* A301 %5&;"5),-#&; $56#- (,/5-,-; . 1#.!5&;!0$0 /,",$0 3,1124. D&' %#(#!&*+#10' 1, !51!(#-18* /,"8 3,1124 %(0$#1'#-.' SQL-5%#(,-5( USE, !5-5(2= 0$##- .&*7## 95($,&;15# 5%0.,10#: USE { !"_2)3&_*)00&$ }
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
16
B.# 5%#(,-5(2, )2%5&1'*70#.' %5. 5%#(,-5(, USE /838- 0.%5&;"5),-; 8!,",118* /,"8 3,1124. D&' )2%5&1#10' %#(#453, 1, 3(8C8* /,"8 3,1124 %5&;"5),-#&; 35&6#1 5/&,3,-; .55-)#-.-)8*70$0 %5&15$5+0'$0, 1,%(0$#(, 0$#-; (5&; dbo 0&0 sysadmin.
7$)$&89#$0!$
!"#$ %&'(!%)(* + ,-.&/'!'+0
!"#$% '()!*"$"+, !"!#$%& '(#)#( *+,- +.!$#,& */' .0+"# (12#$)+3 !*'%-4 *+,- +.!$#,&), ',' */' .5 +.# .5*+,)(+6+ (+"1,&. 7'%$#(! 8# +*!%)+%$' %$/+'$%& )! *+%,#"+.!$#,-)+( *+"9,42#)'' 9 %#/.#/1 SQL Server, ! !$#( 9 %!(+3 8! # "!))50. :! +3 8!))50 *+ 1(+,2!)'4 *#/.+)!2!,-)+ 1%$!)!.,'.!#$%& 8! ! "!))50 master. ;/' !1$#)$'!4$%& . %#9='' Security +9)! Object Explorer %/#"5 Micrisoft SQL Server Management Studio, ! *+,- +.!$#,' 9!>"+3 8! 5 "!))50 +$+8/!>!4$%& . %#9='' Security %++$.#$%$.14@#3 8! 5 "!))50. A! %,#"14@#( /'%1)9# */#"%$!.,#)+ %++$.#$%$.'# (#>"1 12#$)+3 !*'%-4 user1 ' '(#)#( *+,- +.!$#,& 8! 5 "!))50 MyDB name_user1.
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
17
B+>)+ 6+.+/'$-, 2$+ */' *+"9,42#)'# 9 8! # "!))50 */+'%0+"'$ *#/.'2)!& ()! 1/+.)# %#/.#/!) ' .$+/'2)!& ()! 1/+.)# 8! 5 "!))50) '"#)$' ] [ ,...n ] ) [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] [ { TEXTIMAGE_ON { filegroup | "default" } ] [ ; ] ::= [ CONSTRAINT constraint_name ] { { PRIMARY KEY | UNIQUE } [ CLUSTERED | NONCLUSTERED ] [ WITH FILLFACTOR = fillfactor | WITH ( < index_option > [ , ...n ] ) ] [ ON { partition_scheme_name ( partition_column_name ) | filegroup | "default" } ] | [ FOREIGN KEY ] REFERENCES [ /3+!) . ] referenced_table_name [ ( ref_column ) ] [ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( %$8_(17)9+. + ) }
-./0.1!#%. 0.&)0$2$*$""(3 )0+*!4$"+5 1./ 2.""(3
22
#) '(#$- )!2#)'# +$,'2)+# +$ NULL; UNIQUE – .%# )!2#)'& %$+,8=! "+,>)5 85$- 1)'9!,-)5; PRIMARY KEY – 1%$!)!.,'.!#$ +"') %$+,8#= 9!9 *#/.'2)53 9,42 ' +")+./#(#))+ *+"/! 1(#.!#$ 2$+ .%# )!2#)'& %$+,8=! 81"1$ 1)'9!,-)5; CLUSTERED – +*/#"#,$ %+ "!)'# 9,!%$#/)+6+ ')"#9%!; NONCLUSTERED– +*/#"#,$ %+ "!)'# )#9,!%$#/)+6+ ')"#9%!; CHECK (condition) – 19! 5.!#(+# . %9+89!0 1%,+.'# '%*+,- 1#$%& ",& %/!.)#)'& )!2#)'# %$+,8=! ' .+ ./!@!#$ TRUE, FALSE ',' UNKNOWN. K%,' */' *+*5$9# .5*+,)#)'& SQL-+*#/!$+/! .+ ./!@!#(+# )!2#)'# /!.)+ FALSE, $+ +*#/!$+/ .5*+,)#) )# 81"#$; REFERENCES table (fields_list) – +6/!)'2#)'# $/#81#$ %+.*!"#)'& )!2#)'3 %$+,8=+. "!))+3 $!8,'=5 % 19! !))5(' %$+,8=!(' /+"'$#,-%9+3 $!8,'=5. J6/!)'2#)'& =#,+%$)+%$' ",& $!8,'=5 (+61$ 19! 5.!$-%& %,#"14@'(' 7+0)()&') '&?"$-%6'' '7 5%78 2%&&8= +8*"0&3)!43 *"4$)24!+""*)$%!"$% 7%*$"4% SELECT, /"!"$8: '-))! 40)2.1@)) ?"$-%09&") "*'4%&'): SELECT [WITH [,...n]] < !"#$%&'% (#)"*+#> [ ORDER BY { !"#$%&'%_()* +,-"*(-.' #&'* | &-/%"_01-)23# [ ASC | DESC ] } [ ,...n ] ] [ COMPUTE { { AVG | COUNT | MAX | MIN | SUM } ( !"#$%&'% ) } [ ,...n ] [ BY !"#$%&'% [ ,...n ] ] ] [ FOR { BROWSE | <XML> } ] [ OPTION ( [ ,...n ] ) ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
26
< !"#$%&'%_(#)"*+#> ::= { | ( < !"#$%&'%_(#)"*+#> ) [...n ] ] ] <XML>::= XML {{ RAW [ ( 'ElementName' ) ] | AUTO } [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [, { XMLDATA | XMLSCHEMA [('TargetNameSpaceURI')]}] [, ELEMENTS [ XSINIL | ABSENT ] ] | EXPLICIT [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [ , XMLDATA ]
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
27
] | PATH [ ( 'ElementName' ) ] [ [, BINARY BASE64 ][, TYPE ][, ROOT [('RootName')]] [ , ELEMENTS [ XSINIL | ABSENT ] ] ] }
A40' "*)$%!"$ SELECT +8*"0&3)!43 '7 *$'0"B)&'3 &% 2$.#"- 378/) *$"#$%--'$"+%&'3, !" ?"$-'$.)!43 $)7.09!'$.1@': &%5"$, $%7-)@%)-8: + *%-3!' *$'0"B)&'3 '0' 4)$+)$% CD, % 7%!)- *$'0"B)&') '7+0)/%)! 2%&&8) '7 $)7.09!'$.1@)#" &%5"$% + 4+"' *)$)-)&&8). E"40) ?$%78 SELECT ./%78+%)!43 4*'4"/ +8$%B)&':, "*$)2)031@': 7&%()&'3 ?"$-'$.)-8) 7%*$"4"-. < 4%-"- *$"4!"- 40.(%) 4*'4"/ +8$%B)&': 3+03)!43 4*'4/"- *"0): !%50'68. A40' !$)5.)!43 '7+0)()&') 7&%()&': +4)= *"0):, !" +-)4!" 4*'4/% *"0): -"B&" ./%7%!9 4'-+"0 *. F%*$'-)$: SELECT * FROM tbl1; . >-3 *"03 -"B)! 58!9 /+%0'?'6'$"+%&" '-)&)- !%50'68, ./%78+%)-8()$)7 !"(/.. F%*$'-)$: SELECT tbl1.f1, tbl2.f1 FROM tbl1, tbl2; . G$%7% FROM "*$)2)03)! "2&. '0' &)4/"09/" !%50'6 '0' *"27%*$"4"+, '4*"097.)-8= 203 '7+0)()&'3 2%&&8=. G$%7% WHERE "*$)2)03)! .40"+'), /"!"$"-. 2"0B&8 .2"+0)!+"$3!9 +4) 4!$"/', '4*"097.)-8) 203 ?"$-'$"+%&'3 $)7.09!'$.1@)#" &%5"$%. E$)2'/%! 4"2)$B'! "2&" '0' &)4/"09/" +8$%B)&':, +8*"0&31@'= 4$%+&)&'3. < +8$%B)&'3= -"#.! .(%4!+"+%!9 '-)&% 4!"056"+, ?.&/6'' %#$)#'$"+%&'3, *)$)-)&&8). H$"-) 4!%&2%$!&8= "*)$%!"$"+ 4$%+&)&'3, !%/'= /%/ =, , >, =, ::= { ON [ ) ] } ::= [ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ] JOIN ::= "#,(-. +_$ %!"& PIVOT table_alias ::= ( 12.3&"+_ 40)4"0(* ."+ ( value_column ) FOR pivot_column IN ( ) ) ::=
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./ "#,(-. +_$ %!"&
UNPIVOT
32 !" #_$ %!"&'
::= (value_column FOR pivot_column IN (< /$ !2_ )!,+-!1>)) < .$ !/_ )!,+-!0> ::= "5+_#$(!%& [ , ... ] !"# $%&'& FROM ()%*+*",*- .("** (+/(0 -&."#12 #"# )(+'&)%(!&, -( 3!* 4-# -&."#12 !(*+#/,5-!,. 6*%*7%*!-/(* !(*+#/*/#* (CROSS JOIN), /&'23&*8(* -&79* +*7&%-(328 )%(#'3*+*/#*8 !('+&*- %*':";-#%:5*+#/*/#* +&//2?, /* )%*+:!8(-%*//(* !?*8(0 (%(+#-*";!7#8# # 3/*@/#8# 7"5A&8#). =(*+#/,*82* -&."#12 )*%*A#!",5-!, A*%*' '&),-:5 3( $%&'* FROM ()*%&-(%& SELECT. B( $%&'* FROM 8(9/( #!)(";'(3&-; !"*+:5 | ( ) [UNION [ ALL ] < /"-$3$2*-$&_4*/5! *> | ( ) [ ...n ] ] D%&'& UNION (.>*+#/,*- %*':";-&-2 +3:? '&)%(!(3 )( !"*+:5*+#/,*82? !-(".1(3 +("9*/ .2-; (+#/&7(328 #"# )%#3(+#828. J&7 /*";', (.>*+#/,-; '/&A*/#, #' !-(".1& -#)& integer # !-(".1& -#)& varchar; #' %*':";-#%:5*+#/*/#* +3:? )(+'&)%(!(3 &/&"(E#A/( $%&'* ALL !( !"*+:5*+#/*/#, 8(9/( #!)(";'(3&-; !7(.7#
!"#!$%&'! #!()#*+*,*--./ )#0,%1*-02 $!" +!--./
36
D%&'& INTERSECT )('3(",*- 32.%&-; -(";7( -* !-%(7#, 7(-(%2* )%#!:-!-3:5- 3 7&9+(8 (.>*+#/,*8(8 %*':";-#%:5*+#/,*8(8 %*':";-#%:5