M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.

Slides:



Advertisements
Similar presentations
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Advertisements

PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
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.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #15 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #17 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Murali Mani SQL with other Programming Languages.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #18 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Winter 2002Arthur Keller – CS 18010–1 Schedule Today: Feb. 5 (T) u Triggers, PL/SQL. u Read Sections 7.4, 8.2. Assignment 4 due. Feb. 7 (TH) u PL/SQL,
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 10g: SQL & PL/SQL Session #7 Matthew P. Johnson CISDD, CUNY Fall, 2004.
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.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
1 PL/SQL Oracle’s Version of Triggers and PSM. 2 PL/SQL uOracle uses a variant of SQL/PSM which it calls PL/SQL. uPL/SQL not only allows you to create.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #21 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #14 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY June, 2005.
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Embedded SQL Host Language (record-oriented) DBMS (set-oriented) 1. Query 3. Process a tuple at a time 4. Close Cursor 2. Evaluate query. Provide cursor.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
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.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Stored Procedures, Triggers, Program Access Dr Lisa Ball 2008.
Winter 2006Keller, Ullman, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Advanced SQL: Cursors & Stored Procedures
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #8 Matthew P. Johnson CISDD, CUNY June, 2004.
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.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
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.
Stored Procedures. Definition a stored procedure is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database.
1 More basics on DB access Elke A. Rundensteiner.
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.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
© 1997 UW CSE 10/27/97H-1 Embedded SQL and the CLI Chapter 7.7.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #7 Matthew P. Johnson CISDD, CUNY June, 2005.
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.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
Introduction to Database Programming with Python Gary Stewart
1 Lecture 11 PL/SQL Slides from
Introduction to Database Systems, CS420
CMSC-461 Database Management Systems
Difference between Oracle PL/SQL and MySQL
Database Application Development
CPSC-310 Database Systems
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Database Application Development
Presentation transcript:

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: 