Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Copyright © 2004 Pearson Education, Inc.. Chapter 9 More SQL: Assertions, Views, and Programming Techniques.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 9 Introduction to SQL Programming Techniques.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
Objective: To access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Murali Mani SQL with other Programming Languages.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL: Assertions, Views, and Programming Techniques by Pinar Senkul resources: mostly froom.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Overview of Database Languages and Architectures.
Chapter 9 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 &14 Intro to SQL Programming Techniques & Web DB + PhP.
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
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.
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)
CS 405G: Introduction to Database Systems Database programming.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
Advanced SQL: Cursors & Stored Procedures
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.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 6 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6 Sections –
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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.
1 Database Application Development Chapter 6 Sections –
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 & 9 SQL-99: SchemaDefinition, Constraints, Introduction to SQL Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
2) Database System Concepts and Architecture. Slide 2- 2 Outline Data Models and Their Categories Schemas, Instances, and States Three-Schema Architecture.
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.
1 Database Application Development Programming With Databases.
10/3/2017.
Web Database Programming Using PHP
CSE202 Database Management Systems
Chapter 4 An Introduction to SQL.
Introduction to SQL Programming Techniques
SQL Environment.
Web Technologies IT230 Dr Mohamed Habib.
Web Database Programming Using PHP
Subject Name: DATABASE MANAGEMENT SYSTEM Subject Code: 10CS54
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Database System Concepts and Architecture.
CPSC-310 Database Systems
Database Application Development
DB Programming: SQL Programming Techniques
Introduction to SQL Programming Techniques
Chapter 8 Advanced SQL.
Presentation transcript:

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 10 Outline Database Programming: Techniques and Issues Embedded SQL, Dynamic SQL, and SQLJ Database Programming with Function Calls: SQL/CLI and JDBC Database Stored Procedures and SQL/PSM Comparing the Three Approaches Slide 10- 2

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Introduction to SQL Programming Techniques Database applications Host language Java, C/C++/C#, COBOL, or some other programming language Data sublanguage SQL SQL standards Continually evolving Each DBMS vendor may have some variations from standard Slide 10- 3

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Programming: Techniques and Issues Interactive interface SQL commands typed directly into a monitor Execute file of Application programs or database applications Used as canned transactions by the end users access a database May have Web interface Slide 10- 4

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Approaches to Database Programming Embedding database commands in a general- purpose programming language Database statements identified by a special prefix Precompiler or preprocessor scans the source program code Identify database statements and extract them for processing by the DBMS Called embedded SQL Slide 10- 5

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Approaches to Database Programming (cont’d.) Using a library of database functions Library of functions available to the host programming language Application programming interface (API) Designing a brand-new language Database programming language designed from scratch First two approaches are more common Slide 10- 6

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Impedance Mismatch Differences between database model and programming language model Binding for each host programming language Specifies for each attribute type the compatible programming language types Cursor or iterator variable Loop over the tuples in a query result Slide 10- 7

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Typical Sequence of Interaction in Database Programming Open a connection to database server Interact with database by submitting queries, updates, and other database commands Terminate or close connection to database Slide 10- 8

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Embedded SQL, Dynamic SQL, and SQLJ Embedded SQL C language SQLJ Java language Programming language called host language Slide 10- 9

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Single Tuples with Embedded SQL EXEC SQL Prefix Preprocessor separates embedded SQL statements from host language code Terminated by a matching END-EXEC Or by a semicolon (;) Shared variables Used in both the C program and the embedded SQL statements Prefixed by a colon (:) in SQL statement Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.1 C program variables used in the embedded SQL examples E1 and E2. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Single Tuples with Embedded SQL (cont’d.) Connecting to the database CONNECT TO AS AUTHORIZATION ; Change connection SET CONNECTION ; Terminate connection DISCONNECT ; Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Single Tuples with Embedded SQL (cont’d.) SQLCODE and SQLSTATE communication variables Used by DBMS to communicate exception or error conditions SQLCODE variable 0 = statement executed successfully 100 = no more data available in query result < 0 = indicates some error has occurred Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Single Tuples with Embedded SQL (cont’d.) SQLSTATE String of five characters ‘00000’ = no error or exception Other values indicate various errors or exceptions For example, ‘02000’ indicates ‘no more data’ when using SQLSTATE Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.2 Program segment E1, a C program segment with embedded SQL. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Multiple Tuples with Embedded SQL Using Cursors Cursor Points to a single tuple (row) from result of query OPEN CURSOR command Fetches query result and sets cursor to a position before first row in result Becomes current row for cursor FETCH commands Moves cursor to next row in result of query Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.3 Program segment E2, a C program segment that uses cursors with embedded SQL for update purposes. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Multiple Tuples with Embedded SQL Using Cursors (cont’d.) FOR UPDATE OF List the names of any attributes that will be updated by the program Fetch orientation Added using value: NEXT, PRIOR, FIRST, LAST, ABSOLUTE i, and RELATIVE i Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Specifying Queries at Runtime Using Dynamic SQL Dynamic SQL Execute different SQL queries or updates dynamically at runtime Dynamic update Dynamic query Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.4 Program segment E3, a C program segment that uses dynamic SQL for updating a table. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe SQLJ: Embedding SQL Commands in Java Standard adopted by several vendors for embedding SQL in Java Import several class libraries Default context Uses exceptions for error handling SQLException is used to return errors or exception conditions Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.5 Importing classes needed for including SQLJ in Java programs in Oracle, and establishing a connection and default context. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.6 Java program variables used in SQLJ examples J1 and J2. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.7 Program segment J1, a Java program segment with SQLJ. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Retrieving Multiple Tuples in SQLJ Using Iterators Iterator Object associated with a collection (set or multiset) of records in a query result Named iterator Associated with a query result by listing attribute names and types in query result Positional iterator Lists only attribute types in query result Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.8 Program segment J2A, a Java program segment that uses a named iterator to print employee information in a particular department. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure 10.9 Program segment J2B, a Java program segment that uses a positional iterator to print employee information in a particular department. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Programming with Function Calls: SQL/CLI & JDBC Use of function calls Dynamic approach for database programming Library of functions Also known as application programming interface (API) Used to access database SQL Call Level Interface (SQL/CLI) Part of SQL standard Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe SQL/CLI: Using C as the Host Language Environment record Track one or more database connections Set environment information Connection record Keeps track of information needed for a particular database connection Statement record Keeps track of the information needed for one SQL statement Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe SQL/CLI: Using C as the Host Language (cont’d.) Description record Keeps track of information about tuples or parameters Handle to the record C pointer variable makes record accessible to program Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure Program segment CLI1, a C program segment with SQL/CLI. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure Program segment CLI2, a C program segment that uses SQL/CLI for a query with a collection of tuples in its result. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe JDBC: SQL Function Calls for Java Programming JDBC Java function libraries Single Java program can connect to several different databases Called data sources accessed by the Java program Class.forName("oracle.jdbc.driver.OracleDriver") Load a JDBC driver explicitly Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe JDBC: SQL Function Calls for Java Programming Connection object Statement object has two subclasses: PreparedStatement and CallableStatement Question mark ( ? ) symbol Represents a statement parameter Determined at runtime ResultSet object Holds results of query Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure Program segment JDBC1, a Java program segment with JDBC. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure Program segment JDBC2, a Java program segment that uses JDBC for a query with a collection of tuples in its result. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Stored Procedures and SQL/PSM Stored procedures Program modules stored by the DBMS at the database server Can be functions or procedures SQL/PSM (SQL/Persistent Stored Modules) Extensions to SQL Include general-purpose programming constructs in SQL Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Stored Procedures and Functions Persistent stored modules Stored persistently by the DBMS Useful: When database program is needed by several applications To reduce data transfer and communication cost between client and server in certain situations To enhance modeling power provided by views Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Stored Procedures and Functions (cont’d.) Declaring stored procedures: CREATE PROCEDURE ( ) ; declaring a function, a return type is necessary, so the declaration form is CREATE FUNCTION ( ) RETURNS ; Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Database Stored Procedures and Functions (cont’d.) Each parameter has parameter type Parameter type: one of the SQL data types Parameter mode: IN, OUT, or INOUT Calling a stored procedure: CALL ( ) ; Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe SQL/PSM: Extending SQL for Specifying Persistent Stored Modules Conditional branching statement: IF THEN ELSEIF THEN... ELSEIF THEN ELSE END IF ; Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe SQL/PSM (cont’d.) Constructs for looping Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Figure Declaring a function in SQL/PSM. Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Comparing the Three Approaches Embedded SQL Approach Query text checked for syntax errors and validated against database schema at compile time For complex applications where queries have to be generated at runtime Function call approach more suitable Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Comparing the Three Approaches (cont’d.) Library of Function Calls Approach More flexibility More complex programming No checking of syntax done at compile time Database Programming Language Approach Does not suffer from the impedance mismatch problem Programmers must learn a new language Slide

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Summary Techniques for database programming Embedded SQL SQLJ Function call libraries SQL/CLI standard JDBC class library Stored procedures SQL/PSM Slide