SQL in a Programming Environment CIS 4301 Lecture Notes Lecture 22 - 4/11/2006.

Slides:



Advertisements
Similar presentations
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Advertisements

Chapter 8 Embedded SQL.
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.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
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.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Basic Elements of C++ Chapter 2.
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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.
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.
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)
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
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.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
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.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
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.
C LANGUAGE Characteristics of C · Small size
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.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Chapter 7: Constraints and Triggers Foreign Keys Local and Global Constraints Triggers 1.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 System Aspects of SQL. 2 Binding Styles SQL2 implementations are required to support at least one of the following seven host languages: ADA, C, Cobol,
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
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.
1st Semester, 2017 Sanghyun Park
SQL Environment.
Introduction to Database Systems, CS420
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Revision Lecture
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Basic Elements of C++ Chapter 2.
CPSC-310 Database Systems
Database Application Development
Unit I-2.
Persistent Stored Modules (PSM) PL/SQL Embedded SQL
Lecture 17: Systems Aspects of SQL
Lecture 05: SQL Systems Aspects
Presentation transcript:

SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006

Lecture 22© CIS Spring SQL in a Program You're starting by using "direct" (or "interactive") SQL In reality SQL is rarely used in this way, instead it's specified within programs Host language (e.g., ADA, C, Cobol, Fortran, M, Pascal) Some of the steps are actually SQL statements Entire program sent to preprocessor, changes the embedded SQL into something that makes sense in host language Pre-processed program is then compiled in usual manner DBMS vendor normally provides library that supplies necessary function definitions Call-level interface (CLI)

Lecture 22© CIS Spring Processing Programs with SQL Statements Host Language + Embedded SQL Preprocessor Host Language + Function Calls Host-language compiler Object-code program SQL library

Lecture 22© CIS Spring Impedance Mismatch Why not use a single language SQL? Conventional language? Many things SQL cannot do… Example? Also, conventional progr. language not suitable for letting humans write database operations that can be executed efficiently… Why not? Need both, but must deal with disconnect between the data models used Rel. model uses sets, not naturally supported by programming languages Programming lang. support pointers, loops, branches, not available in SQL Passing data back and forth between SQL and other prog. languages is not straightforward

Lecture 22© CIS Spring SQL/Host Language Interface How are values passed from the program into SQL commands? How are results of SQL commands returned into program variables? How do we deal with set-valued (i.e., relation-valued) results? Shared variables Variables of the host languages DECLARE Section Identify in program by preceding variable with colon Special variable SQLSTATE connects the host language with the SQL execution system ‘00000’  no error ‘02000’  no tuple found

Lecture 22© CIS Spring The Declare Section EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION;

Lecture 22© CIS Spring Embedded SQL SQL statements that do not return a result can be embedded directly into a host language using EXEC SQL … syntax EXEC SQL INSERT INTO Studio (name, address) VALUES (:studioName, :studioAddress);

Lecture 22© CIS Spring Sample C Function void getStudio() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50], studioAddr[256]; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* code for handling user input… */ EXEC SQL INSERT INTO STUDIO(name, address) VALUES (:studioName, :studioAddr); }

Lecture 22© CIS Spring Embedded SQL So far, statements return no results No impedance mismatch – EASY SFW queries must use one of two mechanisms Single-row select using shared variables to hold components of single tuple being returned Queries producing more than one tuple must declare cursor

Lecture 22© CIS Spring Single-Row Select Statement Void printNetWorth() { EXEC SQL BEGIN DECLARE SECTION; char studioName[50]; int presNetWorth; char SQLSTATE[6]; EXEC SQL END DECLARE SECTION; /* print request that studio name be entered, read response into studioName */ EXEC SQL SELECT netWorth INTO :presNetWorth FROM Studio, MovieExec WHERE presC# = cert# AND Studio.name = :studioName; /* print value of presNetWorth */ } Declaration of shared vars

Lecture 22© CIS Spring Cursors For set-valued results: associate handle (cursor) with query, get tuples in result one at a time through handle Example: Print names and GPAs of all students who applied to Santa Cruz Step 1: declare cursor EXEC BEGIN DECLARE SECTION; char n[30]; float g; EXEC END DECLARE SECTION; EXEC DECLARE Scinfo CURSOR FOR (SELECT DISTINCT name, GPA FROM Student, Apply WHERE Student.ID = Apply.ID AND Apply.location = "Santa Cruz");

Lecture 22© CIS Spring General Cursor Declaration EXEC SQL DECLARE CURSOR FOR

Lecture 22© CIS Spring Using a Cursor Step 2: run query using iteration EXEC SQL OPEN SCinfo;  initializes cursor EXEC SQL FETCH FROM SCinfo INTO :n, :g; WHILE (MORE_TUPLES) Print(:n, :g); EXEC SQL FETCH SCinfo INTO :n,:g; ENDWHILE; EXEC SQL CLOSE SCinfo;  closes cursor #define MORE_TUPLES !(strcmp(SQLSTATE,”02000”))

Lecture 22© CIS Spring General Fetch Statement EXEC SQL FETCH FROM INTO

Lecture 22© CIS Spring Other Uses of Cursor Cursor can be used to modify or delete tuples See example in FCDBS

Lecture 22© CIS Spring Scrolling Cursors Default movement through cursor is forward, one tuple at a time Option to move in different directions When declaring cursor, declare cursor as scrollable Use FETCH in conjunction with direction keyword NEXT or PRIOR FIRST or LAST RELATIVE ABSOLUTE

Lecture 22© CIS Spring Dynamic SQL So far, model was to embed specific SQL queries and commands within host-language program Alternatively, can have host language compute and assemble SQL statements Since not known at compile time, cannot be handled by SQL preprocessor or host-language compiler Dynamic SQL Example? Host-language program must instruct SQL system to take char string, turn it into executable SQL statement, and finally to execute that statement

Lecture 22© CIS Spring A very simple sqlplus Void vssqlplus () { EXEC SQL BEGIN DECLARE SECTION; char *query; EXEC SQL END DECLARE SECTION; /* prompt user for a query, allocate space and make shared variable :query point to first char of query */ EXEC SQL PREPARE SQLquery FROM :query; EXEC SQL EXECUTE SQLquery; /* code for displaying result to user */ } EXEC SQL EXECUTE IMMEDIATE :query;