CH 10
CH 10
Chapter 10:
Introduction to SQL
Programming
Techniques
standard
Interactive interface (e.g., through a query processor as Oracle
SQLPlus or through a DBMS desktop front-end tool such as MS
Access or SQL Developer, consists of
SQL commands typed directly into a monitor
Impedance mismatch discusses the problems that may occur when databases
are queried using any of the three database programming approaches (e.g.,
Oracle PL/SQL and programming language model of methods 1 and 2)
1. Data model Binding for each host programming language with the data
model of the database can be challenging.
Thus, each such embedded C language program specifies
for each attribute relational database (e.g., Varchar) type
the compatible programming language types (e.g., char[ ])
2. Cursor or iterator variable has to be used to
Loop over the tuples in a query result that are typically
Examples are:
1. Embedded SQL
C language
2. SQLJ
Java language
Programming language called host language
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
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
Example 10.1: Given the Company Database example
table Employee already in your database, write an
embedded SQL program in C/C++ that will accept as
input a social security number of an employee and
prints some information from the Employee record.
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
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
Dynamic SQL
Execute different SQL queries or updates
dynamically at runtime
Dynamic update
Dynamic query
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
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
Description record
Keeps track of information about tuples or
parameters
Handle to the record
C pointer variable makes record accessible to
program
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
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
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