M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004
M.P. Johnson, DBMS, Stern/NYU, Sp Agenda Previously: JDBC Next: Project part 3 due now Project part 4 due next week Scripting for SQL SPs Scripting for SQL on the web CGI/Perl PHP Security
M.P. Johnson, DBMS, Stern/NYU, Sp Recap: JDBC Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C javac + jar prog.pc Prog.java Proj.class
M.P. Johnson, DBMS, Stern/NYU, Sp Java & parameter-based SQL Like SQL/CLI in C, Java also supports parameterized queries (why?) 1. Prepare structure of query 2. Then can set values 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();...
M.P. Johnson, DBMS, Stern/NYU, Sp Also: ODBC Used by Microsoft platforms/tools, others Access: Start | Control Panel | Administrative Tools | Data Sources (ODBC) Similar to JDBC Won’t cover
M.P. Johnson, DBMS, Stern/NYU, Sp Other combinations So far: C/Pro*C, Java/JDBC Q: Only choices? A: No “Call-level interface” for C: SQL/CLI ODBC Embedded Java: SQL/J CLI for Perl, PHP, etc. Stored Procedures (next) {langs} x {dyn/not} x {SPs/not} x {DBMSs}
M.P. Johnson, DBMS, Stern/NYU, Sp Step back Recall basic problem: need SQL plus stronger programming lang need to connect the two langs In all these case (and web apps next time): put SQL in (traditional lang) programs Another way: let programs in SQL i.e., put programs in the DBMS “stored procedures”
M.P. Johnson, DBMS, Stern/NYU, Sp Next topic: SPs (8.2) “Persistent, Stored Modules” / “Stored Procedures / “PL/SQL programs” (in Oracle) Added to MySQL in 5.0 Another way to connect application programming language and SQL Supports usual things: Declare, set vars to vals of expressions Print output Define (optional) procedures, functions Cursors PL/SQL can compute n!
M.P. Johnson, DBMS, Stern/NYU, Sp PL/SQL “Procedural Language/SQL” Oracle’s language for stored procedures Simple, interpreted, procedural language But Pascal-like: BEGIN END, not { } AND OR, not && || vars defined at top of procedre how return works
M.P. Johnson, DBMS, Stern/NYU, Sp PL/SQL Generally speaking can be used wherever SQL can be sqlplus embeded SQL Can store programs in files (.sql), run later runs code in myprog.sql
M.P. Johnson, DBMS, Stern/NYU, Sp Scripting languages Big problems v. small problems Big solutions v. small solutions Programming languages: C/C++, Java, etc. Scripting languages: PL/SQL, Perl, PHP, Unix shell, DOS batch files, Python, Excel macros, VBA, JavaScript Usual properties of scripting languages: Interpreted Don’t require functions/procedures Weakly typed
M.P. Johnson, DBMS, Stern/NYU, Sp PL/SQL Structure of procedure body: As in Pascal, var declars precede body DECLARE --Optional --var declarations BEGIN --executable statements --queries/updates, etc. END; /--to execute DECLARE --Optional --var declarations BEGIN --executable statements --queries/updates, etc. END; /--to execute
M.P. Johnson, DBMS, Stern/NYU, Sp PL/SQL: Hello, World SET SERVEROUTPUT ON; BEGIN -- print out message DBMS_OUTPUT.PUT_LINE('Hello World, from PL/SQL'); END; / SET SERVEROUTPUT ON; BEGIN -- print out message DBMS_OUTPUT.PUT_LINE('Hello World, from PL/SQL'); END; /
M.P. Johnson, DBMS, Stern/NYU, Sp PL/SQL code examples One example: Likes(drinker, beverage) Another example: BEGIN INSERT INTO Likes VALUES(‘Izzy', ‘milk'); DELETE FROM Likes WHERE drinker = ‘Izzy' AND beverage = ‘Beaujolais Nouveau '; COMMIT; END; / BEGIN INSERT INTO Likes VALUES(‘Izzy', ‘milk'); DELETE FROM Likes WHERE drinker = ‘Izzy' AND beverage = ‘Beaujolais Nouveau '; COMMIT; END; /
M.P. Johnson, DBMS, Stern/NYU, Sp Procedures Stored database objects that use a PL/SQL statement(s) in their body Create/drop similar to other SQL objects: ALTER PROCEDURE… in MySQL CREATE PROCEDURE ( ) ; CREATE PROCEDURE ( ) ; DROP PROCEDURE ; CREATE OR REPLACE PROCEDURE ( ) ; CREATE OR REPLACE PROCEDURE ( ) ;
M.P. Johnson, DBMS, Stern/NYU, Sp Example procedure Define the procedure: Now we can call it: CREATE PROCEDURE testProcedure BEGIN INSERT INTO Student VALUES (5, ‘Joe’); COMMIT; END; CREATE PROCEDURE testProcedure BEGIN INSERT INTO Student VALUES (5, ‘Joe’); COMMIT; END; EXEC testProcedure;
M.P. Johnson, DBMS, Stern/NYU, Sp More details on procedures Parameter list has name-mode-type triples: Modes: IN, OUT, or IN OUT Fulfills role similar to pass-by-value v. pass-by- reference Default is IN Types must match, so can get exact field type: relation.attribute%TYPE
M.P. Johnson, DBMS, Stern/NYU, Sp Procedure example A procedure to take a beer and price and add it to Joe's menu: Sells(bar, beer, price) CREATE PROCEDURE joeMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE) AS BEGIN INSERT INTO Sells VALUES('Joe''s Bar', b, p); END; / CREATE PROCEDURE joeMenu( b IN Sells.beer%TYPE, p IN Sells.price%TYPE) AS BEGIN INSERT INTO Sells VALUES('Joe''s Bar', b, p); END; /
M.P. Johnson, DBMS, Stern/NYU, Sp Branching IF–THEN statements use THEN Must end with END IF Use ELSIF in place of ELSE IF Example: IF THEN ELSIF END IF; IF THEN ELSIF END IF;
M.P. Johnson, DBMS, Stern/NYU, Sp Loop example DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END; / DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END; /
M.P. Johnson, DBMS, Stern/NYU, Sp Cursors in PL/SQL As expected, PL/SQL has syntax to do the usual things: Declare cursors Open and close Fetch and eventually leave Each can be done manually Also has elegant for/cursor loop Declare, open, close, fetch all automatic: Example: FOR my-rec IN my-cursor LOOP … END LOOP; FOR my-rec IN my-cursor LOOP … END LOOP;
M.P. Johnson, DBMS, Stern/NYU, Sp Functions Like procedures but with return values Big strength: can be called from SQL CREATE FUNCTION ( ) RETURNS type AS BEGIN END; CREATE FUNCTION ( ) RETURNS type AS BEGIN END; DROP FUNCTION ;
M.P. Johnson, DBMS, Stern/NYU, Sp Function example Like procedures but with return values drop in same way Big strength: can be called from SQL CREATE OR REPLACE FUNCTION maxval(a IN int, b IN int) RETURN int AS BEGIN IF a > b THEN RETURN a; ELSE RETURN b; END IF; END maxval; / CREATE OR REPLACE FUNCTION maxval(a IN int, b IN int) RETURN int AS BEGIN IF a > b THEN RETURN a; ELSE RETURN b; END IF; END maxval; / INSERT INTO R VALUES(“abc”, maxval(5,10));
M.P. Johnson, DBMS, Stern/NYU, Sp How to run scripts Don’t want to type ftns into sqlplus by hand Define them in a.sql file In sqlplus, execute.sql file Runs commands in file Here, defines function Now, we can call functions See SQL> exec DBMS_OUTPUT.PUT_LINE (maxval(5,10))
M.P. Johnson, DBMS, Stern/NYU, Sp Triggers in Oracle Oracle triggers are written in PL/SQL Trigger body is like regular procedure body, but following trigger syntax: CREATE OR REPLACE TRIGGER MYTRIG1 BEFORE DELETE ON mytable BEGIN --code END; CREATE OR REPLACE TRIGGER MYTRIG1 BEFORE DELETE ON mytable BEGIN --code END;
M.P. Johnson, DBMS, Stern/NYU, Sp Look up procedures, functions In Oracle, functions & procedures in user_procedures: SELECT object_name from user_procedures;
M.P. Johnson, DBMS, Stern/NYU, Sp More on PL/SQL O’Reilly’s Oracle PL/SQL Programming: PL/SQL Reference & Tutorial: Introduction to PL/SQL: ml ml Oracle FAQ's Script and Code Exchange: