!-%/ -%& "-!&"!!!*0**

Similar documents
SQL III. The Query Language. R & G - Chapter 5. Based on Slides from UC Berkeley and book.

SQL: Queries, Programming, Triggers

SQL. Chapter 5 FROM WHERE

Basic form of SQL Queries

SQL: Queries, Constraints, Triggers

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

CIS 330: Applied Database Systems

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

The Database Language SQL (i)

Data Science 100. Databases Part 2 (The SQL) Slides by: Joseph E. Gonzalez & Joseph Hellerstein,

Introduction to Data Management. Lecture #14 (Relational Languages IV)

Database Applications (15-415)

Data Science 100 Databases Part 2 (The SQL) Previously. How do you interact with a database? 2/22/18. Database Management Systems

Enterprise Database Systems

SQL: Queries, Constraints, Triggers (iii)

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

Experimenting with bags (tables and query answers with duplicate rows):

Database Systems ( 資料庫系統 )

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

SQL: The Query Language Part 1. Relational Query Languages

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

Database Management Systems. Chapter 5

Keys, SQL, and Views CMPSCI 645

Database Applications (15-415)

This lecture. Step 1

SQL: The Query Language (Part 1)

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

Review: Where have we been?

CSE 444: Database Internals. Section 4: Query Optimizer

SQL: Queries, Programming, Triggers

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Rewrite INTERSECT using IN

Database Application Development

SQL - Lecture 3 (Aggregation, etc.)

QUERIES, PROGRAMMING, TRIGGERS

Midterm Exam #2 (Version A) CS 122A Winter 2017

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Database Applications (15-415)

Database Applications (15-415)

HW2 out! Administrative Notes

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

CITS3240 Databases Mid-semester 2008

Relational Calculus. Chapter Comp 521 Files and Databases Fall

SQL SQL SQL SQL SQL SQL SQL. Eugene Wu Fall 2018

Overview of Query Processing

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

CSIT5300: Advanced Database Systems

CSE 444: Database Internals. Sec2on 4: Query Op2mizer

CompSci 516 Data Intensive Computing Systems

Database Management Systems. Chapter 5

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

Database Management Systems. Chapter 5

CS 186, Fall 2002, Lecture 8 R&G, Chapter 4. Ronald Graham Elements of Ramsey Theory

CompSci 516: Database Systems

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

RELATIONAL ALGEBRA AND CALCULUS

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Query Optimization. Schema for Examples. Motivating Example. Similar to old schema; rname added for variations. Reserves: Sailors:

Relational Algebra 1

Relational Algebra. Relational Query Languages

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

CSIT5300: Advanced Database Systems

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Relational Query Optimization. Highlights of System R Optimizer

Database Applications (15-415)

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Database Application Development

Database Systems. Course Administration. 10/13/2010 Lecture #4

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

Database Systems. Announcement. December 13/14, 2006 Lecture #10. Assignment #4 is due next week.

Database Management System. Relational Algebra and operations

CS330. Query Processing

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

An SQL query is parsed into a collection of query blocks optimize one block at a time. Nested blocks are usually treated as calls to a subroutine

ATYPICAL RELATIONAL QUERY OPTIMIZER

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

CSCC43H: Introduction to Databases. Lecture 3

Overview of Query Evaluation. Overview of Query Evaluation

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

Security and Authorization

Relational Algebra 1

Outer Join, More on SQL Constraints

Relational Query Optimization

Relational Query Languages

MIS Database Systems Relational Algebra

Schema for Examples. Query Optimization. Alternative Plans 1 (No Indexes) Motivating Example. Alternative Plans 2 With Indexes

Overview of Query Evaluation

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

Introduction to Data Management. Lecture #18 (SQL, the Final Chapter )

UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

Transcription:

! 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