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

Slides:



Advertisements
Similar presentations
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Advertisements

CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
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.
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.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #19 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Programs with SQL Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Host language program Preprocessor Host.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
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.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
Dec 11, 2003Murali Mani SQL with other Programming Languages B term 2004: lecture 16.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 10g: SQL & PL/SQL Session #6 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
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.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
Advanced SQL: Cursors & Stored Procedures
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
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.
Phase 2, Answering queries using views. February 2 nd, 2004.
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.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
© D. Wong Java API for XML Processing (JAXP)  For processing XML data using applications written in Java  JAXP APIs - javax.xml.parsers package.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
SQL Programming SQL in Application Programs
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
1 Lecture 8: SQL Programming and Transactions Friday, January 24, 2003.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Chapter 4 An Introduction to SQL.
SQL Environment.
Introduction to Database Systems, CS420
CMSC-461 Database Management Systems
Remaining Topics in SQL to be covered…
CPSC-310 Database Systems
Lecture 06: SQL Systems Aspects
Unit I-2.
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 17: Systems Aspects of SQL
Lecture 14: XML Publishing & Storage
Lecture 05: SQL Systems Aspects
Embedded SQL Chapter 8.
Presentation transcript:

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, Sp Agenda Last time: Views, Constraints, Triggers This time: Begin programming for SQL  Embedded SQL Pro*C, SQLJ  PSMs  CLI SQL/CLI in C JDBC in Java DBI/DBDs in Perl Future:  Midterm on Thursday

M.P. Johnson, DBMS, Stern/NYU, Sp Future Next: PSMs  (Re)read 8.2 Next Thursday: Midterm  Practice SQL on sqlzoo Responsible (in this order) for 1. all material from class, 2. all material assigned reading Feedback is very important! Please fill out midterm course evals before MT  Else lose 30 points on MT  Details TBA by over week

M.P. Johnson, DBMS, Stern/NYU, Sp New topic: SQL Programming (8.1) 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

M.P. Johnson, DBMS, Stern/NYU, Sp Using two languages Q: Why not use just one language? “[W]e can quickly dispense with the idea…” (p351) Q: Why not do everything in the host lang.? A: What SQL provides is highly non-trivial  Query interpretation, optimizing  Queries stay constant across host languages Q: Why not do everything in SQL? A: Not designed as a general-purpose language  No recursion (no factorial!)  No, e.g., Swing library Germ of OO: modularize

M.P. Johnson, DBMS, Stern/NYU, Sp 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

M.P. Johnson, DBMS, Stern/NYU, Sp SQL/host interface in embedded SQL So Q: how to transfer data between? A: Shared variables  Some vars in are 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 a C (e.g.) program with library calls

M.P. Johnson, DBMS, Stern/NYU, Sp Programs with Embedded SQL Host language + Embedded SQL Preprocessor Host Language + function calls Host language compiler Executable Preprocessor Host language compiler Oracle’s Pro*C gcc prog.pc prog.c a.out

M.P. Johnson, DBMS, Stern/NYU, Sp 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;

M.P. Johnson, DBMS, Stern/NYU, Sp 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

M.P. Johnson, DBMS, Stern/NYU, Sp Embedded SQL example: insert void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; /* product-name, company-name */ int p, 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 (:n, :p, :q, :c); } void simpleInsert() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; /* product-name, company-name */ int p, 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 (:n, :p, :q, :c); }

M.P. Johnson, DBMS, Stern/NYU, Sp Embedded SQL example: look-up int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = ‘Windows’; return p; } int getWindowsPrice() { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQLSELECT price INTO :p FROM Product WHERE Product.name = ‘Windows’; return p; }

M.P. Johnson, DBMS, Stern/NYU, Sp 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 SQLSELECT price INTO :p FROM Product WHERE 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 SQLSELECT price INTO :p FROM Product WHERE Product.name = :name; return p; }

M.P. Johnson, DBMS, Stern/NYU, Sp Embedded SQL example: look-up 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 SQLSELECT price INTO :p FROM Product WHERE 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 SQLSELECT price INTO :p FROM Product WHERE Product.name = :n; return p; }

M.P. Johnson, DBMS, Stern/NYU, Sp Cursors 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 cursor 2. Open the cursor 3. Fetch tuples one by one 4. Close the cursor

M.P. Johnson, DBMS, Stern/NYU, Sp 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”)) if(NO_MORE_TUPLES) break;

M.P. Johnson, DBMS, Stern/NYU, Sp Multiple-row look-up example void product2XML() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; int p, 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 product2XML() { EXEC SQL BEGIN DECLARE SECTION; char n[20], c[30]; int p, 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;...

M.P. Johnson, DBMS, Stern/NYU, Sp Multiple look-up example printf(“ \n”); while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c; if (NO_MORE_TUPLES) break; printf(“ \n”); printf(“ %s \n”, n); printf(“ %d \n”, p); printf(“ %d \n”, q); printf(“ %s \n”, c); printf(“ \n”); } EXECT SQL CLOSE crs; printf(“ \n”); } printf(“ \n”); while (1) { EXEC SQL FETCH FROM crs INTO :n, :p, :q,:c; if (NO_MORE_TUPLES) break; printf(“ \n”); printf(“ %s \n”, n); printf(“ %d \n”, p); printf(“ %d \n”, q); printf(“ %s \n”, c); printf(“ \n”); } EXECT SQL CLOSE crs; printf(“ \n”); }

M.P. Johnson, DBMS, Stern/NYU, Sp Cursor on query not table EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = 'Joe''s Bar'; EXEC SQL OPEN CURSOR c; while(1) { EXEC SQL FETCH c INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */ } EXEC SQL CLOSE CURSOR c; EXEC SQL DECLARE c CURSOR FOR SELECT beer, price FROM Sells WHERE bar = 'Joe''s Bar'; EXEC SQL OPEN CURSOR c; while(1) { EXEC SQL FETCH c INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */ } EXEC SQL CLOSE CURSOR c;

M.P. Johnson, DBMS, Stern/NYU, Sp More on Cursors 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) Cursors can traverse both stored tables and queries

M.P. Johnson, DBMS, Stern/NYU, Sp Modifications with cursors As we traverse through result set, can modify the current row NB: In regular SQL, usually modify sets of rows (UPDATE WHERE …) With cursors, we update the last row fetched Simple example: in Product table, we decide we want  to raise (i.e., double) all our prices  Unless price < 100, in which case they’re deleted

M.P. Johnson, DBMS, Stern/NYU, Sp Modification by cursor example void doublePrices() { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM Product; EXEC SQL OPEN crs;... void doublePrices() { EXEC SQL BEGIN DECLARE SECTION; int p; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM Product; EXEC SQL OPEN crs;...

M.P. Johnson, DBMS, Stern/NYU, Sp Modification by cursor example while (1) { EXEC SQL FETCH FROM crs INTO :p; if (NO_MORE_TUPLES) break; if (p < 100) EXEC SQL DELETE FROM Product WHERE CURRENT OF Product; else EXEC SQL UPDATE Product SET price = 2*price; WHERE CURRENT OF Product; } EXECT SQL CLOSE crs; } while (1) { EXEC SQL FETCH FROM crs INTO :p; if (NO_MORE_TUPLES) break; if (p < 100) EXEC SQL DELETE FROM Product WHERE CURRENT OF Product; else EXEC SQL UPDATE Product SET price = 2*price; WHERE CURRENT OF Product; } EXECT SQL CLOSE crs; }

M.P. Johnson, DBMS, Stern/NYU, Sp A mention of concurrent access What if the DB changes while our cursor is looping?  I.e., after we opened the cursor, but while we’re fetching Should we see the changes? Maybe, maybe not make these changes invisible by declaring insensitive Q: How is this accomplished? One crude way: delay any changes until all insensitive cursors close Good idea: indicate read-only cursors so they won’t be held up: EXEC SQL DECLARE crs INSENSITIVE CURSOR FOR SELECT price FROM Product; EXEC SQL DECLARE crs INSENSITIVE CURSOR FOR SELECT price FROM Product; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM Product; FOR READ ONLY; EXEC SQL DECLARE crs CURSOR FOR SELECT price FROM Product; FOR READ ONLY;

M.P. Johnson, DBMS, Stern/NYU, Sp Scrolling cursors Usually cursor just walks through rows 1 by 1 Other options:  NEXT (default) or PREVIOUS  FIRST or LAST  RELATIVE +/-n RELATIVE 1 ~ NEXT RELATIVE –1 ~ ?  ABSOLUTE +/-n ABSOLUTE 1 ~ FIRST (not 0!) ABSOLUTE –1 ~ LAST To use these, declare as SCROLL cursor EXEC SQL DECLARE crs SCROLL CURSOR FOR Product; EXEC SQL DECLARE crs SCROLL CURSOR FOR Product;

M.P. Johnson, DBMS, Stern/NYU, Sp Pro*C on sales Example script: sample1.pc  See Pro*C tutorialPro*C tutorial Pro*C compiler is proc  Must include /oracle/precomp/public  Must link with shared library /oracle/lib/libclntsh.so  Includes makefile proc.mk, but may require modifications sales% cp /oracle/precomp/demo/proc/sample1.pc. sales% proc sample1.pc sales% gcc -osample1 -I/oracle/precomp/public /oracle/lib/libclntsh.so sample1.c sales% sample1 sales% cp /oracle/precomp/demo/proc/sample1.pc. sales% proc sample1.pc sales% gcc -osample1 -I/oracle/precomp/public /oracle/lib/libclntsh.so sample1.c sales% sample1

M.P. Johnson, DBMS, Stern/NYU, Sp Indeterminate-speed review Look at integrity violations in Go over some R.A./SQL questions from hw…  SQL questions located at qlquestions.txt qlquestions.txt  Some “solutions” available at olns.txt olns.txt  Test by running at