0% found this document useful (0 votes)
52 views

SQL: The Query Language: CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5

DBMS SQL

Uploaded by

Prateek Solanki
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

SQL: The Query Language: CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5

DBMS SQL

Uploaded by

Prateek Solanki
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 58

SQL:

The Query Language

CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5

i e is !ust a "#$% # -(n#n

&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<

Re%ati#na% Query Languages


, ( *a!#r strength # the re%ati#na% *#)e%: supp#rts si*p%e, p#$er u% &uerying # )ata' , T$# su"%anguages: , >>L / >ata >e n Language / )e ine an) *#)i y sche*a 0at a%% : %e+e%s2 , >-L / >ata -anipu%ati#n Language / Queries can "e $ritten intuiti+e%y' , The >4-S is resp#nsi"%e #r e icient e+a%uati#n' / The 6ey: precise se*antics #r re%ati#na% &ueries' / (%%#$s the #pti*i?er t# e8tensi+e%y re-#r)er #perati#ns, an) sti%% ensure

The SQL Query Language


, The *#st $i)e%y use) re%ati#na% &uery %anguage' , @rigina%%y .4-, then (AS. in 1B86 , Current stan)ar) is SQL-200:
, .ntr#)uce) C-L eatures, $in)#$ uncti#ns, se&uences, aut#-generate) .>s' , A#t u%%y supp#rte) yet

, SQL-1BBB .ntr#)uce) 5@"!ectRe%ati#na%7 c#ncepts' (%s# n#t


supp#re) yet'

u%%y

SQLB2 is a "asic su"set


, -#st syste*s supp#rt a *e)iu*

The SQL >-L


, Sing%e-ta"%e &ueries are straight #r$ar)' , T# in) a%% 18 year #%) stu)ents, $e can $rite: SELECT F GR@- Stu)ents S =9ERE S'ageH18
QuickTime and a TIFF (LZW) decompressor are needed to see this picture.

in) !ust na*es an) %#gins, rep%ace the SELECT S'na*e, S'%#gin

irst

Querying -u%tip%e Re%ati#ns , Can speci y a !#in #+er

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

S'na*e E'ci) K#nes 9ist#ry105

A#te: #"+i#us%y n# re erentia% integrity c#nstraints ha+e "een use)

SELECT

Q>.ST.ACTR

4asic SQL Query GR@-

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'

SELECT Cr#ss Dr#)uct

S'na*e, E'ci) GR@- Stu)ents S, Enr#%%e) E =9ERE S'si)HE'si) (A> E'gra)eHI

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.

Step 22 >iscar) tup%es that ai% pre)icate

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

Step :2 >iscar) Sn$ante) C#%u*ns

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

Reser+es sid bid

A#$ the >etai%s


Sai%#rs

22 95

day 101 10/10/96 103 11/12/96

=e $i%% use these instances # re%ati#ns in 4#ats #ur e8a*p%es'

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

bid 101 102 103 104

E8a*p%e Sche*as 0in SQL >>L2


CREATE TABLE Sailors (sid INTEGER, sname CHAR(20),rating INTEGER, age REAL, PRI AR! "E! sid) CREATE TABLE Boats (#id INTEGER, #name CHAR (20), $olor CHAR(%0) PRI AR! "E! #id) CREATE TABLE Reser&es (sid INTEGER, #id INTEGER, da' (ATE, PRI AR! "E! (sid, #id, date), )*REIGN "E! sid RE)ERENCES Sailors, )*REIGN "E! #id RE)ERENCES Boats)

(n#ther K#in Query


sna*e Sai%#rs, Reser+es
SELECT GR@=9ERE

(sid) snam e rating age (sid) bid day (A>


"i)H10: 22 dustin

Sai%#rs'si)HReser+es'si)

!".# 22 $#$ $#%$# % &' !".# "( $#) $$%$2 % &' ""." 22 $#$ $#%$# % &' ""." "( $#) $$%$2 % &' ')." 22 $#$ $#%$# % &' ')." &" $#) $$%$2 % &'

22 dustin )$ *ubber )$ *ubber &" +ob &" +ob ( ( ) )

, Can ass#ciate 5range +aria"%es7 $ith the ta"%es in the GR@- c%ause'
/ sa+es $riting, *a6es &ueries easier t# un)erstan)

S#*e A#tes #n Range Taria"%es

, Aee)e) $hen a*"iguity c#u%) arise'


/ #r e8a*p%e, i sa*e ta"%e use) *u%tip%e ti*es in sa*e GR@- 0ca%%e) a 5se% !#in72 SELECT sna*e GR@- Sai%#rs,Reser+es =9ERE Sai%#rs'si)HReser+es'si) (A> "i)H10: Can "e SELECT S'sna*e re$ritten using GR@- Sai%#rs S, Reser+es R range +aria"%es as: =9ERE S'si)HR'si) (A> "i)H10:

-#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

Gin) sai%#rs $h#3+e reser+e) at %east #ne "#at


SELECT S'si) GR@- Sai%#rs S, Reser+es R =9ERE S'si)HR'si) , =#u%) a))ing >.ST.ACT t# this &uery *a6e a )i erenceU , =hat is the e ect # rep%acing S'si) "y S'sna*e in the SELECT c%auseU
/ =#u%) a))ing >.ST.ACT t# this +ariant # the &uery *a6e a )i erenceU

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'

Gin) si)3s # sai%#rs $h#3+e reser+e) a re) #r a green "#at

Ts'

GR@- 4#ats 4,Reser+es R =9ERE R'"i)H4'"i) (A> 04'c#%#rHIre)3@R 4'c#%#rHIgreen32

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

Gin) si)3s # sai%#rs $h#3+e reser+e) a re) an) a green "#at

, . $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 #

sai%#rs $h#3+e reser+e) "#at [10::

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

-#re #n Set-C#*paris#n @perat#rs


, =e3+e a%rea)y seen .A, EC.STS an) SA.QSE ' Can a%s# use A@T .A, A@T EC.STS an) A@T SA.QSE ' , (%s# a+ai%a"%e: #p (A] , #p (LL , Gin) sai%#rs $h#se rating is greater than that # s#*e sai%#r ca%%e) 9#rati#:

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

Re$riting .ATERSECT Queries Ssing .A

>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)

that )#esn3t a Reser+es ha+e '''tup%e sh#$ing S reser+e) 4

4asic SQL Queries - Su**ary


, (n a)+antage # the re%ati#na% *#)e% is its $e%%-)e ine) &uery se*antics' , SQL pr#+i)es uncti#na%ity c%#se t# that # the "asic re%ati#na% *#)e%' / s#*e )i erences in )up%icate han)%ing, nu%% +a%ues, set #perat#rs, etc' , Typica%%y, *any $ays t# $rite a &uery / the syste* is resp#nsi"%e #r iguring a ast $ay t# actua%%y e8ecute a &uery regar)%ess # h#$ it is $ritten' , L#ts *#re uncti#na%ity "ey#n) these "asic eatures'

(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

(ggregate @perat#rs 0c#ntinue)2

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'sna*e Sai%#rs S S'ratingH 0SELECT

-(C0S2'rating2 GR@- Sai%#rs S2

Gin) na*e an) age # sai%#r0s2


, The irst &uery is inc#rrect1

the #%)est

SELECT S'sna*e, -(C GR@- Sai%#rs S

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

SELECT S'sna*e, GR@- Sai%#rs S =9ERE S'age H

S'age

GR@SD 4] an) 9(T.AG


, S# ar, $e3+e app%ie) aggregate #perat#rs t# a%% 0&ua%i ying2 tup%es' / S#*eti*es, $e $ant t# app%y the* t# each # se+era% gr#ups # tup%es' , C#nsi)er: Gin) the age # the y#ungest sai%#r #r each rating %e+e%' / .n genera%, $e )#n3t 6n#$ h#$ *any rating %e+e%s e8ist, an) $hat the rating +a%ues #r these %e+e%s are1 / Supp#se $e 6n#$ that rating +a%ues g# r#* 1 t# 10\ $e can $rite 10 &ueries that %##6 %i6e this 012: -.A 0S'age2 SELECT G#r i H 1, 2, ''' , 10: GR@- Sai%#rs S =9ERE S'rating H i

Queries =ith GR@SD 4]


, T# generate +a%ues #r a c#%u*n "ase) #n gr#ups # r#$s, use aggregate uncti#ns in SELECT state*ents $ith the GR@SD 4] c%ause SELECT Q>.ST.ACTR target-%ist GR@re%ati#n-%ist Q=9ERE &ua%i icati#nR GR@SD 4] gr#uping-%ist The target-%ist c#ntains 0i2 %ist # c#%u*n na*es & 0ii2 ter*s $ith aggregate #perati#ns 0e'g', -.A 0 S'age 22'

Gr#up 4y E8a*p%es in) the a+erage age #


(TG

each rating,

the sai

SELECT S'rating, GR@- Sai%#rs S GR@SD 4] S'rating

0S'age2

r each rating in) the age # i%#r $ith age 18


SELECT S'rating, -.A GR@- Sai%#rs S =9ERE S'age NH 18 GR@SD 4] S'rating

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

(ns$er Ta"%e :' Der #r* (ggregati#n

dustin lubber zorba horatio brutus rusty

7 8 10 7 1 10

45.0 55.5 16.0 35.0 33.0 35.0

rating 1 7 7 8 10

age 33.0 45.0 35.0 55.5 35.0

rating 1 7 8 10

age 33.0 35.0 55.0 35.0

1' G#r* cr#ss pr#)uct 2' >e%ete unnee)e) c#%u*ns, r#$s\ #r* gr#ups

Gin) the nu*"er # reser+ati#ns #r each re) "#at'


SELECT 4'"i), C@SAT0F2(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) , Gr#uping #+er a !#in # re%ati#ns' t$#

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

Queries =ith GR@SD 4] an) 9(T.AG


SELECT Q>.ST.ACTR target-%ist GR@re%ati#n-%ist =9ERE &ua%i icati#n GR@SD 4] gr#uping-%ist 9(T.AG gr#up&ua%i icati#n

, 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 #

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# "#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 #

sai%#rs $h#3+e reser+e) a%% "#a

, Can y#u )# this using Gr#up 4y an) 9a+ingU

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

s+name (,stin L,##er Bo# (,stin L,##er Bo#

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+name (,stin Bo#

s+sid 22 /0

#$o,nt % %

C#unt 0F2 H <

r#* "#ats

s+name

s+sid

(pp%y ha+ing c%ause t# gr#ups

.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

>ELETE & SD>(TE


>ELETE Q=9ERE QGR@-R

ta"%eWna*e &ua%i icati#nR


>ELETE GR@- 4#ats =9ERE c#%#r H Ire)3 >ELETE GR@- 4#ats " =9ERE "' "i) H 0SELECT r'"i) GR@- Reser+es R =9ERE 222

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

E8p%icit !#in se*antics nee)e) un%ess it is an .AAER !#in 0.AAER is )e au%t2

.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

sname rating age Dustin 7 45.0 Lubber 8 55.5 Bob 3 63.5

sid bid 22 95

day

101 10/10/96 103 11/12/96

s+sid s+name r+#id 22 (,stin %0% /0 Bo# %0-

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

sname rating age Dustin 7 45.0 Lubber 8 55.5 Bob 3 63.5

sid bid day 22 101 10/10/96 95 103 11/12/96

s+sid 22 /0 -%

s+name r+#id (,stin %0% Bo# %0L,##er

Right @uter K#in


Right @uter K#in returns a%% *atche) r#$s, p%us a%% un*atche) r#$s r#* the ta"%e #n the right # the !#in c%ause SELECT r'si), "'"i), "'na*e GR@- Reser+es r R.G9T @STER [email protected] 4#ats " @A r'"i) H "'"i) Returns a%% "#ats & in #r*ati#n #n $hich #nes are reser+e)'

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

101 10/10/96 103 11/12/96

bid 101 102 103 104

bname nterla!e nterla!e "li##er $arine

color blue red green red

r+sid 22 /0

#+#id %0% %02 %0%0.

#+name Interla1e Interla1e Cli22er arine

Gu%% @uter K#in


Gu%% @uter K#in returns a%% 0*atche) #r un*atche)2 r#$s r#* the ta"%es #n "#th si)es # the !#in c%ause SELECT r'si), "'"i), "'na*e GR@- Reser+es r GSLL @STER [email protected] 4#ats " @A r'"i) H "'"i) Returns a%% "#ats & a%% in #r*ati#n #n reser+ati#ns

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

101 10/10/96 103 11/12/96

101 102 103 104

nterla!e nterla!e "li##er $arine

blue red green red

r+sid 22 /0

#+#id %0% %02 %0%0.

#+name Interla1e Interla1e Cli22er arine

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

You might also like