Вы находитесь на странице: 1из 10

SQL Commands

Sunday, January 13, 2008 11:44 AM

SQL -Structured Query Language SQL (usua y !ronounced /skjul/ or somet"mes /sikwl/# "s a data$ase com!uter anguage des"gned %or t&e retr"e'a and management o% data "n re at"ona data$ase management systems (()*MS#, data$ase sc&ema creat"on and mod"%"cat"on, and data$ase o$+ect access contro management,-1.-2. SQL "s an acronym %or Structured Query Language, SQL "s a standard "nteract"'e and !rogramm"ng anguage %or /uery"ng and mod"%y"ng data and manag"ng data$ases, A t&oug& SQL "s $ot& an A0S1 and an 1S2 standard, many data$ase !roducts su!!ort SQL 3"t& !ro!r"etary e4tens"ons to t&e standard anguage, 5&e core o% SQL "s %ormed $y a command anguage t&at a o3s you to retr"e'e, "nsert, u!date, and de ete data, and !er%orm management and adm"n"strat"'e %unct"ons, SQL a so "nc udes a ca - e'e "nter%ace (SQL6CL1# %or access"ng and manag"ng data and data$ases remote y, 5&e %"rst 'ers"on o% SQL 3as de'e o!ed at 1*M $y )ona d ), C&am$er "n and (aymond 7, *oyce "n t&e ear y 1890s, 5&"s 'ers"on, "n"t"a y ca ed SEQUEL, 3as des"gned to man"!u ate and retr"e'e data stored "n 1*M:s or"g"na re at"ona data$ase !roduct, System (, 5&e SQL anguage 3as ater %orma y standard";ed $y t&e Amer"can 0at"ona Standards 1nst"tute (A0S1# "n 188<, Su$se/uent 'ers"ons o% t&e SQL standard &a'e $een re eased as 1nternat"ona 2rgan";at"on %or Standard";at"on (1S2# standards, 2r"g"na y des"gned as a dec arat"'e /uery and data man"!u at"on anguage, 'ar"at"ons o% SQL &a'e $een created $y SQL data$ase management system ()*MS# 'endors t&at add !rocedura constructs, contro -o%-% o3 statements, user-de%"ned data ty!es, and 'ar"ous ot&er anguage e4tens"ons, ="t& t&e re ease o% t&e SQL:1888 standard, many suc& e4tens"ons 3ere %orma y ado!ted as !art o% t&e SQL anguage '"a t&e SQL >ers"stent Stored Modu es (SQL6>SM# !ort"on o% t&e standard, Common cr"t"c"sms o% SQL "nc ude a !erce"'ed ac? o% cross-! at%orm !orta$" "ty $et3een 'endors, "na!!ro!r"ate &and "ng o% m"ss"ng data (see Null (SQL)#, and unnecessar" y com! e4 and occas"ona y am$"guous anguage grammar and semant"cs, Some Common SQL Data Types: Numeric: 0@M*A( 105ABA( SMALL105 )AC1MAL Character: CCA( DA(CCA( Date: )A5A

Some Common SQL Commands: L"st"ng 5&e 5a$ e Contents 1% you 3ant to see t&e contents o% a ta$ e, use t&e SALAC5 command %o o3ed $y t&e des"red attr"$utes, 1% you 3ant to see a t&e attr"$utes "sted, use t&e E (aster"s?# to "nd"cate Fa attr"$utesF 7or e4am! e to "st a t&e attr"$utes and a t&e ro3s o% t&e C@S52MA( 5a$ e, useG SALAC5 E 7(2M C@S52MA(G *e o3 "s t&e out!ut %rom t&e C@S52MA( 5a$ e:

2r "nstead o% us"ng t&e E c&aracter (3" dcard#, eac& o% t&e ta$ e:s attr"$utes may $e "sted: SALAC5 C@SHC2)A, C@SHL0AMA, C@SH70AMA, C@SH10151AL, C@SHA(AAC2)A, C@SH>C20A 7(2M C@S52MA( >art"a L"st"ng 2% 5a$ e Contents Iou can se ect !art"a ta$ e contents $y nam"ng t&e des"red %"e ds and $y ! ac"ng restr"ct"ons on t&e ro3s to $e "nc uded "n t&e out!ut, 5&e %o o3"ng synta4 ena$ es you to s!ec"%y 3&"c& ta$ e 'a ues are !resented: SALAC5 Jco umn(s#K 7(2M Jta$ e nameK =CA(AJcond"t"onsK 7or e4am! e, t&e /uery: SALAC5 C@SHC2)A, C@SHL0AMA, C@SH70AMA 7(2M C@S52MA(

=CA(A C@SHC2)A L 10018 5&e /uery a$o'e 3" return t&e %o o3"ng resu ts:

0umerous og"ca restr"ct"ons may $e ! aced on t&e se ected ta$ e contents, 5&e SQL command structure t&us !ro'"des a most "m"t ess /uery % e4"$" "ty, 7or e4am! e, t&e mat&emat"ca sym$o s s&o3n $e o3 may $e used to restr"ct out!ut: Mat&emat"ca 2!erators Sym$o L J JL K KL JK Mean"ng A/ua 5o Less 5&an Less 5&an or A/ua 5o Breater 5&an Breater 5&an or A/ua 5o 0ot e/ua to E

E Some SQL 'ers"ons use ML rat&er t&an JK 5&e %o o3"ng e4am! e uses t&e F0ot A/ua 5oF o!erator: SALAC5 C@SHC2)A, C@SHL0AMA, C@SH70AMA, C@SH10151AL, C@SHA(AAC2)A, C@SH>C20A 7(2M C@S52MA( =CA(A C@SHC2)A JK 10018G 5&e out!ut returns a t&e ro3s "n t&e C@S52MA( 5a$ e e4ce!t %or 3&ere C@SHC2)A L 10018:

@s"ng t&e >(2)@C5 5a$ e to %o o3 t&e e4am! e $e o3, 3e on y 3ant to /uery data 3&ere t&e >roduct >r"ce "s ess t&an or e/ua to N 10,00, See >(2)@C5 5a$ e $e o3:

SALAC5 >(2)H)ASC(1>5120, >(2)H>(1CA, >(2)H20HCA0) 7(2M >(2)@C5 =CA(A >(2)H>(1CA JL 10G 5&e %o o3"ng resu ts are returned:

@s"ng Mat&emat"ca 2!erators on )ates @se t&e 10DA052(I to /uery %or a records 3&ere t&e 1n'entory )ate "s ess t&an or e/ua to 1161<62009

SALAC5 10DH0@M*A(, C@SHC2)A, 10DH)A5A 7(2M 10DA052(I =CA(A 10DH)A5A JL :1161<62009: 5&e %o o3"ng resu ts are returned:

Log"ca 2!erators: A0), 2(, and 025 SQL a o3s t&e use o% og"ca restr"ct"ons on "tOs /uer"es, 7or e4am! e, "% you 3ant a "st o% ta$ e contents %or A"t&er t&e C@SHC2)A 10013 2( C@SHC2)A 10016 us"ng t&e C@S52MA( 5a$ e, you can use t&e %o o3"ng command se/uence: SALAC5 C@SHC2)A, C@SHL0AMA, C@SH70AMA, C@SH10151AL, C@SHA(AAC2)A, C@SH>C20A 7(2M C@S52MA( =CA(A C@SHC2)A L 10013 2( C@SHC2)A L1001<G 5&e %o o3"ng resu ts are returned:

5&e og"ca A0) &as t&e same SQL synta4 re/u"rement, 5&e %o o3"ng command generates a "st o% a ro3s %or 3&"c& >H>(1CA "s ess t&an NP0,00 A0) %or 3&"c& t&e >H10)A5A "s a date "s a date occurr"ng a%ter 1061162009

SALAC5 >H)ASC(1>5120, >H10)A5A, >H>(1CA, DHC2)A 7(2M >(2)@C5 =CA(A >H>(1CA J P0 A0) >H10)A5A K :1061162009: 5&e /uery 3" return t&e %o o3"ng resu ts:

Iou can a so com$"ne t&e og"ca 2( 3"t& t&e og"ca A0) to ! ace %urt&er restr"ct"ons on t&e out!ut, 7or e4am! e, su!!ose you 3ant a ta$ e "st"ng %or t&e %o o3"ng cond"t"ons:

5&e >H10)A5A "s a%ter 1061162009 and t&e >H>r"ce "s ess t&an NP0,00, 2r, t&e DHC2)A "s 24288 5&e re/u"red "st"ng can $e !roduced $y us"ng: SALAC5 >H)ASC(1>5120, >H10)A5A, >H>(1CA, DHC2)A 7(2M >(2)@C5 =CA(A (>H>(1CA J P0 A0) >H10)A5A K :1061162009:# 2( DHC2)A L2144 5&e %o o3"ng resu ts return:

S!ec"a 2!erators SQL a o3s t&e use o% s!ec"a o!erators "n con+unct"on 3"t& t&e =CA(A c ause, 5&ese s!ec"a o!erators "nc ude:

*A5=AA0 - used to de%"ne a range 1S 0@LL - used to c&ec? 3&et&er an attr"$ute 'a ue "s nu , L1QA - used to c&ec? %or s"m" ar c&aracter str"ngs 10- used to c&ec? 3&et&er an attr"$ute 'a ue matc&es a 'a ue conta"ned 3"t&"n a (su$#set o% "sted 'a ues AR1S5- used to c&ec? 3&et&er an attr"$ute &as a 'a ue, 1n e%%ect, AR1S5 "s t&e o!!os"te o% 1S 0@LL 1% you 3anted to see a "st"ng %or a !roducts 3&ose !r"ces are $et3een NP0,00 and N100,00, use t&e %o o3"ng command se/uence: SALAC5 E 7(2M >(2)@C5 =CA(A >H>(1CA *A5=AA0 P0,00 A0) 100,00 1% your )*MS does not *A5=AA0, you can use: SALAC5E 7(2M >(2)@C5 =CA(A >H>(1CA K P0,00 A0) >H>(1CA J100,00 Standard SQL a o3s t&e use o% 1S 0@LL to c&ec? %or an em!ty or nu attr"$ute 'a ue, A nu entry can $e %ound us"ng t&e command se/uence: SALAC5 >HC2)A, >H)ASC(1>5120 7(2M >(2)@C5 =CA(A >HM10 1S 0@LL SQL a o3s you to use t&e !ercent s"gn (S# and underscore (H# 3" dcard c&aracters to ma?e matc&es 3&en t&e ent"re str"ng "s not ?no3:

S means any and a %o o3"ng c&aracters are e "g"$ e, 7or e4am! e :JS: "nc udes Jo&nson, Jones, Jern"gan, Ju y, and J-231Q :JoS: "nc udes Jo&nson and Jones

Hmeans any one c&aracter may $e su$st"tuted %or t&e under "ne, 7or e4am! e, :H23-4P<-<988: "nc udes 123-4P<-<988, 223-4P<-<988, and 323-4P<-<988 :H23-HP<-<98H: "nc udes 123-1P<-<981, 123-2P<-<982, and 823-8P<-<988 :HoHes: "nc udes Jones, Cones, Co?es, totes, and ro es =" dcard c&aracters may $e used "n com$"nat"ons, 7or e4am! e, t&e 3" dcard searc& $ased on t&e str"ng :H S: can y"e d t&e str"ng A , A ton, A g"n, * a?eston, $ an?, $ oated, and e "g"$ e, 5&e cond"t"ona L1QA must $e used to con+unct"on 3"t& 3" dcard c&aracters, 7or e4am! e t&e %o o3"ng /uery 3ou d %"nd a t&e DA0)2( ro3s %or contacts 3&ose ast names $eg"n 3"t& Sm"t&: SALAC5 DH0AMA, DHC205AC5, DHA(AAC2)A, DH>C20A 7(2M DA0)2( =CA(A DHC205AC5 L1QA :Sm"t&S:G 1n t&e e4am! e $e o3, 3ou d t&"s /uery return any 'a uesT SALAC5 DH0AMA, DHC205AC5, DHA(AAC2)A, DH>C20A 7(2M DA0)2( =CA(A DHC205AC5 L1QA :SM15CS:G AR1S5 can $e used 3&ene'er t&ere "s a re/u"rement to e4ecute a command $ased on attr"$ute 'a ues t&at are not nu , SALAC5E 7(2M >(2)@C5 =CA(A DHC2)A AR1S5G 2rder"ng A L"st 5&e 2()A( *I c ause "s es!ec"a y use%u "% t&e "st"ng se/uence "s "m!ortant to you, 5&e synta4: 2()A( *I Jattr"$uteK 5&"s command 3" !roduce a "st "n ascend"ng order, 5o !roduce a "st "n descend"ng order, use 2()A( *I Jattr"$uteK )ASC

7or e4am! e, "% 3anted t&e contents o% t&e C@S52MA( ta$ e "sted $y Customer Last 0ame (C@SHL0AMA# "n ascend"ng order, use: SALAC5 C@SHC2)A, C@SHL0AMA, C@SH70AMA, C@SH10151AL, C@SHA(AAC2)A, C@SH>C20A 7(2M C@S52MA( =CA(A 2()A( *I C@SHL0AMA 5&e %o o3"ng resu ts are returned:

10

Вам также может понравиться