UJeffrey D. Ullman uAnfang von: CS145 - Herbst 2004 - Stanford University uOnline unter: www.odbms.org www.odbms.org Folien mit weißem Hintergrund wurden.

Slides:



Advertisements
Similar presentations
Copyright © 2004 Pearson Education, Inc.. Chapter 9 More SQL: Assertions, Views, and Programming Techniques.
Advertisements

1 Database-Connection Libraries Call-Level Interface Java Database Connectivity PHP.
Chapter 9 SQL in a Server Environment Call-Level Interface Java Database Connectivity PHP.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
1 Combining SQL and Conventional Programming Languages Source: slides by Jeffrey Ullman.
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.
1 Chapter 7 System Aspects of SQL uSQL in a Programming Environment uTransactions uAuthorization.
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, Spring C : Database Management Systems Lecture #16 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Murali Mani SQL with other Programming Languages.
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 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,
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,
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
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.
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.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
1 SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity Persistent Stored Modules.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 SQL/PSM Procedures Stored in the Database General-Purpose Programming.
CS411 Database Systems Kazuhiro Minami 07: SQL System Aspects.
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)
Winter 2006Keller, Ullman, Cushing10–1 Modification to Views Via Triggers Oracle allows us to “intercept” a modification to a view through an instead-of.
Advanced SQL: Cursors & Stored Procedures
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
1 Real SQL Programming Embedded SQL Java Database Connectivity Stored Procedures.
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.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
1 Real SQL Programming - 2 Persistent Stored Modules (PSM) PL/SQL Embedded SQL These slides are reused from Jeffrey Ullman’s class with the author’s permission.
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.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
© 1997 UW CSE 10/27/97H-1 Embedded SQL and the CLI Chapter 7.7.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL.
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.
1 Real SQL Programming Persistent Stored Modules (PSM) PL/SQL Embedded SQL (modified 11/11/2013)
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.
SQL in the real world 1. The SQL Client-Server Interactions SQL-clientSQL-server Environment SQL-agent Module Connection Session Current catalog and schema.
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.
1 Lecture 11 PL/SQL Slides from
SQL Environment.
Introduction to Database Systems, CS420
CMSC-461 Database Management Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 17: Systems Aspects of SQL
Database-Connection Libraries
Lecture 05: SQL Systems Aspects
Embedded SQL Chapter 8.
Presentation transcript:

uJeffrey D. Ullman uAnfang von: CS145 - Herbst Stanford University uOnline unter: Folien mit weißem Hintergrund wurden hinzugefügt!

2 Real SQL Programming Embedded SQL Call-Level Interface Java Database Connectivity

3 SQL in Real Programs uWe have seen only how SQL is used at the generic query interface --- an environment where we sit at a terminal and ask queries of a database. uReality is almost always different. wPrograms in a conventional language like C are written to access a database by “calls” to SQL statements.

4 Host Languages uAny conventional language can be a host language, that is, a language in which SQL calls are embedded. uThe use of a host/SQL combination allows us to do anything computable, yet still get the very-high-level SQL interface to the database.

5 Kommt man auch ohne host language aus? Abhängig vom SQL Standard wTheorie sagt: Mit “if” (Bedingung) und “while” (Schleife) kriege ich alles hin! wSQL92 bietet dies nicht  z.B. Berechnung der Fakultät nicht möglich (n!). wIBM DB2 erlaubt mitlerweile rekursives SQL (nach SQL3) w… wie auch immer, host language bietet viele Vorteile!

6 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 C to an SQL database. 3.JDBC (Java Database Connectivity ) is a way to connect Java with an SQL database.

7 Bemerkungen u CLI beschreibt wie aufgerufen wird, es ist nicht auf C beschränkt! u JDBC basiert ebenfalls auf CLI  ist eine Umsetzung des CLI in Java Mehr hierzu in der nächsten Vorlesung!

8 Embedded SQL uKey idea: Use a preprocessor to turn SQL statements into procedure calls that fit with the host-language code surrounding. uAll embedded SQL statements begin with EXEC SQL, so the preprocessor can find them easily.

9 Ausführbares Programm Linker Compiler Precompiler Quelltext mit SQL-Befehlen Quelltext in reiner host language Objektdatei SQL LIB STD LIB

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

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

12 Example: Looking Up Prices uWe’ll use C with embedded SQL to sketch the important parts of a function that obtains a beer and a bar, and looks up the price of that beer at that bar. uAssumes database has our usual Sells(bar, beer, price) relation.

13 Example: C Plus SQL EXEC SQL BEGIN DECLARE SECTION; char theBar[21], theBeer[21]; float thePrice; EXEC SQL END DECLARE SECTION; /* obtain values for theBar and theBeer */ EXEC SQL SELECT price INTO :thePrice FROM Sells WHERE bar = :theBar AND beer = :theBeer; /* do something with thePrice */ Note 21-char arrays needed for 20 chars + endmarker SELECT-INTO just like PSM

14 Embedded Queries uEmbedded SQL has the same limitations as PSM regarding queries: wYou may use SELECT-INTO for a query guaranteed to produce a single tuple. wOtherwise, you have to use a cursor. Small syntactic differences between PSM and Embedded SQL cursors, but the key ideas are identical.

15 Cursor Statements uDeclare a cursor c with: EXEC SQL DECLARE c CURSOR FOR ; uOpen and close cursor c with: EXEC SQL OPEN CURSOR c; EXEC SQL CLOSE CURSOR c; uFetch from c by: EXEC SQL FETCH c INTO ; wMacro NOT FOUND is true if and only if the FETCH fails to find a tuple.

16 Example --- (1) uLet’s write C + SQL to print Joe’s menu --- the list of beer-price pairs that we find in Sells(bar, beer, price) with bar = Joe’s Bar. uA cursor will visit each Sells tuple that has bar = Joe’s Bar.

17 Example --- (2: Declarations) 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’; The cursor declaration goes outside the declare-section

18 Example --- (3: Executable) 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