Fundamental and Application of Database ? o?¨$ ? o?¨$ ??? ??? ? e/ ?D1???ù?? ? e/ ?D1???ù?? ? ¥t ? ¥t 3.5.2 SQL?¥ ?¤°? SQL? ?¤°?¥?1 ?? – <l2 5: – ?′ ?¤ (c1 ? ?¤ ) – d?′ ?¤°? – 1 & ?¤°? – ? ?¤°? – ˉ?Hq ?¤°? Ba<l2 5: z?{ ?¤ìM¥ ?¤ z ?  P¨ è SELECT Student.* , SC.* FROM Student, SC =a?′Dd?′ ?¤°? ?′ ?¤a1 ? ?¤ad?′ ?¤ [ è] °? ??D 3# ê? [?¥ f ?b SELECT Student.*SC.* FROM StudentSC WHERE Student.Sno = SC.Sno ?′ ?¤ z ?¤ ??1 = ¥ ?¤?T – [<V 1>.]<  1> = [<V 2>.]<  2> – ??0 ??¨V 1?V 2? ] ?? H?A?F V -b?¨·B ?? H V[F9 V[ 8 {V -b ?′ ?¤ L ! StudentVa SCVsYμ/  ?  StudentV Sno Sname Ssex Sage Sdept 95001 ù§ 3 20 CS 95002 >? o 19 IS 95003 | ù o 18 MA 95004 f ? 3 19 IS ?′ ?¤ SCV Sno Cno Grade 95001 1 92 95001 2 85 95001 95002 95002 3 2 3 88 90 80 ?′ ?¤ 2TV Student.Sno Sname Ssex Sage Sdept SC.Sno Cno Grade 95001 ù§ 3 20 CS 95001 1 92 95001 ù§ 3 20 CS 95001 2 85 95001 ù§ 3 20 CS 95001 3 88 95002 >? o 19 IS 95002 2 90 95002 >? o 19 IS 95002 3 80 1 ? ?¤ z?′ ?¤¥B?+ y f ?ü "S ? ×ˉ¥ ??  ??b [ è]   è¨1 ? ?¤??b SELECT Student.SnoSnameSsexSage SdeptCnoGrade FROM StudentSC WHERE Student.Sno = SC.Sno d?′ ?¤°? ?¤ ??? ^ = ¥ ?¤?T [<V 1>.]<  1><1? ?? >[<V 2>.]<  2> 1? ?? >a<a>=a<=a!= [<V 1>.]<  1> BETWEEN [<V 2>.]<  2> AND [<V 2>.]<  3> ?a1 & ?¤ z B?VD 1-é? ?¤?1V¥ 1 & ?¤ z 31óV Y [ U uY z ?? ?μ ?? ? ^] ??yNA? P¨ Y - 1 & ?¤ ? [ è ] °? ?B ê [¥W¤5? [ '5? [¥5? [ SELECT FIRST.CnoSECOND.Cpno FROM Course FIRSTCourse SECOND WHERE FIRST.Cpno = SECOND.Cno 1 & ?¤ ? FIRSTV CourseV Cno Cname Cpno Ccredit 1 ? o 5 4 2 ?D 2 3 ?"d 1 4 4 ?T"d 6 3 5 ? 2 7 4 6 ? ) ? 2 7 PASCAL?y 6 4 1 & ?¤ ? SECONDV CourseV Cno Cname Cpno Ccredit 1 ? o 5 4 2 ?D 2 3 ?"d 1 4 4 ?T"d 6 3 5 ? 2 7 4 6 ? ) ? 2 7 PASCAL?y 6 4 1 & ?¤ ? °?2T 1 7 3 5 5 6 cpnocno 1a? ?¤ Outer Join z? ?¤D ?Y ?¤¥ uY – ?Y ?¤?To { ?@ ?¤Hq¥íF – ? ?¤?T[·?V1 ?¤?8|?8V ?? ?@ ?¤Hq¥íFBi { ? ?¤ ?  [ è ] °? ??D 3# ê? [?¥ f ? ? àμ ê? [?¥D 3 ----¨? ?¤?T SELECT Student.SnoSnameSsex SageSdeptCnoGrade FROM StudentSC WHERE Student.Sno = SC.Sno(*) ? ?¤ ?  2T Student.Sno Sname Ssex Sage Sdept Cno Grade 95001 ù§ 3 20 CS 1 92 95001 ù§ 3 20 CS 2 85 95001 ù§ 3 20 CS 3 88 95002 >? o 19 IS 2 90 95002 >? o 19 IS 3 80 95003 | ù o 18 MA 95004 f ? 3 19 IS ? ?¤ ?  –V a ?F? ?¤?T? (*) (+)·?d? 8V –d?8VμB “£ ? ”¥′??? ??? b ′F? –′? V[??8V? ?μ? ?@ ?¤Hq¥ íFé? ?¤ –??′?ò  ?? ^ b′yND′? ?¤ ¥2T? ?1d?8V¥ ??′ ?? ^ b ′ ? ?¤ ?  z P? ?¤ – ? ?¤?C ?¤Hq¥PH z ·? ?¤ – ? ?¤?C ?¤Hq¥·H ?aˉ?Hq ?¤ WHERE0 ?c? ?¤Hq H?1ˉ?Hq ?¤ [ è]°?ê? 2| [? O? 90s[ ¥ ?μD 3¥ D|a? SELECT Student.Sno, student.Sname FROM Student, SC WHERE Student.Sno = SC.Sno AND /* ?¤ìM */ SC.Cno= ' 2 ' AND /*  eK?Hq */ SC.Grade > 90 /*  eK?Hq */ V ?¤ [ è] °? ??D 3¥D|a? aê?¥ [ ? #? b SELECT Student.SnoSnameCnameGrade FROM StudentSCCourse WHERE Student.Sno = SC.Sno and SC.Cno = Course.Cno 2T Student.Sno Sname Cname Grade 95001 ù§ ? o 92 95001 ù§ ?D 85 95001 ù§ ?"d 88 95002 >? ?D 90 95002 >? ?"d 80 3.5.4 3*°? z 3*°?à ? z 3*°?s ? z 3*°? p3ZE z?0°?¥ìM 3*°? (? ) z 3*°?à ? – B? SELECT-FROM-WHERE? ?1B ?°? v – |B?°? v 3* 6B?°? v¥ WHERE0  HAVING ?¥Hq?¥° ??1 3*°? 3*°? (? ) SELECT Sname ?a°? /3°? FROM Student WHERE Sno IN  SELECT Sno =a°? /0°? FROM SC WHERE Cno= ' 2 ' 3*°? (? ) – 0°?¥K? z? ? P¨ ORDER BY0  – aa 3*Z TQ?  SQL?y¥2? – μt 3*°? V[¨ ?¤ ?9} 3*°?s ? z?M10°? 0°?¥°?Hq?G ??3°? zM10°? 0°?¥°?HqG ??3°? 3*°? p3ZE z?M10°? ^? ú_??a) ?b' ??0°? ^? ú_??a) ?b' ??0°? B)°?) ?- - p30°?¥2T B)°?) ?- - p30°?¥2T ¨?y ? 3°?¥°sHqb ¨?y ? 3°?¥°sHqb 3*°? p3ZE ? z M10°? – n5 |?a°??V¥?B?íF? ?D =a °?M1¥ ??′) ? =a°? ? WHERE0  Rí′1?5 |NíFb ?2TV – ?a |?aV¥/B?íF – ×ˉ?BV?°à?aV ??_°?1?b ?0°?¥ìM z {μ */ìM¥0°? z{μ1? ??¥0°? z{μ "/: "--ìM¥0°? z{μ &9*454ìM¥0°? Ba{μ INìM¥0°? [ è] °?D “ >? ”]B?"D¥D 3b N°?1 p V[s? ??? ? ??“ >? ” ?" SELECT Sdept FROM Student WHERE Sname= ' >? ' 2T1 Sdept IS {μ INìM¥0°? ? ?°s ?μ IS"D¥D 3b SELECT SnoSnameSdept FROM Student WHERE Sdept= ' IS ' 2T1 Sno Sname Sdept 95001 >? IS 95004 f ? IS / 3*°? |?B?°? 3 ???=?°?¥Hq? SELECT SnoSnameSdept FROM Student WHERE Sdept IN (SELECT Sdept FROM Student WHERE Sname= ‘ >? ’) N°?1?M10°?b DBMS p3?°? H9 ^s? ?S¥b {μ INìM¥0°? ? ¨1 & ?¤??'°?1 p SELECT S1.SnoS1.SnameS1.Sdept FROM Student S1Student S2 WHERE S1.Sdept = S2.Sdept AND S2.Sname = ' >? ' {μ INìM¥0°? ? 3°??0°??¥V ( V[?lY SELECT SnoSnameSdept FROM Student S1 WHERE S1.Sdept IN (SELECT Sdept FROM Student S2 WHERE S2.Sname= ‘ >? ’) {μ INìM¥0°? ? [ è55]°?ê?  [? 1 “5 ?D ”¥D 3D|?? SELECT SnoSname èKa Student1"? FROM Student | | Sno?? Sname WHERE Sno IN (SELECT Sno ? ?a ?a SC1"?sê FROM SC ?  1137| [?¥D 3D| WHERE Cno IN (SELECT Cno ?5 Course1"?s “5 ?D FROM Course ”¥ [?|2T1 1137| | WHERE Cname= ‘5 ?D ’)); {μ INìM¥0°? ? 2T 4OP 4OBNF  |  $Y {O {μ INìM¥0°? ? – ¨ ?¤°? SELECT SnoSname FROM StudentSCCourse WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno AND Course.Cname=‘5 ?D ’ =a{μ1? ??¥0°? °? ? ? M?? =a°?Rí?′ H V ¨1? ?? ><=>=<=!=  < >b zD SOME ALLìM ¥? P¨ {μ1? ??¥0°? ? èL !B?D 3o V ?B?"Di OA ? ??B?"5 V[ ¨ = }9 IN SELECT SnoSnameSdept FROM Student WHERE Sdept = SELECT Sdept FROM Student WHERE Sname= ' >? ' {μ1? ??¥0°? ? 0°?B?1?1??-a p ¥ è0 SELECT SnoSnameSdept FROM Student WHERE ( SELECT Sdept FROM Student WHERE Sname= ‘ >? ’ ) = Sdept ?a{μ SOME ALLìM¥0°? ìM?l – ANY SOME ?iB?′ – ALL ?μ′ {μ {μ ANY ALLìM¥0°? ? 31 ¥? P¨1? ?? > ANY v?0°?2T?¥ ?′ > ALL v?0°?2T?¥ ?μ′ < ANY l?0°?2T?¥ ?′ < ALL l?0°?2T?¥ ?μ′ >= ANY v???0°?2T?¥ ?′ >= ALL v???0°?2T?¥ ?μ′ <= ANY l???0°?2T?¥ ?′ <= ALL l???0°?2T?¥ ?μ′ = ANY ??0°?2T?¥ ?′ =ALL ??0°?2T?¥ ?μ′ Yè àμ L=il !=  <> ANY ???0°?2T?¥ ?′ !=  <> ALL ???0°?2T?¥ ??B?′ {μ {μ ANY ALLìM¥0°? ? [ è] °?  e"?15 ?" ?i B? ? ( ?  B ? ? )D 3 M -l¥D 3? ? M - SELECT SnameSage FROM Student WHERE Sage < ANY (SELECT Sage FROM Student WHERE Sdept= ‘5 ? ') AND Sdept " ‘5 ? ' ; /* ?i? ^3°? v?¥Hq */ {μ {μ ANY ALLìM¥0°? ? 2T Sname Sage ?o ?o 18 3?V? 1.DBMS3?N°? H n5) ?0°?s IS"? ?μD 3¥ M -?B?"? (1918) 2. ) ?3°?s ?μ? ^ ‘5 ? ’" O M -l? 18 19¥D 3 {μ {μ ANY ALLìM¥0°? ? ° ANY? ? ALLìMμ H V[¨"f ? LC – ANYD ALLD"f ?¥?1" = <> != < <= > >= ANY IN -- <MAX <=MAX >MIN >= MIN ALL -- NOT IN <MIN <= MIN >MAX >= MAX {μ {μ ANY ALLìM¥0°? ? – ¨"f ? LC0°?Yè1°¤¨ ANY ALL°?r q1úy1 -?Yè ?h 1?Q ? {μ {μ ANY ALLìM¥0°? ? [ è]¨"f ? LC SELECT SnameSage FROM Student WHERE Sage < (SELECT MAX(Sage) FROM Student WHERE Sdept= ‘5 ? ') AND Sdept <> ‘5 ? ’; {μ {μ ANY ALLìM¥0°? ? [ è] °?  e"?1?" ?μ D 3 M - ? ? l¥ D 3? # M -b ZEB¨ ALLìM SELECT SnameSage FROM Student WHERE Sage < ALL (SELECT Sage FROM Student WHERE Sdept= ' IS ') AND Sdept <> ' IS ’; °?2T1 bVb {μ {μ ANY ALLìM¥0°? ? ZE=¨"f ? SELECT SnameSage FROM Student WHERE Sage < (SELECT MIN(Sage) FROM Student WHERE Sdept= ' IS ') AND Sdept <>' IS ’; 1a{μ EXISTSìM¥0°? 1. EXISTSìM 2. NOT EXISTSìM 3. ?]? T¥°?W¥9D 4. M10°?¥r q 5. ¨ EXISTS/NOT EXISTS LC ?? M 6. ¨ EXISTS/NOT EXISTS LC ? f {μ EXISTSìM¥0°? (? °&9*454ìM – i M ? – {μ &9*454ìM¥0°??Rí ?? ? oá 3 ? ?′ “USVF” ? L′ “GBMTF”b ° ? =a°?2Td b5Rí?′ ° ? =a°?2T1 b5RíL′ – ? &9*454?¥0°?  "S Vr TYè?¨ y1{ &9*454¥0°?oRí?′L′ó   í L=il °/05&9*454ìM {μ EXISTSìM¥0°? (? < è>°? ?μê?  | [?¥D 3? b – ¨ 3*°? 4&-&$54OBNF '30.4UVEFOUY 8)&3&&9*454 4&-&$5 '30.4$ M10°?  8)&3&4OPY4OP "/% $OP  p3V? {μ EXISTSìM¥0°? (? ± ^s ° '°? # 4UVEFOU?? 4$1"b °  4UVEFOU?GQ | ??íF¥ 4OP′¨N′ ? _° 4$1"b ° ? ? 4$?i?"¥íF  4OP′??N 4UVEFOU4OP′i O  $OP5 |N 4UVEFOU4OBNF ? ?2T1"b {μ EXISTSìM¥0°? (? – ¨ ?¤ ? 4&-&$54OBNF '30.4UVEFOU 4$ 8)&3&4UVEFOU4OP4$4OP "/% 4$$OP {μ EXISTSìM¥0°? (? < è è >°? àμê? | [?¥D 3? b 4&-&$54OBNF '30.4UVEFOU 8)&3&/05&9*454 4&-&$5 '30.4$ 8)&3&4OP 4UVEFOU4OP "/%$OP  N è¨ ?¤ ? 4? LC {μ EXISTSìM¥0°? (? ?]? T¥°?W¥9D Bt {&9*454/05&9*454ìM¥0°?? ?$  e ? T¥0°??N9D ?μ {*/ìMa1? ??a"/:?"--ìM¥0°? ? ?¨{&9*454ìM¥0°??N9Db {μ EXISTSìM¥0°? (? è°?D “ >? ”]B?"D¥D 3b V[ ¨{&9*454ìM¥0°?9D 4&-&$54OP4OBNF4EFQU '30.4UVEFOU4 8)&3&&9*454 4&-&$5 '30.4UVEFOU4 8)&3&44EFQU44EFQU"/% 44OBNF >?  {μ EXISTSìM¥0°? (? ¨ &9*454/05&9*454 LC ?? M 4? – 42-?y? àμ ?? M ?  'PSBMM – V[ü{μ ?? M¥ìMD1?N¥{μ i M¥ìM ?Y 1? ? ? Y ? 1 {μ EXISTSìM¥0°? < è è >°?ê?  ?? [?¥D 3? b 4&-&$54OBNF '30.4UVEFOUZ 8)&3&/05&9*454  4&-&$5 '30.$PVSTFY 8)&3&/05&9*454 4&-&$5 '30.4$ 8)&3&4OPZ4OP "/%$OPY$OP 3.5 ° ? z 3.5.1 ?V°? z 3.5.2 ?¤°? z 3.5.3 SQL?¥ ?¤°? z 3.5.4 3*°? z 3.5.5 "??T z 3.5.6 SELECT¥B?ì T "?°? S42-°¤|?¥"??T? ? i?T 6/*0/ B? ¨ ? o|?¥"??T? ? i?T 6/*0/ ??T */5&34&$5 μ?T .*/64 1i?T z? T <°? v > UNION <°? v > – ?F UNION?T¥ò2TV¥  ?A?M ] ?[¥ ? ??9A?M] i?T ? [ è] °?9 ? SD"¥D 3# M -?v? 19 ? ¥D 3b ZEB SELECT * FROM Student WHERE Sdept= 'CS' UNION SELECT * FROM Student WHERE Sage<=19 i?T ? ZE= SELECT DISTINCT * FROM Student WHERE Sdept= 'CS' OR Sage<=19 i?T ? [ è46] °?ê?  [? 1?ê?  [? 2¥D 3b ZEB SELECT Sno FROM SC WHERE Cno=' 1 ' UNION SELECT Sno FROM SC WHERE Cno= ' 2 ' i?T ? ZE= SELECT DISTINCT Sno FROM SC WHERE Cno=' 1 ' OR Cno= ' 2 ' i?T ? [ è] ! ? o?μB? =V Teacher(Tno, Tname,...)b°?Dn? ?μ = 3¥? b SELECT Sname FROM Student UNION SELECT Tname FROM Teacher; new 2??T S SQL? àμ4"???T? V¨  eZEW¤ LCb 2??T [ è] °?9 ? SD"¥D 3D M -?v? 19 ? ¥D 3¥?" ' è L= ü ^°?9 ? SD"? M -?v? 19 ?¥D 3 SELECT * FROM Student WHERE Sdept= 'CS' AND Sage<=19 ??T ? [ è] °?ê? [? 1¥D 3"?Dê? [? 2¥D 3"?¥?" ' è L=  ^°?;ê?  [? 1?ê?  [? 2 ¥D 3 SELECT Sno FROM SC WHERE Cno=' 1 ' AND Sno IN (SELECT Sno FROM SC WHERE Cno=' 2 ') ??T ? [ è] °?D 3? D? =? ¥?" ' è L=  ^°?Dn?D? =] ¥D 3? SELECT DISTINCT Sname FROM Student WHERE Sname IN (SELECT Tname FROM Teacher); new 3μ?T S SQL? àμ4"?μ?T? V¨  eZEW¤ LCb 3μ?T [ è] °?9 ? SD"¥D 3D M -?v? 19 ? ¥D 3¥μ"b ' è L=  ^°?9 ? SD"? M -v? 19 ?¥D 3 SELECT * FROM Student WHERE Sdept= 'CS' AND Sage>19 μ?T ? [ è] °?D 3? D? =? ¥μ" ' è L=  ^°?Dn??D? =] ¥D 3 ? SELECT DISTINCT Sname FROM Student WHERE Sname NOT IN (SELECT Tname FROM Teacher); new 4. "??T2T¥ ?? z ORDER BY0 o ?¨?K?°?2 T ??? ??W2T ?? z ?? f ?/ ORDER BY0 o ?C Ka z"??T2T ?? H ORDER BY0 ?¨ ?3·? ?? ?? "??T2T¥ ?? ? [ è] pE SELECT * FROM Student WHERE Sdept= 'CS' ORDER BY Sno UNION SELECT * FROM Student WHERE Sage<=19 ORDER BY Sno new "??T2T¥ ?? ? ? ?E SELECT * FROM Student WHERE Sdept= 'CS' UNION SELECT * FROM Student WHERE Sage<=19 ORDER BY 1 new 3.3.6 SELECT? ¥B?ì T SELECT [ALL|DISTINCT] < "S Vr T > [Y ] [ < "S Vr T > [Y ]] … FROM <V  jm > [Y ] [ <V  jm > [Y ]] … [WHERE <HqVr T >] [GROUP BY <  1>[<  1’>] ... [HAVING <HqVr T >]] [ORDER BY <  2> [ASC|DESC] [<  2’> [ASC|DESC] ] … ] "S Vr T z "S Vr Tì T (1) [ <V >.] * (2) [<V >.]< ??  Vr T >[[<V >.]< ??  Vr T >] … < ??  Vr T >? ??  aT¨? ?? ¥ "f ? ? è  ¥ ?i ? ? ? +-*/F?¥ ? Tb "f ?ì T COUNT SUM AVG ([DISTINCT|ALL] <  >) MAX MIN COUNT ([DISTINCT|ALL] *) HqVr Tì T  1 < ??  > < ??  > , <è  > [ANY|ALL] (SELECT?  ) HqVr Tì T  2 < ??  > < ??  > < ??  > [NOT] BETWEEN <è  > AND <è  > (SELECT (SELECT ?  ) ?  ) HqVr Tì T  3 (<′ 1>[<′ 2> ] …) < ??  > [NOT] IN (SELECT?  ) HqVr Tì T (4) < ??  > [NOT] LIKE < ? ¥1 > (5) < ??  > IS [NOT] NULL (6) [NOT] EXISTS (SELECT?  ) HqVr Tì T  7 AND AND <HqVr T > <HqVr T > <HqVr > … OR OR 3.6 ? ÷? = 1. ? ?0°?2T z? ì T INSERT INTO <V > [(< ??  1> [< ??  2>… )] 0°?  z? ? |0°?2T? ?·?V? ? ?0°?2T ? [ è3]  ?B?" pD 3¥ ü ( M -i ü2Ti ? ? ob ?B?yV CREATE TABLE Deptage (Sdept CHAR(15) /* " */ Avgage SMALLINT) /*D 3 ü ( M - */ ? ?0°?2T ? ?=?? ? ? INSERT INTO Deptage(SdeptAvgage) SELECT SdeptAVG(Sage) FROM Student GROUP BY Sdept ? ?0°?2T ? – INTO0 (D? ??HíF ? ? ) z ·?1? ? ? ¥V # ??  z ?? ¥ ¨? VDV?l?¥ ¨??Bá z àμ·? ?? V U1? ?¥ ^BH??¥í F z ·??s ?? ? ?¥íF ? ??   | b′ – 0°? z SELECT0  "S A?D INTO0  ? ¥ –′¥? ? –′¥ ?? ? ?0°?2T ? DBMS3?? ??  H?_° ??íF ^ ? ?êV X?l¥????5 – L8??? – ?v??? – ¨??l¥??? z ?μ NOT NULL? ?¥ ??  ^?4 d b′ z ?μ UNIQUE? ?¥ ??  ^?4 d×ˉ′ z ?μ′×? ?¥ ??  ?4¥ ??′ ^?′×S? = 2. {0°?¥???  [ è ] |9 ? SD" ?8D 3¥?? ,b UPDATE SC SET Grade=0 WHERE 'CS'= (SELETE Sdept FROM Student WHERE Student.Sno = SC.Sno) 3. {0°?¥ ÷"?  [ è11] ÷"9 ? SD" ?μD 3¥ê [: cb DELETE FROM SC WHERE 'CS'= (SELETE Sdept FROM Student WHERE Student.Sno=SC.Sno) ? ? ?  ?       ?   z  a ° ?B ° ?B     z  a ° ?= ° ?=     z  a ° ? ? ° ? ?