ppt

44
M.P. Johnson, DBMS, Stern/NYU, Sprin g 2008 1 C20.0046: Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008

Upload: techdude

Post on 26-Jan-2015

1.266 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

1

C20.0046: Database Management SystemsLecture #14

M.P. Johnson

Stern School of Business, NYU

Spring, 2008

Page 2: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

2

Agenda Today:

midterms, etc Proj2, Mysql accounts SQL programming

Page 3: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

3

Midterm, etc. Stats:

Possible: Max: Mean: Median: Stdev: “Pretty good”: >=

Go through MT…

Page 4: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

4

proj2 Give relational model (normalized as needed) for

your project

Give interesting queries

Create tables (indices, etc.) for it in your account

Should be straightforward If you have an account…

Due: 4/2

Page 5: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

5

NYU Infrastructure Server: sales (sales.stern.nyu.edu) To connect, ssh to sales

http://www.google.com/search?hl=en&q=ssh+client&btnG=Google+Search

I use: WinSCP/Putty

Server: i5.nyu.edu (ssh) User/pass: netid / netid (or nyu pass?)

MySQL user/pass: NetID / NetID

MySQL shell: mysql MySQL host: mysql2 See proj3 page for exact command syntax

sales% mysql -h mysql2 -u my-NetID -p my-UID Check that your account works tonight! phpMyAdmin: http://i5.nyu.edu/phpMyAdmin/

Page 6: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

6

Programming for SQL Today: from procedural languages to SQL

Pro*C JDBC

Next: DB-backed apps on the web PHP

Later: PL/SQL, Triggers

Page 7: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

7

R.A./SQL has limitations Can easily find Alice’s direct subordinates:

But: find all of King’s underlings Cannot compute “transitive closure” Cannot express in R.A./SQL! SQL is not “Turing-Complete”

Name Job Boss

King President NULL

Jones Manager King

Blake Manager King

Ford Analyst Jones

Scott Analyst Jones

Page 8: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

8

New topic: SQL Programming Can write SQL queries in a SQL interpreter

Command prompt SQL*Plus (sqlplus) in Oracle mysql in MySQL

Good for experimenting, not for anything non-trivial

Better: use a standard programming language Host language talks to SQL/DB

Page 9: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

9

Using two languages Q: Why not use just one language? “[W]e can quickly dispense with the idea…” (Ullman, p351)

Q: Why not do everything in SQL? A: Not designed as a general-purpose language

No recursion (no factorial!), not Turing-complete No, e.g., Swing library

Q: Why not do everything in the host lang.? A: What Oracle provides is highly non-trivial

Query interpretation, optimizing Queries stay constant across host languages Many points of entry

Germ of OO: modularize

Page 10: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

10

Impedance mismatch problem Big problem, though: impedance mismatch

Data structures in our app-programming lang. don’t automatically map onto those in SQL Different types/representations for data

In SQL: tables with scalar fields In C: scalars, records (containing records…),

pointers, arrays In Java: scalars, objects, references, arrays In Perl: scalars, lists/arrays, hashes/assoc.

Page 11: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

11

SQL/host interface in embedded SQL So Q: how to transfer data between? A: Shared variables

Some vars in the program can be used by SQL Prefix var with a : After query, look here for received data

SQL commands embedded in app. code Identified by EXEC SQL

Source code is preprocessed before regular compilation Result is (e.g.) a C program with library calls

Page 12: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

12

Programs with Embedded SQL

Preprocessor

Host language compiler

PreprocessorOracle’sPro*C

Host language compiler gcc

Host language + Embedded SQL prog.pc

Host Language + function calls

prog.c

Executablea.out

Page 13: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

13

Interface: SQL / Host Language Values get passed through shared variables.

Colons precede shared variables in SQL statements EXEC SQL demarcates every SQL statement

The variable SQLSTATE provides error messages and status reports “00000” ~ success “02000” ~ tuple not found

Used in loops

EXEC SQL BEGIN DECLARE SECTION;char productName[30];char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

EXEC SQL BEGIN DECLARE SECTION;char productName[30];char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

Page 14: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

14

Embedded SQL example Context:

Product (pname, price, quantity, maker) Purchase (buyer, seller, store, pname) Company (cname, city) Person(name, phone, city)

Goal 1: Insert a new row in Purchase Goal 2: Look up price of product by name

Page 15: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

15

Embedded SQL example: insertvoid simpleInsert() {

EXEC SQL BEGIN DECLARE SECTION;char pn[20], cn[30];/* product-name, company-name */double p, int q; /* price, quantity */char SQLSTATE[6];EXEC SQL END DECLARE SECTION;

/* get values for name, price and company somehow */

EXEC SQL INSERT INTO Product(pname, price, quantity, maker)

VALUES (:pn, :p, :q, :cn); }

void simpleInsert() {

EXEC SQL BEGIN DECLARE SECTION;char pn[20], cn[30];/* product-name, company-name */double p, int q; /* price, quantity */char SQLSTATE[6];EXEC SQL END DECLARE SECTION;

/* get values for name, price and company somehow */

EXEC SQL INSERT INTO Product(pname, price, quantity, maker)

VALUES (:pn, :p, :q, :cn); }

Page 16: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

16

Embedded SQL example: look-up

int getWindowsPrice() {

EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = ‘Windows’;

return p;}

int getWindowsPrice() {

EXEC SQL BEGIN DECLARE SECTION; double p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = ‘Windows’;

return p;}

Page 17: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

17

Embedded SQL example: look-up What about search for arbitrary product?

Q: Will this work?

int getPrice(char *name) {

EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :name;

return p;}

int getPrice(char *name) {

EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :name;

return p;}

Page 18: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

18

Embedded SQL example: look-upint getPrice(char *name) {

EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

strcpy(n, name); /* copy name to local var */

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :n;

return p;}

int getPrice(char *name) {

EXEC SQL BEGIN DECLARE SECTION; char n[20]; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

strcpy(n, name); /* copy name to local var */

EXEC SQL SELECT price INTO :pFROM ProductWHERE Product.name = :n;

return p;}

Page 19: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

19

Cursors (quickly to 32) For product’s price, looked up single (scalar) value Q: What if we SELECT multiple fields?

E.g., find all info for some product A: Just list destination vars separated by commas

Q: What if find multiple rows? E.g., find all products above a certain price

Use a cursor to step through the results Each result placed in an array

Using cursors:1. Declare the cursor2. Open the cursor3. Fetch tuples one by one4. Close the cursor

Page 20: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

20

Cursor loop structure Each time around loop, we

Do a FETCH to obtain next row Examine SQLSTATE to check success Can say:

What is NO_MORE_TUPLES?

#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))#define NO_MORE_TUPLES !(strcmp(SQLSTATE,”02000”))

if(NO_MORE_TUPLES) break;if(NO_MORE_TUPLES) break;

Page 21: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

21

Multiple-row look-up examplevoid productToXML() {

EXEC SQL BEGIN DECLARE SECTION;char pn[20], cn[30];double p; int q;char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE crs CURSOR FOR

SELECT pname, price, quantity, maker

FROM Product;

EXEC SQL OPEN crs;

...

void productToXML() {

EXEC SQL BEGIN DECLARE SECTION;char pn[20], cn[30];double p; int q;char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE crs CURSOR FOR

SELECT pname, price, quantity, maker

FROM Product;

EXEC SQL OPEN crs;

...

Page 22: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

22

Multiple look-up exampleprintf("<allProducts>\n");

while (1) {

EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c;

if (NO_MORE_TUPLES)

break;

printf(“<product>\n”);

printf(“ <name>%s</name>\n”, n);

printf(“ <price>%d</price>\n”, p);

printf(“ <quantity>%d</quantity>\n”, q);

printf(“ <maker>%s</maker>\n”, c);

printf(“</product>\n”);

}

EXEC SQL CLOSE crs;

printf(“</allProducts>\n”);}

printf("<allProducts>\n");

while (1) {

EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c;

if (NO_MORE_TUPLES)

break;

printf(“<product>\n”);

printf(“ <name>%s</name>\n”, n);

printf(“ <price>%d</price>\n”, p);

printf(“ <quantity>%d</quantity>\n”, q);

printf(“ <maker>%s</maker>\n”, c);

printf(“</product>\n”);

}

EXEC SQL CLOSE crs;

printf(“</allProducts>\n”);}

Page 23: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

23

More on Cursors Cursors can traverse both stored tables and queries

Cursors can modify a relation as well as read it

Cursors can be protected against changes to the underlying relations

Can determine the order in which the cursor will get tuples by the ORDER BY keyword in the SQL query

The cursor can be a scrolling one: can go forward, backward +n, -n, Abs(n), Abs(-n)

Page 24: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

24

Cursor on query not table

EXEC SQL DECLARE c CURSOR FORSELECT beer, priceFROM SellsWHERE bar = ‘Izzy''s';

EXEC SQL OPEN CURSOR c;while(1) {

EXEC SQL FETCH cINTO :theBeer, :thePrice;

if (NOT FOUND) break;/* format and print beer and price */

}EXEC SQL CLOSE CURSOR c;

EXEC SQL DECLARE c CURSOR FORSELECT beer, priceFROM SellsWHERE bar = ‘Izzy''s';

EXEC SQL OPEN CURSOR c;while(1) {

EXEC SQL FETCH cINTO :theBeer, :thePrice;

if (NOT FOUND) break;/* format and print beer and price */

}EXEC SQL CLOSE CURSOR c;

Page 25: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

25

Modifications with cursors As we traverse through result set, can modify the

current row Can also modify with arb. WHERE clauses

NB: In regular SQL, usually modify sets of rows (UPDATE WHERE …)

With cursors, we update the last row fetched

Simple example: in RentStab table, we decide we want to raise (e.g., by 5%) all our prices Unless price > 2000, in which case they’re deleted

Page 26: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

26

Modification by cursor examplevoid raisePrices() {

EXEC SQL BEGIN DECLARE SECTION;double p;char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE crs CURSOR FOR

SELECT price

FROM RentStab;

EXEC SQL OPEN crs;...

void raisePrices() {

EXEC SQL BEGIN DECLARE SECTION;double p;char SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE crs CURSOR FOR

SELECT price

FROM RentStab;

EXEC SQL OPEN crs;...

Page 27: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

27

Modification by cursor examplewhile (1) {

EXEC SQL FETCH FROM crs INTO :p;

if (NO_MORE_TUPLES) break;

if (p < 2000)

EXEC SQL UPDATE RentStab

SET price = 1.05*price;

WHERE CURRENT OF RentStab;

else

EXEC SQL DELETE FROM RentStab

WHERE CURRENT OF RentStab;

}

EXEC SQL CLOSE crs;

}

while (1) {

EXEC SQL FETCH FROM crs INTO :p;

if (NO_MORE_TUPLES) break;

if (p < 2000)

EXEC SQL UPDATE RentStab

SET price = 1.05*price;

WHERE CURRENT OF RentStab;

else

EXEC SQL DELETE FROM RentStab

WHERE CURRENT OF RentStab;

}

EXEC SQL CLOSE crs;

}

Page 28: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

28

Limitation of embedded SQL Okay for apps with a fixed set of

queries/updates Maybe very simple kiosks

But consider, say, sqlplus or the sqlzoo website Processes arbitrary queries from user

Can we do this with embedded SQL?

Page 29: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

29

Dynamic SQL In dynamic SQL, query string can be taken as

a parameter, passed to DB

Two steps: Prepare: compiles/optimizes the string Execute: executes the query

Combine together: EXECUTE IMMEDIATE But separate if query is executed many times

(why?)

Page 30: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

30

Dynamic SQL myquery = a SQL variable

not prefixed by :

void runQuery() {EXEC SQL BEGIN DECLARE SECTION;

char *command; EXEC SQL END DECLARE SECTION;

/* command set to some query string */

EXEC SQL PREPARE myquery FROM :command;

EXEC SQL EXECUTE myquery;

/* or just: */

EXEC SQL EXECUTE IMMEDIATE :command;

}

void runQuery() {EXEC SQL BEGIN DECLARE SECTION;

char *command; EXEC SQL END DECLARE SECTION;

/* command set to some query string */

EXEC SQL PREPARE myquery FROM :command;

EXEC SQL EXECUTE myquery;

/* or just: */

EXEC SQL EXECUTE IMMEDIATE :command;

}

Page 31: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

31

Sketch of sqlplus Something like the sqlplus program could be

written as a simple dynamic SQL future hw idea?

EXEC SQL BEGIN DECLARE SECTION; char query[MAX QUERY LENGTH];EXEC SQL END DECLARE SECTION;/* issue SQL> prompt *//* read user's text into array query */EXEC SQL EXECUTE IMMEDIATE :query;/* go back to reissue prompt */

EXEC SQL BEGIN DECLARE SECTION; char query[MAX QUERY LENGTH];EXEC SQL END DECLARE SECTION;/* issue SQL> prompt *//* read user's text into array query */EXEC SQL EXECUTE IMMEDIATE :query;/* go back to reissue prompt */

Page 32: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

32

Interface: SQL/Host Language Two languages: SQL, host (C/Java/whatever) Benefits:

DB code (SQL is portable) SQL, host language focus on own strengths

SQL code placed in host language code SQL and host language have diff. data types

“impedance mismatch” Data transferred with “shared variables”

Use cursors to access/modify data Error messages placed in SQLSTATE

Page 33: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

33

Programs with Embedded SQL

Host language + Embedded SQL

Preprocessor

Host Language + function calls

Host language compiler

Executable

Preprocessor

Host language compiler

Oracle’sPro*C

gcc

prog.pc

prog.c

a.out

Page 34: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

34

Next topic: SQL/CLI (break?) Pro*C converts EXEC SQL code

--into what?

If we know the API (“Call-Level Interface”), can call library routines by hand

Is this better or worse? Pros & cons

Won’t cover in depth

Page 35: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

35

CLI: Java

Host language + Embedded SQL

Preprocessor

Host Language + function calls

Host language compiler

Executable

Preprocessor

Host language compiler

Oracle’sPro*C

javac + jar

prog.pc

Prog.java

Proj.class

Page 36: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

36

CLI - Overview Similar to what really happens in embedded SQL

implementations. Major approaches:

SQL/CLI - standard of ODBC JDBC - Java database connectivity See http://cbbrowne.com/info/middleware.html for many options

Advantages over embedded SQL: Avoid preprocessor-stage, easier to debug In th., use same program with several DBMS “write once, run everywhere”

Disadvantages: Must keep up to date with API changes DBMS may have conflicting APIs

Page 37: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

37

Next topic: JDBC (Java’s CLI) As expected: Java too can talk to SQL

In some ways: much nicer

JDBC is an interface Changes very little Each vendor writes own plug-in

Dev. Strategy: write to API, run w/ the DBMS jar See http://servlet.java.sun.com/products/jdbc/drivers

for 219 (!) JDBC drivers

Page 38: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

38

JDBC1. Load JDBC driver for DBMS:

2. Obtain a connection:

What does this mean? Find info here:

$ORACLE_HOME/network/admin/tnsnames.ora

Class.forName("oracle.jdbc.driver.OracleDriver")Class.forName("oracle.jdbc.driver.OracleDriver")

Connection con = DriverManager.getConnection(

"jdbc:oracle:thin:@as19.es.its.nyu.edu:1521:STN2", username, passwd);

Connection con = DriverManager.getConnection(

"jdbc:oracle:thin:@as19.es.its.nyu.edu:1521:STN2", username, passwd);

Page 39: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

39

JDBC3. Obtain a statement object:

4. Run a query:

Or an update:

Statement stmt = con.createStatement();Statement stmt = con.createStatement();

stmt.executeQuery(“SELECT * FROM table”);stmt.executeQuery(“SELECT * FROM table”);

stmt.executeUpdate(“INSERT INTO mytable ” +

“VALUES(‘abc’, ‘def’)”);

stmt.executeUpdate(“INSERT INTO mytable ” +

“VALUES(‘abc’, ‘def’)”);

Page 40: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

40

Prepared Statements in JDBC JDBC also supports prepared statements

3. Obtain a PreparedStatement object:

4. Now execute:

When better?

PreparedStatement ps = con.createStatement(

“SELECT * FROM table”);

PreparedStatement ps = con.createStatement(

“SELECT * FROM table”);

ps.executeQuery();ps.executeQuery();

Page 41: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

41

Obtaining query results “Cursor” not used, but same idea executeQuery() returns a ResultSet:

rs.next() advances to new row, returns false if EOF getInt(i) returns ith column (if an int!) from current row can look up metadata…

ResultSet rs = ps.executeQuery();ResultSet rs = ps.executeQuery();

while (rs.next()) {

String val1 = rs.getString(1);

int val2 = rs.getInt(2);

}

while (rs.next()) {

String val1 = rs.getString(1);

int val2 = rs.getInt(2);

}

Page 42: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

42

Java & parameter-based SQL Like SQL/CLI in C, Java also supports

parameterized queries (why?)1. Prepare structure of query2. Then can set values

When better?

PreparedStatement ps = conn.prepareStatement( "SELECT * FROM table WHERE f1 = ? and f2 = ?");ps.setString(1, "abc");ps.setString(2, "def");ResultSet rs = ps.executeQuery();...

PreparedStatement ps = conn.prepareStatement( "SELECT * FROM table WHERE f1 = ? and f2 = ?");ps.setString(1, "abc");ps.setString(2, "def");ResultSet rs = ps.executeQuery();...

Page 43: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

43

Also: ODBC Used by Microsoft platforms/tools, others

Access: Start | Control Panel | Administrative Tools | Data

Sources (ODBC)

Similar to JDBC

Won’t cover

Page 44: PPT

M.P. Johnson, DBMS, Stern/NYU, Spring 2008

44

Next time DBs on the web: PHP, etc. Start working through PHP/MySQL tutorials

on website Skim relevant parts of ch.7

If you (and your group members) can’t log in, let me know ASAP