SQL: The Query Language: CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5
SQL: The Query Language: CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5
&ueries'
()*inistri+ia
, -i)ter*1 $as a "it easier than . $ante) it t# "e' / -ean $as 80 / Three pe#p%e g#t 100012 / .3* actua%%y &uite p%ease)' / 4ut, . )# p%an t# 56ic6 it up a n#tch7 #r the uture e8a*s' , 4e sure t# register y#ur na*e $ith y#ur cs186 %#gin i y#u ha+en3t a%rea)y --- e%se, y#u ris6 n#t getting gra)es' , 9#*e$#r6 2 is "eing re%ease) t#)ay' / T#)ay an) Tues)ay3s %ectures pr#+i)e "ac6gr#un)' / 9$ 2 is )ue Tues)ay :;1<
u%%y
in) !ust na*es an) %#gins, rep%ace the SELECT S'na*e, S'%#gin
irst
t$#
ta"%es as #%%#$s: SELECT S'na*e, E'ci) GR@- Stu)ents S, Enr#%%e) E =9ERE S'si)HE'si) (A> E'gra)eHI4J
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
resu%t H
SELECT
Q>.ST.ACTR
target-%ist %ist
re%ati#n-
, re%ati#n-%ist : ( %ist # re%ati#n na*es =9ERE &ua%i icati#n a ter / p#ssi"%y $ith a range-+aria"%e each na*e , target-%ist : ( %ist # attri"utes # ta"%es in re%ati#n-%ist , &ua%i icati#n : C#*paris#ns c#*"ine) using (A>, @R an) A@T' / C#*paris#ns are (ttr #p c#nst #r (ttr1 #p (ttr2, $here #p is #ne # HLMNOP
, >.ST.ACT: #pti#na% 6ey$#r) in)icating that the ans$er sh#u%) n#t c#ntain )up%icates' / .n SQL SELECT, the )e au%t is that )up%icates are n#t e%i*inate)1 0Resu%t is
Query Se*antics
, Se*antics # an SQL &uery are )e ine) in ter*s # the #%%#$ing c#nceptua% e+a%uati#n strategy: 1' )# GR@- c%ause: c#*pute cr#sspr#)uct # ta"%es 0e'g', Stu)ents an) Enr#%%e)2' 2' )# =9ERE c%ause: Chec6 c#n)iti#ns, )iscar) tup%es that ai%' 0i'e', 5se%ecti#n72' :' )# SELECT c%ause: >e%ete un$ante) ie%)s' 0i'e', 5pr#!ecti#n72' <' . >.ST.ACT speci ie), e%i*inate )up%icate r#$s'
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
SELECT S'na*e, E'ci) GR@- Stu)ents S, Enr#%%e) E =9ERE S'si)HE'si) (A> E'gra)eHI4J
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
SELECT S'na*e, E'ci) GR@- Stu)ents S, Enr#%%e) E =9ERE S'si)HE'si) (A> E'gra)eHI4J
22 95
sid 22 31 95
sname rating age Dustin 7 45.0 Lubber 8 55.5 Bob 3 63.5 bname Interlake Interlake Clipper Marine color blue red green red
Sai%#rs'si)HReser+es'si)
!".# 22 $#$ $#%$# % &' !".# "( $#) $$%$2 % &' ""." 22 $#$ $#%$# % &' ""." "( $#) $$%$2 % &' ')." 22 $#$ $#%$# % &' ')." &" $#) $$%$2 % &'
, Can ass#ciate 5range +aria"%es7 $ith the ta"%es in the GR@- c%ause'
/ sa+es $riting, *a6es &ueries easier t# un)erstan)
-#re A#tes
, 9ere3s an e8a*p%e $here range +aria"%es are re&uire) 0se% -!#in e8a*p%e2: ELECT 8'sna*e, 8'age, y'sna*e, y'age R@- Sai%#rs 8, Sai%#rs y 9ERE 8'age N y'age , A#te that target %ist can "e rep%ace) "y 5F7 i y#u )#n3t $ant SELECT F t# )# a pr#!ecti#n: GR@- Sai%#rs 8 =9ERE 8'age N 20
E8pressi#ns
, Can use arith*etic e8pressi#ns in SELECT c%ause 0p%us #ther #perati#ns $e3%% )iscuss %ater2 , Sse (S t# pr#+i)e c#%u*n SELECT S'age, S'age-5 (S age1, na*es 2FS'age (S age2
GR@- Sai%#rs S =9ERE S'sna*e H I)ustin3
, Can a%s# ha+e e8pressi#ns in =9ERE c%ause: SELECT S1'sna*e (S na*e1, S2'sna*e (S na*e2
GR@- Sai%#rs S1, Sai%#rs S2 =9ERE 2FS1'rating H S2'rating - 1
String #perati#ns
,SQL a%s# supp#rts s#*e string #perati#ns ,5L.YE7 is use) #r string *atching'
SELECT S'age, age1HS'age-5, 2FS'age (S age2 GR@- Sai%#rs S =9ERE S'sna*e L.YE I4WX43
VW3 stan)s #r any #ne character an) V X3 stan)s #r 0 #r *#re ar"itrary characters'
, SA.@A: Can "e use) t# c#*pute the uni#n # any t$# uni#n-c#*pati"%e sets # tup%es 0$hich are the*se%+es the SELECT resu%t>.ST.ACT # SQL R'si) &ueries2'
Ts'
0n#te: SELECT R'si) SA.@A GR@- 4#ats 4, Reser+es R e%i*inates =9ERE R'"i)H4'"i) (A> 4'c#%#rHIre)3 )up%icates SA.@A SELECT R'si) "y )e au%t' GR@- 4#ats 4, Reser+es R @+erri)e $; =9ERE R'"i)H4'"i) (A> SA.@A (LL2 4'c#%#rHIgreen3
, . $e si*p%y rep%ace @R "y (A> in the pre+i#us &uery, $e get the $r#ng ans$er' 0=hyU2 , .nstea), c#u%) use a se% -!#in:
SELECT R1'si) SELECT R'si) GR@4#ats 41, Reser+es R1, GR@- 4#ats 4#ats 4,Reser+es R 42, Reser+es R2 =9ERE R'"i)H4'"i) (A> =9ERE R1'si)HR2'si) 04'c#%#rHIre)3 (A> 4'c#%#rHIgreen32 (A> R1'"i)H41'"i) (A> R2'"i)H42'"i) (A> 041'c#%#rHIre)3 (A> 42'c#%#rHIgreen32
(A> C#ntinue)Z
, .ATERSECT :)iscusse)
Yey
ie%)1
in "##6' Can "e use) t# c#*pute the intersecti#n # any t$# uni#nc#*pati"%e sets # tup%es' , (%s# in te8t:
ECCEDT 0s#*eti*es ca%%e) -.ASS2
, .nc%u)e) in the SQL;B2 stan)ar), "ut *any syste*s )#n3t supp#rt the*'
SELECT S'si) GR@- Sai%#rs S, 4#ats 4, Reser+es R =9ERE S'si)HR'si) (A> R'"i)H4'"i) (A> 4'c#%#rHIre)3 .ATERSECT SELECT S'si) GR@- Sai%#rs S, 4#ats 4, Reser+es R =9ERE S'si)HR'si) (A> R'"i)H4'"i) (A> 4'c#%#rHIgreen3
Aeste) Queries
, D#$er u% eature # SQL: =9ERE c%ause can itse% c#ntain an SQL &uery1 / (ctua%%y, s# can GR@- an) 9(T.AG c%auses'
*es #
SELECT S'sna*e GR@- Sai%#rs S =9ERE S'si) .A 0SELECT R'si) GR@- Reser+es R =9ERE R'"i)H10:2 , T# in) sai%#rs $h#3+e n#t reser+e) [10:, use A@T .A ' , T# un)erstan) se*antics # neste) &ueries: / thin6 # a neste) %##ps e+a%uati#n: G#r
Aeste) Queries $ith C#rre%ati#n Gin) na*es # sai%#rs $h#3+e reser+e) "#at [
SELECT S'sna*e GR@- Sai%#rs S =9ERE EC.STS 0SELECT F GR@- Reser+es R =9ERE R'"i)H10: (A> S'si)HR'si)2 is an#ther set c#*paris#n #perat#r, %i6e .A ' , Can a%s# speci y A@T EC.STS , . SA.QSE is use), an) F is rep%ace) "y R'"i) , in)s sai%#rs $ith at *#st #ne reser+ati#n #r "#at [10:' / SA.QSE chec6s #r )up%icate tup%es in a su"&uery\
, EC.STS
SELECT F GR@- Sai%#rs S =9ERE S'rating N (A] 0SELECT S2'rating GR@- Sai%#rs S2 =9ERE S2'sna*eHI9#rati#32
Gin) si)3s # sai%#rs $h#3+e reser+e) "#th a re) an) a green "#at: SELECT R'si) GR@- 4#ats 4, Reser+es R =9ERE R'"i)H4'"i) (A> 4'c#%#rHIre)3 (A> R'si) .A 0SELECT R2'si) GR@- 4#ats 42, Reser+es R2 =9ERE R2'"i)H42'"i) (A> 42'c#%#rHIgreen32
, Si*i%ar%y, ECCEDT &ueries re-$ritten using A@T .A ' , 9#$ $#u%) y#u change this t# in) na*es 0n#t si) 3s2 # Sai%#rs $h#3+e reser+e) "#th re) an) green "#atsU
>i+isi#n in SQL
sai%#rs $h#3+e reser+e) a%% "#ats'
, E8a*p%e in "##6, n#t using ECCEDT : Sai%#rs S such that ''' 4'"i) there is n# GR@- 4#ats 4 "#at 4 =9ERE A@T EC.STS
na*es #
0SELECT
R'"i)
(ggregate @perat#rs
, Signi icant e8tensi#n # re%ati#na% a%ge"ra'
SELECT C@SAT 0F2 GR@- Sai%#rs S SELECT (TG 0S'age2 GR@- Sai%#rs S =9ERE S'ratingH10 SELECT C@SAT 0>.ST.ACT GR@- Sai%#rs S =9ERE S'sna*eHI4#"3
C@SAT 0F2 C@SAT 0 Q>.ST.ACTR (2 SS- 0 Q>.ST.ACTR (2 (TG 0 Q>.ST.ACTR (2 -(C 0(2 -.A 0(2
sing%e c#%u*n
S'rating2
C@SAT 0F2 C@SAT 0 Q>.ST.ACTR (2 SS- 0 Q>.ST.ACTR (2 (TG 0 Q>.ST.ACTR (2 -(C 0(2 -.A 0(2
sing%e c#%u*n
the #%)est
0S'age
, Thir) &uery e&ui+a%ent t# 0SELECT -(C 0S2' sec#n) &uery GR@- Sai%#rs S / a%%#$e) in SQL;B2 stan)ar), "ut n#t SELECT S'sna*e, S'age supp#rte) in s#*e GR@- Sai%#rs S syste*s' =9ERE 0SELECT -(C 0S2'age2 GR@- Sai%#rs H S'age
S'age
each rating,
the sai
0S'age2
the y#ungest
0S'age2
C#nceptua% E+a%uati#n
, The cr#ss-pr#)uct # re%ati#n-%ist is c#*pute), tup%es that ai% &ua%i icati#n are )iscar)e), V unnecessary3 ie%)s are )e%ete), an) the re*aining tup%es are partiti#ne) int# gr#ups "y the +a%ue # attri"utes in gr#uping-%ist ' , @ne ans$er tup%e is generate) per &ua%i ying gr#up'
sid 22 31 71 64 29 58
S'rating, -.A 0S'age2 GR@- Sai%#rs S =9ERE S'age NH 18 GR@SD 4] S'rating sname rating age
SELECT
7 8 10 7 1 10
rating 1 7 7 8 10
rating 1 7 8 10
1' G#r* cr#ss pr#)uct 2' >e%ete unnee)e) c#%u*ns, r#$s\ #r* gr#ups
SELECT 4'"i), C@SAT 0F2 (S sc#unt GR@- 4#ats 4, Reser+es R =9ERE R'"i)H4'"i) (A> 4'c#%#rHIre)3 GR@SD 4] 4'"i)
#+#id %0% %02 %0%0. %0% %02 %0%0. #+$olor #l,e red green red #l,e red green red r+#id %0% %0% %0% %0% %02 %02 %02 %02 #+#id #+$olor r+#id
%02 red
%02
2
#+#id %02 s$o,nt %
ans$er
, Sse the 9(T.AG c%ause $ith the GR@SD 4] c%ause t# restrict $hich gr#up-r#$s are returne) in the resu%t set
C#nceptua% E+a%uati#n
, G#r* gr#ups as "e #re' , The gr#up-&ua%i icati#n is then app%ie) t# e%i*inate s#*e gr#ups' / E8pressi#ns in gr#up-&ua%i icati#n *ust ha+e a sing%e +a%ue per gr#up1 / That is, attri"utes in gr#up&ua%i icati#n *ust "e argu*ents # an aggregate #p #r *ust a%s# appear in the gr#uping-%ist' 0SQL )#es n#t e8p%#it pri*ary 6ey se*antics here12 , @ne ans$er tup%e is generate) per
Gin) the age # the y#ungest sai%#r $ith age 18, #r each rating $ith at %east 2 such sai%#rs sid sname rating age LECT S'rating, -.A 0S'age2 22 dustin 7 45.0 @- Sai%#rs S 31 lubber 8 55.5 ERE S'age NH 18 71 zorba 10 16.0 @SD 4] S'rating 64 horatio 7 35.0 T.AG C@SAT 0F2 N 1 29 brutus 1 33.0 58 rusty 10 35.0 rating age 1 33.0 rating m-age count 7 45.0 1 33.0 1 7 35.0 7 35.0 2 8 55.5 8 55.0 1 10 35.0 10 35.0 1
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.
(ns$er re%ati#
) na*es #
there is n# "#at 4 GR@- 4#ats 4 $ith#ut ''' =9ERE A@T EC.STS 0SELECT R'"i) a Reser+es tup%e sh#$ing S reser+e) 4
in) na*es #
S'na*e Sai%#rs S, reser+es R S'si) H R'si) GR@SD 4] S'na*e, S'si) 9(T.AG C@SAT0>.ST.ACT R'"i)2 H 0 Se%ect C@SAT 0F2 GR
te: *ust ha+e "#th si) an) na*e in the GR@SD ause' =hyU
SELECT S'na*e, S'si) GR@- Sai%#rs S, reser+es R =9ERE S'si) H r'si) GR@SD 4] S'na*e, S'si) 9(T.AG C@SAT0>.ST.ACT R'"i)2 H Se%ect C@SAT 0F2 GR 4#ats s+sid r+sid r+#id 22 22 %0% -% 22 %0% QuickTime and a TIFF (LZW) decompressor /0 22 %0% are needed to see this picture. 22 /0 %02 -% /0 %02 /0 /0 %02
s+sid 22 /0
#$o,nt % %
r#* "#ats
s+name
s+sid
.ASERT
.ASERT Q.AT@R ta"%eWna*e Q0c#%u*nW%ist2R T(LSES 0 +a%ueW%ist2 .ASERT Q.AT@R ta"%eWna*e .ASERT 4#ats T(LSES Q0.AT@ c#%u*nW%ist 2R 0 105, IC%ipper3, Ipurp%e32 .ASERT .AT@ 4#ats 0"i), c#%#r2 T(LSES 0BB, Iye%%#$32 M se%ect state*entN
]#u can a%s# )# a 5"u%6 insert7 # +a%ues r#* #ne ta"%e int# an#ther: .ASERT .AT@ TE-D0"i)2 SELECT r'"i) GR@- Reser+es R =9ERE r'si) H 22\ 0*ust "e type c#*pati"%e2
r'si) H
Can a%s# *#)i y tup%es using SD>(TE state*ent' SD>(TE 4#ats SET C#%#r H 5green7 =9ERE "i) H 10:\
Au%% Ta%ues
, Gie%) +a%ues in a tup%e are s#*eti*es un6n#$n 0e'g', a rating has n#t "een assigne)2 #r inapp%ica"%e 0e'g', n# sp#use3s na*e2' / SQL pr#+i)es a specia% +a%ue nu%% #r such situati#ns' , The presence # nu%% c#*p%icates *any issues' E'g': / Specia% #perat#rs nee)e) t# chec6 i +a%ue is;is n#t nu%%' / .s ratingN8 true #r a%se $hen rating is e&ua% t# nu%%U =hat a"#ut (A>, @R an) A@T c#nnecti+esU / =e nee) a :-+a%ue) %#gic 0true, a%se an) un6n#$n2'
K#ins
SELECT 0c#%u*nW%ist2 GR@- ta"%eWna*e Q.AAER ^ _LEGT ^R.G9T ^ GSLL ` @STERR [email protected] ta"%eWna*e @A &ua%i icati#nW%ist =9ERE Z
.nner K#in
@n%y the r#$s that *atch the search c#n)iti#ns are returne)' SELECT s'si), s'na*e, r'"i) GR@- Sai%#rs s .AAER [email protected] Reser+es r @A s'si) H r'si) Returns #n%y th#se sai%#rs $h# ha+e reser+e) "#ats SQL-B2 a%s# a%%#$s: SELECT s'si), s'na*e, r'"i) GR@- Sai%#rs s A(TSR(L [email protected] Reser+es r 5A(TSR(L7 *eans e&ui-!#in #r each pair # attri"utes $ith the sa*e na*e 0*ay nee) t# rena*e $ith 5(S72
SELECT s'si), s'na*e, r'"i) GR@- Sai%#rs s .AAER [email protected] Reser+es r @A s'si) H r'si)
sid 22 31 95
sid bid 22 95
day
Le t @uter K#in
Le t @uter K#in returns a%% *atche) r#$s, p%us a%% un*atche) r#$s r#* the ta"%e #n the %e t # the !#in c%ause 0use nu%%s in ie%)s # n#n-*atching tup%es2 SELECT s'si), s'na*e, r'"i) GR@- Sai%#rs s LEGT @STER [email protected] Reser+es r @A s'si) H r'si) Returns a%% sai%#rs & in #r*ati#n #n $hether they ha+e reser+e) "#ats
SELECT s'si), s'na*e, r'"i) GR@- Sai%#rs s LEGT @STER [email protected] Reser+es r @A s'si) H r'si)
sid 22 31 95
s+sid 22 /0 -%
SELECT r'si), "'"i), "'na*e GR@- Reser+es r R.G9T @STER [email protected] 4#ats " @A r'"i) H "'"i)
sid bid 22 95
day
r+sid 22 /0
sid bid 22 95
SELECT r'si), "'"i), "'na*e GR@- Reser+es r GSLL @STER [email protected] 4#ats " @A r'"i) H "'"i) bid bname color
day
r+sid 22 /0
e: in this case it is the sa*e as the R@K "ecause is a #reign 6ey in reser+es, s# a%% reser+ati#ns e a c#rresp#n)ing tup%e in "#ats'
Tie$s
CRE(TE T.E= +ie$Wna*e (S se%ectWstate*ent
-a6es )e+e%#p*ent si*p%er @ ten use) #r security A#t instantiate) - *a6es up)ates tric6y
CRE(TE T.E= Re)s (S SELECT 4'"i), C@SAT 0F2 GR@- 4#ats 4, Reser+es =9ERE R'"i)H4'"i) (A>
(S sc#unt R
4'c#%#rHIre)3
GR@SD 4]
4'"i)
CRE(TE T.E= Re)s (S SELECT 4'"i), C@SAT 0F2 GR@- 4#ats 4, Reser+es =9ERE R'"i)H4'"i) (A> GR@SD 4] 4'"i)
(S sc#unt R 4'c#%#rHIre)3
#+#id %02
s$o,nt %
Re)s