SQL in the real world 1. The SQL Client-Server Interactions SQL-clientSQL-server Environment SQL-agent Module Connection Session Current catalog and schema.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Objective: To access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not.
UJeffrey D. Ullman uAnfang von: CS145 - Herbst Stanford University uOnline unter: Folien mit weißem Hintergrund wurden.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Announcements Read JDBC Project Step 5, due Monday.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1 DATADABASE PROGRAMMING 2Chapter 13 from our text.
1 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
CS 405G: Introduction to Database Systems Database programming.
CSCD34 - Data Management Systems – A. Vaisman1 Database Application Development.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
1 Database Application Development Chapter 6 Sections 6.1 –
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
Databases 1 9th lecture. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
Introduction to PL/SQL. Main topic: Oracle PL/SQL What is PL/SQL? ▫Procedural Langauge extension for standard SQL.. PL/SQL not only allows you to create.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections 6.1 –
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
SQL Programming SQL in Application Programs
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: CLI & JDBC & Security Ch Ch. 9.6 – Ch 10.1.
6/9/2016CENG 352 Database Management Systems1 SQL in the Real World.
1 Database Design: DBS CB, 2 nd Edition SQL in a Server Environment: Stored Procedure & Embedded SQL Ch. 9.3, 9.4.
1 Introduction to Database Systems, CS420 SQL Persistent Stored Modules (PSM) – Stored Procedure.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
SQL in the real world 1. The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
1 Database Application Development Programming With Databases.
1 Database Application Development Chapter 6. 2 Overview  SQL in application code  Embedded SQL  Cursors  Dynamic SQL  JDBC.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.
COP Introduction to Database Structures
Introduction to Database Systems, CS420
Web Technologies IT230 Dr Mohamed Habib.
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Database Application Development
Database Application Development
DB Programming: SQL Programming Techniques
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Database-Connection Libraries
Database Application Development
Embedded SQL Chapter 8.
Database Application Development
Presentation transcript:

SQL in the real world 1

The SQL Client-Server Interactions SQL-clientSQL-server Environment SQL-agent Module Connection Session Current catalog and schema Written in a host language Contains Embedded SQL 2

SQL/Host Language Interface Function call SQL Library Host language program SQL stmt DBMS Prepare SQL statements from user inputs Fetch rows from the cursor TableCursor while(1){ } Impedance mismatch problem 3

Connecting SQL to the Host Language 1.Embedded SQL is a standard for combining SQL with seven languages. 2.CLI (Call-Level Interface ) is a different approach to connecting from a host language to an SQL database. SQL/CLI JDBC (Java Database Connectivity ) is a way to connect Java with an SQL database. PHP… 4

Two Ways to Connect SQL with a Host Program Host language + Embedded SQL Preprocessor Host language + Function calls Host-language compiler Object-code program SQL library CLI (Call-level interface) 5

6 All these methods follow the same paradigm 1.Connect to a DB server. 2.Say what database you want to use. 3.Assemble a string containing an SQL statement. 4.Get the DBMS to prepare a plan for executing the statement. 5.Execute the statement. 6.Extract the results into variables in the local programming language.

Embedded SQL 7

Use a preprocessor to turn SQL statements into procedure calls that fit with the host-language code surrounding. All embedded SQL statements begin with EXEC SQL, so the preprocessor can find them easily. Language constructs: –Connecting to a database: EXEC SQL CONNECT –Declaring variables: EXEC SQL BEGIN (END) DECLARE SECTION –Statements: EXEC SQL Statement; 8

Shared Variables To connect SQL and the host-language program, the two parts must share some variables. Declarations of shared variables are bracketed by: EXEC SQL BEGIN DECLARE SECTION; EXEC SQL END DECLARE SECTION; Always needed 9

Use of Shared Variables In SQL, the shared variables must be preceded by a colon. –They may be used as constants provided by the host-language program. –They may get values from SQL statements and pass those values to the host-language program. In the host language, shared variables behave like any other variable. 10

Extract Results Use SELECT-INTO for a query guaranteed to produce a single tuple. Otherwise, you have to use a cursor. 11

Example of C plus SQL: Looking Up the Price EXEC SQL BEGIN DECLARE SECTION; char theBar[21], theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; /* obtain values for theBar and theBeer */ theBar = “Blind Pig”; theBeer = “Bud”; EXEC SQL SELECT price INTO :thePrice FROM Sells WHERE bar = :theBar AND beer = :theBeer; /* do something with thePrice */ printf(“Price is %f\n”, thePrice); Note 21-char arrays needed for 20 chars + endmarker 12

Cursor Statements Declare a cursor c with: EXEC SQL DECLARE c CURSOR FOR ; Open and close cursor c with: EXEC SQL OPEN CURSOR c; EXEC SQL CLOSE CURSOR c; Fetch from c by: EXEC SQL FETCH c INTO ; –Macro NOT FOUND is true if and only if the FETCH fails to find a tuple. 13

Example with a Cursor EXEC SQL BEGIN DECLARE SECTION; char theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’; Compute a list of beer-price pairs at Joe’s bar 14 The cursor declaration goes outside the declare-section

Example with a Cursor EXEC SQL OPEN CURSOR c; while(1) { EXEC SQL FETCH c INTO :theBeer, :thePrice; if (NOT FOUND) break; /* format and print theBeer and thePrice */ } EXEC SQL CLOSE CURSOR c; The C style of breaking loops 15

Two special “error” variables: –SQLCODE (long, is negative if an error has occurred) –SQLSTATE (char[6], predefined codes for common errors) Example: if (NOT FOUND)… is indeed: if (!strcmp(SQLSTATE, “02000”)) break; No tuple found Example with a Cursor

Need for Dynamic SQL Most applications use specific queries and modification statements in their interaction with the database. –Thus, we can compile the EXEC SQL … statements into specific procedure calls and produce an ordinary host- language program that uses a library. What if the program is something like a generic query interface, that doesn’t know what it needs to do until it runs? 17

Dynamic SQL Preparing a query: EXEC SQL PREPARE FROM ; Executing a query: EXEC SQL EXECUTE ; “Prepare” = optimize query. Prepare once, execute many times. 18

Example: A Generic Interface EXEC SQL BEGIN DECLARE SECTION; char query[MAX_LENGTH]; EXEC SQL END DECLARE SECTION; while(1) { /* issue SQL> prompt */ /* read user’s query into array query */ EXEC SQL PREPARE q FROM :query; EXEC SQL EXECUTE q; } q is an SQL variable representing the optimized form of whatever statement is typed into :query 19

20 Example: Generic Interface Again EXEC SQL BEGIN DECLARE SECTION; char query[MAX_LENGTH]; EXEC SQL END DECLARE SECTION; while(1) { /* issue SQL> prompt */ /* read user’s query into array query */ EXEC SQL EXECUTE IMMEDIATE :query; } If we are only going to execute the query once, we can combine the PREPARE and EXECUTE steps into one

21 Dynamic SQL stst is an SQL variable; names the SQL statement tmpcrscodenum_enrolledtmp, crscode, num_enrolled are host language variables (note colon notation) crscodecrscode is an in parameter; supplies value for placeholder ( ? ) num_enrollednum_enrolled is an out parameter; receives value from C.NumEnrolled Course strcpy (tmp, “ SELECT C.NumEnrolled FROM Course C \ WHERE C.CrsCode = ? ” ) ; PREPARE EXEC SQL PREPARE st FROM :tmp ; EXECUTE EXEC SQL EXECUTE st INTO :num_enrolled USING :crs_code; placeholder

JDBC 22

JDBC Instead of using a preprocessor, we can use a library of functions and call them as part of a Java program. Java Database Connectivity (JDBC) is a library, with Java as the host language. –The library for C is called SQL/CLI = “Call-Level Interface.” 23

CENG 352 Database Management Systems JDBC: Architecture Four architectural components: –Application (initiates and terminates connections, submits SQL statements) –Driver manager (load JDBC driver) –Driver (connects to data source, transmits requests and returns/translates results and error codes) –Data source (processes SQL statements)

25 JDBC Run-Time Architecture DBMS application driver manager DB/2 driver SQLServer driver Oracle driver DB/2 database SQLServer database Oracle database

Data Structures Java connects to the database by structures of the following types: 1.Connections : logins to the database. 2.Statements : records that hold SQL statements to be passed to a connection. 3.Descriptions : records about tuples from a query or parameters of a statement. 26

Steps in JDBC Database Access 1.Import JDBC library (java.sql.*) 2.Load JDBC driver: Class.forname(“oracle.jdbc.driver.OracleDriver”) 3.Define appropriate variables 4.Create a connect object (via getConnection ) 5.Create a statement object from the Statement class: 1.PreparedStatement 2.CallableStatement 27

Steps in JDBC Database Access (continued) 6.Identify statement parameters 7.Bound parameters to program variables 8.Execute SQL statement (referenced by an object) via JDBC’s executeQuery 9.Process query results (returned in an object of type ResultSet ) –ResultSet is a 2-dimentional table 28

29 Initial steps import java.sql.*; -- import all classes in package java.sql Class.forName (driver name); // static method of class Class // loads specified driver //e.g. Class.forName(“oracle.jdbc.driver.Oracledriver”); Connection myCon = DriverManager.getConnection(Url, Id, Passwd); Static method of class DriverManager; attempts to connect to DBMS If successful, creates a connection object, myCon, for managing the connection Statement stat = con.CreateStatement (); Creates a statement object stat Statements have executeQuery() method

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke30 Statements  Three types:  Statement (both static and dynamic SQL statements)  PreparedStatement (semi-static SQL statements)  CallableStatment (stored procedures)  PreparedStatement class: Precompiled, parametrized SQL statements:  Structure is fixed  Values of parameters are determined at run-time

Creating Statements The Connection class has methods to create Statements and PreparedStatements. Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.prepareStatement( “SELECT beer, price FROM Sells” + “WHERE bar = ‘Joe’’s Bar’” ); Java trick: + concatenates strings. 31

Executing SQL Statements JDBC distinguishes queries from modifications, which it calls “updates.” Statement and PreparedStatement each have methods executeQuery and executeUpdate. –For Statements, these methods have one argument: the query or modification to be executed. –For PreparedStatements: no argument. 32

Example: Update stat1 is a Statement. We can use it to insert a tuple as: stat1.executeUpdate( “INSERT INTO Sells” + “VALUES(‘Brass Rail’, ‘Bud’, 3.00)” ); 33

Example: Query stat2 is a PreparedStatement holding the query “SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’”. executeQuery returns an object of class ResultSet The query: ResultSet Menu = stat2.executeQuery(); 34

Accessing the ResultSet An object of type ResultSet is something like a cursor. Method Next() advances the “cursor” to the next tuple. –The first time Next() is applied, it gets the first tuple. –If there are no more tuples, Next() returns the value FALSE. 35

Accessing Components of Tuples When a ResultSet is referring to a tuple, we can get the components of that tuple by applying certain methods to the ResultSet. Method getX (i ), where X is some type, and i is the component number, returns the value of that component. –The value must have type X. 36

Example: Accessing Components Menu is the ResultSet for the query “SELECT beer, price FROM Sells WHERE bar = ‘Joe’’s Bar’”. Access the beer and price from each tuple by: while ( Menu.Next() ) { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2); /* do something with theBeer and thePrice */ } 37

38 Transcript String query = “SELECT T.StudId FROM Transcript T” + “WHERE T.CrsCode = ? AND T.Semester = ?”; PreparedStatement ps = con.prepareStatement ( query ); Prepares the statement Creates a prepared statement object, ps, containing the prepared statement Placeholdersin Placeholders (?) mark positions of in parameters; special API is provided to plug the actual values in ? positions indicated by the ?’s Preparing and Executing a Query placeholders

39 Preparing and Executing a Query String crs_code, semester; ……… in ps.setString(1, crs_code); // set value of first in parameter in ps.setString(2, semester); // set value of second in parameter ResultSet res = ps.executeQuery ( ); Creates a result set object, res Executes the query Stores the result set produced by execution in res while ( res.next ( ) ) { // advance the cursor j = res.getInt (“StudId”); // fetch output int-value …process output value… }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke40 Matching Java and SQL Data Types getTimestamp()java.sql.TimeStampTIMESTAMP getTime()java.sql.TimeTIME getDate()java.sql.DateDATE getFloat()DoubleREAL getInt()IntegerINTEGER getDouble()DoubleFLOAT getDouble()DoubleDOUBLE getString()StringVARCHAR getString()StringCHAR getBoolean()BooleanBIT ResultSet get methodJava classSQL Type

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke41 JDBC: Exceptions and Warnings  Most of java.sql can throw and SQLException if an error occurs.  SQLWarning is a subclass of EQLException; not as severe (they are not thrown and their existence has to be explicitly tested)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke42 Warning and Exceptions (Contd.) try { stmt=con.createStatement(); warning=con.getWarnings(); while(warning != null) { // handle SQLWarnings; warning = warning.getNextWarning(): } con.clearWarnings(); stmt.executeUpdate(queryString); warning = con.getWarnings(); … } //end try catch( SQLException SQLe) { // handle the exception }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke43 Examining Database Metadata DatabaseMetaData object gives information about the database system and the catalog. DatabaseMetaData md = con.getMetaData(); // print information about the driver: System.out.println( “Name:” + md.getDriverName() + “version: ” + md.getDriverVersion());

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke44 Database Metadata (Contd.) DatabaseMetaData md=con.getMetaData(); ResultSet trs=md.getTables(null,null,null,null); String tableName; While(trs.next()) { tableName = trs.getString(“TABLE_NAME”); System.out.println(“Table: “ + tableName); //print all attributes ResultSet crs = md.getColumns(null,null,tableName, null); while (crs.next()) { System.out.println(crs.getString(“COLUMN_NAME” + “, “); }

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke45 A (Semi-)Complete Example Connection con = // connect DriverManager.getConnection(url, ”login", ”pass"); Statement stmt = con.createStatement(); // set up stmt String query = "SELECT name, rating FROM Sailors"; ResultSet rs = stmt.executeQuery(query); try { // handle exceptions // loop through result tuples while (rs.next()) { String s = rs.getString(“name"); Int n = rs.getFloat(“rating"); System.out.println(s + " " + n); } } catch(SQLException ex) { System.out.println(ex.getMessage () + ex.getSQLState () + ex.getErrorCode ()); }

46 Transactions in JDBC Default for a connection is –Transaction boundaries Autocommit mode: each SQL statement is a transaction. To group several statements into a transaction use con.setAutoCommit (false) –Isolation default isolation level of the underlying DBMS To change isolation level use con.setTransactionIsolationLevel ( TRANSACTION_SERIALIZABLE ) With autocommit off: –transaction is committed using con.commit(). –next transaction is automatically initiated (chaining) Transactions on each connection committed separately

SQL/PSM Procedures Stored in the Database 47

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke48 Stored Procedures  What is a stored procedure:  Program executed through a single SQL statement  Executed in the process space of the server  Advantages:  Can encapsulate application logic while staying “close” to the data  Reuse of application logic by different users  Avoid tuple-at-a-time return of records through cursors

Stored Procedures An extension to SQL, called SQL/PSM, or “persistent, stored modules,” allows us to store procedures as database schema elements. The programming style is a mixture of conventional statements (if, while, etc.) and SQL. Lets us do things we cannot do in SQL alone. Why? Performance. 49

Basic PSM Format CREATE PROCEDURE ( ) ; Function alternative: CREATE FUNCTION ( ) RETURNS Example: compute square footage of house lot; then you can query on it 50

Parameters in PSM Unlike the usual name-type pairs in languages like C, PSM uses mode-name-type triples, where the mode can be: –IN = procedure uses value, does not change value. –OUT = procedure changes, does not use. –INOUT = both. 51

Example: Stored Procedure Let’s write a procedure that takes two arguments b and p, and adds a tuple to Sells that has bar = ’Joe’’s Bar’, beer = b, and price = p. –Used by Joe to add to his menu more easily. 52

The Procedure CREATE PROCEDURE JoeMenu ( INbCHAR(20), IN pREAL ) INSERT INTO Sells VALUES(’Joe’’s Bar’, b, p); Parameters are both read-only, not changed The body --- a single insertion 53

Invoking Procedures Use SQL/PSM statement CALL, with the name of the desired procedure and arguments. Example: CALL JoeMenu(’Moosedrool’, 5.00); Functions used in SQL expressions where a value of their return type is appropriate. 54

Types of PSM statements -- 1 RETURN sets the return value of a function. –Unlike C, etc., RETURN does not terminate function execution. DECLARE used to declare local variables. BEGIN... END for groups of statements. –Separate by semicolons. 55

Types of PSM Statements -- 2 Assignment statements: SET = ; –Example: SET b = ’Bud’; Statement labels: give a statement a label by prefixing a name and a colon. 56

IF statements Simplest form: IF THEN END IF; Add ELSE if desired, as IF... THEN... ELSE... END IF; Add additional cases by ELSEIF : IF … THEN … ELSEIF … ELSEIF … ELSE … END IF; 57

Example: IF Let’s rate bars by how many customers they have, based on Frequents(drinker, bar). –<100 customers: ‘unpopular’. – customers: ‘average’. –>= 200 customers: ‘popular’. Function Rate(b) rates bar b. 58

59 Example: IF (continued) CREATE FUNCTION Rate (IN b CHAR(20) ) RETURNS CHAR(10) DECLARE cust INTEGER; BEGIN SET cust = (SELECT COUNT(*) FROM Frequents WHERE bar = b); IF cust < 100 THEN RETURN ’unpopular’ ELSEIF cust < 200 THEN RETURN ’average’ ELSE RETURN ’popular’ END IF; END; Number of customers of bar b Return occurs here, not at one of the RETURN statements Nested IF statement

Loops Basic form: LOOP END LOOP; Exit from a loop by: LEAVE The is associated with a loop by prepending the name and a colon to the keyword LOOP. 60

Example: Exiting a Loop loop1: LOOP... LEAVE loop1;... END LOOP; If this statement is executed... Control winds up here 61

Other Loop Forms WHILE DO END WHILE; REPEAT UNTIL END REPEAT; 62

Queries General SELECT-FROM-WHERE queries are not permitted in PSM. There are three ways to get the effect of a query: 1.Queries producing one value can be the expression in an assignment. 2.Single-row SELECT... INTO. 3.Cursors. 63

Example: Assignment/Query If p is a local variable and Sells(bar, beer, price) the usual relation, we can get the price Joe charges for Bud by: SET p = (SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’); 64

SELECT... INTO An equivalent way to get the value of a query that is guaranteed to return a single tuple is by placing INTO after the SELECT clause. Example: SELECT price INTO p FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’; 65

Cursors A cursor is essentially a tuple-variable that ranges over all tuples in the result of some query. Declare a cursor c by: DECLARE c CURSOR FOR ; 66

Opening and Closing Cursors To use cursor c, we must issue the command: OPEN c; –The query of c is evaluated, and c is set to point to the first tuple of the result. When finished with c, issue command: CLOSE c; 67

Fetching Tuples From a Cursor To get the next tuple from cursor c, issue command: FETCH FROM c INTO x1, x2,…,xn ; The x ’s are a list of variables, one for each component of the tuples referred to by c. c is moved automatically to the next tuple. 68

Breaking Cursor Loops -- 1 The usual way to use a cursor is to create a loop with a FETCH statement, and do something with each tuple fetched. A tricky point is how we get out of the loop when the cursor has no more tuples to deliver. 69

Breaking Cursor Loops -- 2 Each SQL operation returns a status, which is a 5-digit number. –For example, = “Everything OK,” and = “Failed to find a tuple.” In PSM, we can get the value of the status in a variable called SQLSTATE. 70

Breaking Cursor Loops -- 3 We may declare a condition, which is a boolean variable that is true if and only if SQLSTATE has a particular value. Example: We can declare condition NotFound to represent by: DECLARE NotFound CONDITION FOR SQLSTATE ’02000’; 71

Breaking Cursor Loops -- 4 The structure of a cursor loop is thus: cursorLoop: LOOP … FETCH c INTO … ; IF NotFound THEN LEAVE cursorLoop; END IF; … END LOOP; 72

Example: Cursor Let’s write a procedure that examines Sells(bar, beer, price), and raises by $1 the price of all beers at Joe’s Bar that are under $3. –Yes, we could write this as a simple UPDATE, but the details are instructive anyway. 73

The Needed Declarations CREATE PROCEDURE JoeGouge( ) DECLARE theBeer CHAR(20); DECLARE thePrice REAL; DECLARE NotFound CONDITION FOR SQLSTATE ’02000’; DECLARE c CURSOR FOR (SELECT beer, price FROM Sells WHERE bar = ’Joe’’s Bar’); Used to hold beer-price pairs when fetching through cursor c Returns Joe’s menu 74

The Procedure Body BEGIN OPEN c; menuLoop: LOOP FETCH c INTO theBeer, thePrice; IF NotFound THEN LEAVE menuLoop END IF; IF thePrice < 3.00 THEN UPDATE Sells SET price = thePrice+1.00 WHERE bar = ’Joe’’s Bar’ AND beer = theBeer; END IF; END LOOP; CLOSE c; END; Check if the recent FETCH failed to get a tuple If Joe charges less than $3 for the beer, raise it’s price at Joe’s Bar by $1. 75

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke76 Calling Stored Procedures (Contd.) JDBC: CallableStatement cstmt= con.prepareCall(“{call ShowSailors}); ResultSet rs = cstmt.executeQuery(); while (rs.next()) { … }

Statements JDBC provides two classes: 1.Statement = an object that can accept a string that is an SQL statement and can execute such a string. 2.PreparedStatement = an object that has an associated SQL statement ready to execute. 77

The Three-Tier Architecture of Database Applications browser network HTTP Web server Application server Database server database Server Forms & Buttons Your business logic is executed here Display query result Client & Server Interaction 78