Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.

Slides:



Advertisements
Similar presentations
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
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 Minggu 6, Pertemuan 11 Programmatic SQL Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
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.
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.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke; edited K. Shomper1 Database Application Development Chapter 6.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
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 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 HW3 Oct. 2 to –Available Oct 2 only until 2 pm.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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)
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Converting To Anthony Tichonoff Florida Hospital MIS January 2007 With Ease Multi-Row Fetch.
Advanced SQL: Cursors & 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.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
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.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
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.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Chapter 8 Embedded SQL.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
In this module, we will design a program to work with the PROJECT table, learning to insert a new project into the table, to delete one or more projects.
© 1997 UW CSE 10/27/97H-1 Embedded SQL and the CLI Chapter 7.7.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
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.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Chapter 4 An Introduction to SQL.
1st Semester, 2017 Sanghyun Park
Introduction to SQL Programming Techniques
SQL Environment.
Introduction to Database Systems, CS420
A Guide to SQL, Seventh Edition
Handling Exceptions.
CPSC-310 Database Systems
Embedded DB2.
Lecture 17: Systems Aspects of SQL
Chapter 8 Advanced SQL.
Presentation transcript:

Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL

Lu Wei2 Embeded SQL Introduction SQL Communications Area Host Language Varibles Cursors Static Embedded SQL Statements Dynamic SQL ODBC

Lu Wei3 Introduction We mentioned in chapter 3 that SQL can be used in two ways: –Use SQL interactively by entering the statements at a terminal –Embed SQL statements in a procedural language (programmatic SQL)

Lu Wei4 Introduction In fact, we can distinguish between two types of programmic SQL –Embeded SQL statements SQL statements are embeded directly into the program source code and mixed with the host language statements. Need to be precompiled. –Application Programming Interface (API) Provide the programmer with a standard set of functions that can be invoked from the software. Need not to be precompiled. Open Database Connectivity (ODBC) standard.

Lu Wei5 SQL Communications Area The DBMS uses an SQL Communications Area (SQLCA) to report runtime errors to the application program. SQLCA is a data structure that contains error variables and status indicators. An application program can examine the SQLCA to determine the success or failure of each SQL statement.

Lu Wei6 SQL Communications Area Definition of the SQLCA for Oracle. /** NAME SQLCA: SQL Communication Area. FUNCTION Contains no code. Oracle fills in the SQLCA with status info during the execution of an SQL statement. **/

Lu Wei7 SQL Communications Area struct sqlca{ char sqlcaid[8]; //contains fixed text “SQLCA” long sqlcabc; //length of SQLCA structure long sqlcode; //SQL return code struct{ short sqlerrml; //length of error message char sqlerrmc[70]; //text of error message } sqlerrm; char sqlerrp[8]; //reserved for future use long sqlerrd[6]; //sqlerrd[2]-number of rows processed char sqlwarn[8];

Lu Wei8 SQL Communications Area //sqlwarn[0] set to “W” on warning //sqlwarn[1] set to “W” if character string truncated //sqlwarn[2] set to “W” if NULLs eliminated from aggregates //sqlwarn[3] set to “W” if mismatch in columns/host variables //sqlwarn[4] set to “W” when preparing an update/delete without a where-clause //sqlwarn[5] set to “W” due to PL/SQL compilation failure //sqlwarn[6] no longer used //sqlwarn[7] no longer used char sqlext[8]; //reserved for furture use }

Lu Wei9 SQL Communications Area The SQLCODE is set by the DBMS as follows: –0 indicates that the statement executed successfully (although there may be warning messages in sqlwarn) –A negative value indicates that an error occurred. The value in SQLCODE indicates the specific error that occurred. –A positive value indicates that the statement executed successfully, but an exceptional condition occurred, such as no more rows returned by a SELECT statement.

Lu Wei10 SQL Communications Area The WHENEVER statement –Every embedded SQL statement can potentially generate an error. Clearly, checking for success after every SQL statement would be quite laborious. –Oracle precompiler provides an alternative method to simplify error handling. –The format of the WHENEVER statement is EXEC SQL WHENEVER

Lu Wei11 SQL Communications Area Condition -SQLERROR(SQLCODE<0) -SQLWARNING(SQLCODE>0) -NOT FOUND Action -CONTINUE -DO, DO BREAK, DO CONTINUE -GO TO label -STOP

Lu Wei12 Host Language Varibles A host language variable is a program variable declared in the host language. It can be either a single variable or a structure. Host language variables can be used in embedded SQL statements to transfer data from the database into the program, and vice versa.

Lu Wei13 Host Language Varibles To use a host variable in an embedded SQL statement, the variable name is prefixed by a colon(;). Example EXEC SQL UPDATE Staff SET salary = salary + :increment WHERE staffNo=‘SL21’;

Lu Wei14 Host Language Varibles Host language variables must be declared to SQL as well as being declared in the syntax of the host language. All host variables must be declared to SQL in a BEGIN DECLARE SECTION…END DECLARE SECTION block. EXEC SQL BEGIN DECLARE SECTION; float increment; EXEC SQL END DECLARE SECTION;

Lu Wei15 Host Language Varibles Indicator variables –Most programming languages do not provide support for unknown or missing values, as represented in the relational model by nulls. –Embedded SQL provides indicator variables to resolve this problem.

Lu Wei16 Host Language Varibles The meaning of the indicator variable is as follows: –A 0 value means that the associated host variable contains a valid value. –A -1 value means that the associated host variable should be assumed to contain a null(the actual content of the host variable is irrelevant). –A positive indicator value means that the associated host variable contains a valid value, which may have been rounded or truncated.

Lu Wei17 Host Language Varibles Example EXEC SQL BEGIN DECLARE SECTION; char address[51]; short addressInd; EXEC SQL END DECLARE SECTION; addressInd = -1; EXEC SQL UPDATE privateOwner SET address =:address :addressInd WHERE ownerNo = ‘CO21’;

Lu Wei18 Cursors We can also retrieve data using the SELECT statement, but the processing is more complicated if the query produces more than one row. The complication results from the fact that most high-level programming languages can process only individual data items or individual rows of a structure whereas SQL process multiple rows of data.

Lu Wei19 Cursors To overcome this impedance mismatch, SQL provides a mechanism for allowing the host language to access the rows of a query result one at a time. Embedded SQL divides queries into two groups: –Single-row queries –Multi-row queries

Lu Wei20 Cursors Single-row queries –example1

Lu Wei21 Cursors Multi-row queries –example2

Lu Wei22 Static Embedded SQL Statements In static embedded SQL statement, the pattern of database access is fixed and can be ‘hard- coded’ into the program. Static SQL does not allow host variables to be used in place of table names or column names. The embedded SQLs we discussed above are all static SQLs.

Lu Wei23 Static Embedded SQL Statements In static Embedded SQL, the follow elements must be fixed: –Reserved words (SELECT,UPDATE,DELETE…) –The number of host variables –The data type of each host variable –The database object will be accessed in SQL(table,column,view,index,…) Or dynamic SQL will be used.

Lu Wei24 Dynamic SQL In many situations where the pattern of database access is not fixed and is known only at runtime. This requires more flexibility than static SQL. Dynamic embedded SQL can be used to resolve this problem.

Lu Wei25 Dynamic SQL The basic idea of dynamic SQL is to place the complete SQL statement to be executed in a host variable. The host variable is then passed to the DBMS to be executed.

Lu Wei26 Dynamic SQL EXECUTE IMMEDIATE statement EXEC SQL EXECUTE IMMEDIATE [hostVariable|stringLiteral];

Lu Wei27 Dynamic SQL EXEC SQL BEGIN DECLARE SECTION; float increment; EXEC SQL END DECLARE SECTION; EXEC SQL UPDATE Staff SET salary=salary+:increment WHERE staffNo=‘SL21’;

Lu Wei28 Dynamic SQL EXEC SQL BEGIN DECLARE SECTION; char buffer[100]; EXEC SQL END DECLARE SECTION; sprintf(buffer,”UPDATE Staff SET salary=salary+%f WHERE staffNo=‘SL21’”,increment;); EXEC SQL EXECUTE IMMEDIATE :buffer;

Lu Wei29 Dynamic SQL The PREPARE and EXECUTE Statements PrecompilerBind utilityExecute Compile timeRun time ( a ) static SQL Run time EXECUTE IMMEDIATE statement PREPARE statement EXECUTE Parse statement Validate statement Optimize statement Create execute plan Execute statement ( b )动态 SQL

Lu Wei30 Dynamic SQL The format of the PREPARE statement The format of the EXECUTE statement EXEC SQL PREPARE statementName FROM [hostVariable|stringLiteral] EXEC SQL EXECUTE statementName [USING hostVariable[indicatorVariable][,…]| USING DESCRIPTOR descriptorName]

Lu Wei31 Dynamic SQL EXEC SQL BEGIN DECLARE SECTION; char buffer[100]; float newSalary; char staffNo[6]; EXEC SQL END DECLARE SECTION; SPRINTF(buffer,”update staff set salary=:sal where staffNo=:sn”); EXEC SQL PREPARE stmt FROM :buffer;

Lu Wei32 Dynamic SQL do{ printf(“Enter staff number:”); scanf(“%s”,staffNo); printf(“Enter new salary:”); scanf(“%f”,newSalary); EXEC SQL EXECUTE stmt USING :newSalary,:staffNo; printf(“Enter another(Y/N)?”); scanf(“%c”,nore); } until(more!=‘Y’);

Lu Wei33 Dynamic SQL Example3

Lu Wei34 Dynamic SQL The SQL Descriptor Area –Placeholders are one way to pass parameters to the EXECUTE statement. –An alternative is through a dynamic data structure called the SQL Descriptor Area(SQLDA) –The SQLDA is used when the number of placeholders and their data types are not known when the statement is formulated.

Lu Wei35 ODBC

Lu Wei36 Summary How SQL statements can be embedded in high- level programming languages. The difference between static and dynamic embedded SQL How to write programs that use static or dynamic embedded SQL statements How to use ODBC to connect to database.