! It is not every question that deserves an answer. Publius Syrus. 42 B. C. "#$!"!%&"&! '%()** "!%+!!"#,%" "-+)**%! %!.!&,*!-%/ -%& "-!&"!!!*0**!"# $%&'()(*" #+,-'. /0$1-' #23)30 4. -'5.$ 1
6 SELECT (column_list) FROM table_name [INNER {LEFT RIGHT FULL } OUTER] JOIN table_name ON qualification_list WHERE 01 "-"2%"!&"-!##!!""!34402%" )3440"!#/, 6 5%(!&-!--%#""%! #* 00!*!"#!*&*+"# 56"%!!34407534!$! 54!*!"#8*!"#!%%!!"%!(%$!$#+%! 9!%%(! 00!*!"#!*&*+"# 56"%!!4:;:7534!$! <4:;:=&!> "2%"/%- "%/"+! ("!&& 2
337*'0'$ 8)(/3)6() (9 sid sname rating age 22 Dustin 7 45.0 31 Lubber 8 55.5 95 Bob 3 63.5 sid bid day 22 101 10/10/96 95 103 11/12/96 s.sid s.name r.bid 22 Dustin 101 95 Bob 103 (6 /5 7%"! &-#%(!! &-#%(!/%&+%/%/ 2%"-! )!!"/"#!%/%&-"!, 00!*!"#!*&*+"# 56"%!!05;07534!$! 54!*!"#8*!"#!!"%!"/%&"%%( $!$#+%! 3
337*'0'$ 8)(/38*(:*3)6() (9 sid sname rating age 22 Dustin 7 45.0 31 Lubber 8 55.5 95 Bob 3 63.5 sid bid day 22 101 10/10/96 95 103 11/12/96 s.sid s.name r.bid 22 Dustin 101 95 Bob 103 31 Lubber )(6 "5 7%"! &-#%(!! &-#%(!/%&+%" %/2%"-! 00*!"#+*+"#+*& 56!$!3?5;07534@%!+ 54*+"#8+*+"#! +%!"/%&"%%("-%!!$#* 4
337*'$$'$0 8)(/));2*(:*3)6(<$ ($9$$ sid bid day 22 101 10/10/96 95 103 11/12/96 bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red r.sid b.bid b.name 22 101 Interlake 102 Interlake 95 103 Clipper 104 Marine 8(6 5 7%"! )&-#% &-#,%(!/%&+!%+%!"#!%/2%"-! 00*!"#+*+"#+*& 56!$!;5;07534@%!+ 54*+"#8+*+"#! +%! "/%&"%%!$"%! 5
337*'$$'$0 8)(/)8:(:*3)6(<$ ($9$$ sid bid day 22 101 10/10/96 95 103 11/12/96 bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red r.sid b.bid b.name 22 101 Interlake 102 Interlake 95 103 Clipper 104 Marine Note: in this case it is the same as the ROJ because bid is a foreign key in reserves, so all reservations must have a corresponding tuple in boats. 73 SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list HAVING group-qualification Project away columns (just keep those used in SELECT, GBY, HAVING) SELECT [DISTINCT] Eliminate duplicates Apply selections (eliminate rows) WHERE HAVING Eliminate groups Relation cross-product FROM GROUP BY Form groups & aggregate 6
)1 5A0@B-% & C:DA0EC *E SELECT S.rating, S.sname, S.age FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red ORDER BY S.rating, S.sname; %#+-% &"00"! "- #"1!!"%!%! SELECT S.sid, COUNT (*) AS redrescnt FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red GROUP BY S.sid ORDER BY redrescnt DESC; =&3>&< 0 CREATE VIEW view_name AS select_statement Makes development simpler Often used for security Not instantiated - makes updates tricky CREATE VIEW Reds AS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color= red GROUP BY B.bid 7
) CREATE VIEW Reds AS SELECT B.bid, COUNT (*) AS scount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color= red GROUP BY B.bid b.bid scount 102 1 Reds SELECT bname, scount FROM Reds R, Boats B WHERE R.bid=B.bid AND scount < 10 &1%7 GRANT privileges ON object TO users [WITH GRANT OPTION] 5+2--++%F"( "$"!-+ - 3! A /-!)-%!,G %(%-/%" '/-!! -"/"#-% &)!, : +0F5H0# ;!!-+!"!!%%! I/%&%#"!* 8
* 00 %!"! &+###"%! 17-). :3#!-"+!-%#""%!$"!- %/"%&!!"!/* 74 7$0-' 1.'-'0 $ ' 0$?@AA.!%/3.!A%&"-%!"! "&' -%!"!/%"'-%!"! -%!"!* &0=80$1 % 1 B0111=1 0 9
; 7 ;!/ ( &%3! '! "$%$#*!> "! %1!! -%!"* -'#%"! % #* %!"!- +&#* CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 )) CREATE TABLE Reserves ( sname CHAR(10), bid INTEGER, day DATE, PRIMARY KEY (bid,day), CONSTRAINT nointerlakeres CHECK ( Ìnterlake <> ( SELECT B.bname FROM Boats B WHERE B.bid=bid))) 7(/) CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), :('(## rating INTEGER, (%J age REAL, 5--'!!"%!J PRIMARY KEY (sid), 5> "#% %#"/ CHECK!!%-"#+"! %& * :0354"! "!% "%K%!!%-"#(" "+* ;/% %! %#"& A@6* "! %!% "%* Number of boats plus number of sailors is < 100 ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) CREATE ASSERTION smallclub CHECK ( (SELECT COUNT (S.sid) FROM Sailors S) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) 10
#% "!% %! %&&" * C*0 C)110DD, 74 5 "%! /!1E*0F %E000F $'01 % $0$00 =#$0 " 30$ A@6$#%!! %$"#<%! +"#"!= 377(<( % 0$0 0 *11110 B1&<1 $1 ($-'. 0$0 "-$#%! -"/"- # 41' 4 %+& -0,.'$ 0$7 00 11
61! 7 #--!%%"%%> % -!% #/- )&%$"-!%, -" $ (!$+"$#* 5A0@B %(!-%%%$%#"("-! #* 8()&3)<G0337*!%&%#"/L# %"#%+-!% %E,F 1.!0&+###!1/%-!%! &37%)3?07:)()8()?0 83*728)(/?0*(?$0 < 46&<7 12
&$%B=% 0$ &%#"/-%& "##"+ ("#+!-!):3, $5 0',1 1 (&<777CC# 6&<76! /0 38B&<'EBF' 6%!A@6 1 &</ % Application ODBC driver Data Source %0E0F-E&F. *11$1( <&'EF 0 *'0&</, &$$ (&<7'00$-. 0$10 &01$$H 13
(&<76&<7 F"%!$#%! %$"##"$! /$$# &&( 0( A"$!/%$"%!#!% -! )&</-('&<@''0>'. E&F&</-%'&$'B>'8>B'. -/3>'I,@,+'. &0>-7'*J*'. B% -!7A@L5A@-"!%$&#!% -! 3/10 01/( -0!1+@>. ("% %(7$%MM %&!"!1A@ 6&<7 %/7$$!%! 7$-%&!("7A@%5A@+"# 6&<71(&<7 31# 7B 6&<7(&<7H 7A@ %"%" =50<6&<7 * 14
6&<7<=7 :%-"%"!%+2-!"%"% #+! // GET CONNECTION Connection con; try { con = DriverManager.getConnection( "jdbc:odbc:sailorsdb", username,password); } catch(exception e){ System.out.println(e); K 0$ % -%!-%-"% // CLOSE CONNECTION try { con.close(); } catch (Exception e) { System.out.println(e); } 6&<7<=0 B% #&%+2-/%-!& 7)3%*3*%*3/3* 00L 1M 090-.L K-3>.M 10-.L K 410>1-ENF.L 15
70$ (%% "%! %-& createstatement(resultset.<type>, ResultSet.<CONCUR>) 7 NB0O"!%%/ *GB3O8()#%)&O(G=40$ *GB3O7)(O3*3=0$ '$4 1 *GB3O7)(O3*3=0$ ' 0 N54;O"!%%/ 7(7:)O)3%&O(G=04 7(7:)O:B&%*%<3=0 A/! *GB3O8()#%)&O(G7(7:)O)3%&O(G 6&<7<=) :! %+2-!$!!-!%/%!&.!!!)!&*1- ),, // EXECUTE QUERY ResultSet results; try { results = stmt.executequery( "select * from Sailors") } catch (Exception e){ System.out.println(e); } 5+$"%!#&%#! >-.> )EF $-$. E$F = -.'-.'$-.' -.'-.'$8-.'-. 16
)/ /"#%! /+%!!-&$"! 6A ResultSetMetaData rsmd = results.getmetadata(); int numcols = rsmd.getcolumncount(); int i, rowcount = 0; // get column header info for (i=1; i <= numcols; i++){ if (i > 1) buf.append(","); buf.append(rsmd.getcolumnlabel(i)); } buf.append("\n"); 5! 6A&%#! 70*1-.'$-.' ; 7 7 " // break it off at 100 rows max while (results.next() && rowcount < 100){ // Loop through each column, getting the // column data and displaying for (i=1; i <= numcols; i++) { if (i > 1) buf.append(","); buf.append(results.getstring(i)); } buf.append("\n"); rowcount++; } "&"%3-* 17
:7 7 ; #/"#!"- %/-!% result.next(); result.updateint("rating", 10); :!% #" #%-* 5-(!! +&"/ ;A:0!& >1-. %""!&&!$+ 54;P;A::@0""-!J 71 try { // CLOSE RESULT SET results.close(); // CLOSE STATEMENT stmt.close(); // CLOSE CONNECTION con.close(); } catch (Exception e) { System.out.println(e); } 18
B *- 1. Connection con = DriverManager.getConnection("jdbc:odbc:weblog",userNa me,password); Statement stmt = con.createstatement(); ResultSet results = stmt.executequery("select * from Sailors") ResultSetMetaData rsmd = results.getmetadata(); int numcols = rsmd.getcolumncount(), i; StringBuffer buf = new StringBuffer(); while (results.next() && rowcount < 100){ for (i=1; i <= numcols; i++) { if (i > 1) buf.append(","); buf.append(results.getstring(i)); } buf.append("\n"); } results.close(); stmt.close(); con.close(); 0 $ %&&%!-"%%#"!%$(+-" % $0!1&< )0 2*/ 6(+!-" "!!# 5''B2B' 00'6&<7 2*/0> 19
3B2BB <?php $conn = pg_pconnect("dbname=cowbook user=jmh\ password=secret"); if(!$conn) { echo "An error occured.\n"; exit; } $result = pg_query ($conn,"select * FRO M Sailors"); if(!$result){ echo "An error occured.\n"; exit; } $num = pg_num_rows($result); for($i=0; $i < $num; $i++) { $r = pg_fetch_row($result, $i); for($j=0; $j < count($r);$j++) { echo "$r[$j] "; } echo "<BR>"; }?> %B001 :3!##%"/-A@6! % %&&"! 30$EF 1$ (&<7-$/,.77CC 6&<76 -B2B'B'6B. $0! $,$ 10 20