Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Data Objects
Accessed under the users direction and by which the users data is organized.
System Objects
Controlled and used by DB2
DB2 Objects
Data Objects System Objects
DB2 Catalog DB2 Directory Active & Archive Logs Boot Strap Data Set Buffer Pools Locks
What is a Database ? Is a repository for stored data - C.J.Date What is Data ? A representation of facts or instruction in a form suitable for communication - IBM Dictionary What is a database system ? An integrated and shared repository for stored data or collection of stored operational data used by application systems of some particular enterprise. Or Nothing more than a computer-based record keeping system.
System Objects
DB2 Catalog
Consists of tables of data about every object defined in the DB2 system. When an object is created, altered or dropped, a row is inserted, updated or deleted in the Catalog.
DB2 Directory Contains information required to start DB2 & for its normal operation Cannot be accessed using SQL Directory objects are not described in DB2 catalog
Bootstrap Dataset Contains inventory of all active and archive log datasets. During installation of DB2, two BSDS datasets are created, kept in different volumes.
Active & Archive Logs DB2 records all data changes and significant events in active logs as they occur. In case of failure, it uses this data to recover the lost information. When the active log is full, DB2 copies the contents of the active log to a DASD or magnetic tape data set called archive logs. Buffer Pools Are areas of virtual storage which DB2 uses during execution of an application program or an interface SQL, to temporarily store pages of tablespace There are four different bufferpools named BPO, BP1, BP2, and BP32.
Locks
DB2 guarantees data integrity by using several locking mechanisms. These strategies permits multiple users from multiple environments to access and modify data concurrently without lose of data integrity. DB2 supports locking at three levels. Table Space Level, Table Level and Page Level. DB2 uses locks to control concurrency & prevent inconsistent data.
Data Objects
Tables All data is stored in tables. A table is a collection of rows, all having same columns Rows are records and Columns are fields. Intersection of a column & row is a Value SQL is used to access data in DB2 tables.
Indexes
An ordered set of pointers to the data in DB2 table Stored separately from the table Each index occupies its own space Used to Improve performance; Ensure Uniqueness Multiple indexes can be defined on a table A table having a primary key will necessarily have a primary unique index.
Views
Is a virtual table derived from one or more base tables The view definition is stored in a catalog. Alternate way of representing data that exists in one or more tables Can include all or some of the columns from one or more base tables Indexes on Base table can improve the performance operations on the views
Tablespaces Is the physical space used to hold one or more tables Consists of 1 to 64 VSAM LDS can contain upto 64 GB of data Three types of tablespaces are
Database A Set of DB2 objects (tables, indexes, tablespaces & views) One database for one application recommended Default Database is DSNDB04 Storage Group (STOGROUP) A set of Volumes on DASD. Volumes hold the data in which tables and indexes are actually stored. Up to 133 DASD volumes can be assigned to a single STOGROUP. DB2 allocates storage for tablespaces and indexes. When a tablespace is created, DB2 defines the necessary VSAM datasets using IDCAMS.
STOGROUP
It is a collection of direct access volumes , all of the same device type. The option is defined as a part of table space definition. When a given space needs to be extended, storage is acquired from the appropriate STOGROUP. Max. 133 volumes in a single database. Syntax: CREATE STOGROUP MAPSG VOLUMES(SMS001) VCAT DSNCAT;
DATABASE
A collection of logically related objects like Tablespaces - Indexspaces - Tables etc Not a physical kind of object may occupy more than one disk space A STOGROUP & BUFFERPOOL ( is buffer area used to maintain recently accessed table & index pages) must be defined for each database. STOGROUP and user-defined VSAM are the two storage allocation for a DB2 database definition.
In a given database, all the spaces need not have the Same STOGROUP. More than one volume can be defined in a STOGROUP. DB2 keep track of which volume was defined first & uses that volume.
Syntax:
Create Database DBMAP STOGROUP MAPSG;
TABLESPACES Logical address space on secondary storage to hold one or more tables. A SPACE is basically an extendable collection of pages with each page of size 4k or 32k bytes. It is the storage unit for recovery & reorganizing purpose.
simple Tables paces A simple tablespace can contain one or more tables. A single page of a simple tablespace can contain rows from all the tables defined to the tablespace. In general only one table should be defined per simple tablespace. One table space can be up 64 billion bytes Page size 4kb to 32 kb Each row contains 127 tuples rows Lock is at page level Here data integrity will be there but data concurrency will not be there
simple tablespace can contain more than one stored table, though one is usually the best option. It normally consists of only one VSAM ESDS and may contain one or more table. Reading the data of one table requires a sequential read of the Tablespace(tablespace scan) thus increasing I/O and degrading performance. Concurrent access to tables in the same tablespace can cause lengthy response delays
LOCKSIZE - indicates the type of locking DB2 performs for the given TS. (PAGE/TABLE/TABLESPACE) USING PCTFREE method of storage allocations Stogroup or VCAT - % of space available for future inserts.
FREEPAGE - no. of pages after which an empty page is available. BUFFERPOOL BP1,BP2, BP32K CLOSE YES/NO- whether the underlying VSAM datasets be closed each time the table is used. ERASE YES/NO - whether physical DASD where the TS reside to be written with binary zeros when the TS is dropped. NUMPARTS - For partitioned tables paces SEGSIZE For segmented tables paces
SYNTAX CREATE TABLESPACE SSSVIJTS IN SSSVIJDB USING STOGROUP ST0R1 PRIQTY 30720 SECQTY 10240 LOCKSIZE TABLE BUFFERPOOL BP0 CLOSE NO; character: A Cobol word must be formed from following character
Page1
Page 2
Page 3
Page 4
Table Row 1
Table 1 Row 3
Table 1 Row 4
Table 3 Row 2
Table 1 Row 2
Table 3 Row 1
Table 2 Row 3
Table 3 Row 3
Table 2 Row 1
Table 2 Row 2
Table 2 Row 4
Segmented Tablespaces The pages within a tablespace are organized into segments and each segment holds rows from only one table. Can hold more than one table. A segmented tablespace is the most efficient type of tablespace for most of the DB2 development efforts.
The space is divided in to equal-sized groups of pages called segments, and each segment can contain rows from only one table. Each segment is a group of 4 to 64 pages. Provides improved performance, concurrency and space management for multiple tables in a single table space. Related tables can be efficiently placed together in one Table space.
Segment 1
Segment 2
Page 1
Page 2
Page 3
Page 4
Table 1 Row 1
Table 1 Row 4
Table 2 Row 1
Table 2 Row 4
Table 1 Row 2
Table 1 Row 5
Table 2 Row 2
Table 1 Row 3
Table 1 Row 6
Table 2 Row 3
Create segment
SYNTAX CREATE TABLESPACE KSATS
IN SSSVJDB
USING STOR1
PRIQTY 52 SECQTY 20 ERASE NO SEGSIZE 32 LOCKSIZE PAGE BUFFERPOOL BP1 close YES;
Partitioned Tablespaces
The tablespace is divided into partitions, and each partition is stored in one VSAM data set. A partitioned tablespace is divided into components called partitions. Each partition resides in a separate physical dataset. DB2 permits 1 to 64 partitions per tablespace. Only one table can be defined in a partitioned tablespace
Partitioned Tablespaces
SYNTAX CREATE TABLESPACE SALESHX IN SSSVJDB USING STOR1 PRIQTY 4000 SECQTY 130 ERASE NO NUMPARTS 82 LOCKSIZE PAGE BUFFERPOOL BP1
Page 3
Page 4
Table 1 Row 1
Table 1 Row 4
Table 1 Row
Table 1 Row 10
Table 1 Row 2
Table 1 Row 5
Table 1 Row 8
Table 1 Row 11
Table 1 Row 3
Table 1 Row 6
Table 1 Row 9
DB2 Threads
Controls Structures used by DB2 to communicate with an application program. Used to send requests to DB2, to send data from DB2 to the program, and to communicate the result of each SQL statement after it is executed.
Using SQL Processor Using File Input (SPUFI) Using Query Management Facility (QMF)
Background
By embedding them in application programs written in C, Cobol, Fortran, Assembler, PL/1, etc.
Relational concepts
Relation : A table or File Tuple : Row contains an entry for each attribute Attributes : Columns or the characteristics that define the entity Domain:. A range of values (or Pool) Entity : Some object about which we wish to store information Null : Represents an unknown/empty value Atomic Value: Smallest unit of data; the individual data value
Null: All datatypes include the null value. The null value is a special value that is distinct from all other values and thereby denotes the absence of a value. It is NOT the same as 0 or blanks.
Date format
Time format
NOT NULL: Value must be given NULL: If Value not given it is marked unknown NOT NULL: If value not given DB2 fill default value WITH DEFAULT: Numeric Becomes Zero, Character Becomes Blank, Date becomes Current
Date, Time becomes Current Time, Timestamp becomes Current Timestamp. If Nulls are allowed, internal storage length is increased by 1 byte
A mechanism that ensures data integrity between tables related by Primary & Foreign Keys
Insert Rules
When inserting a row with the foreign key, DB2 checks the values of the foreign key columns against the value of the primary key columns in the parent table. A new Primary Key can be inserted as long as it is unique.
Update Rules When updating foreign key values DB2 performs same checks as when it is inserting a row with a foreign key. Delete Rules
Cascade: When a row of the parent table is deleted, any related rows in the dependent table are also deleted. Restrict: Rows of parent table that have dependent rows cannot be deleted Set Null: When a row of a parent is deleted, the corresponding values of the foreign key in any dependent rows are set to null.
CREATE Object, Object Name Option - List Object STOGROUP | DATABASE | TABLESPACE | TABLE | VIEW | INDEX
CREATE TABLE This statement is used to create objects SYNTAX : For Creating a Table CREATE TABLE <tabname> (Col Definitions) PRIMARY KEY(Columns) / FOREIGN KEY UNIQUE (Colname) (referential constraint) CHECK (colname relational operator value) (check constraint) [IN Database.Tablespace Name ]
Create Tables
CREATE TABLE EMPTAB (EMPNO INTEGER(5) NOT NULL NOT NULL NOT NULL CHAR(25) DECIMAL(7,2) DATE NOT NULL
IN GSLDB.GSLTS
Create Index
CREATE UNIQUE INDEX index name ON table name (FLD 1 ASC/DESC, FLD 2 ASC/DESC) Example: CREATE UNIQUE INDEX EMPIDX ON EMPTAB (EMPNO ASC);
Create View
CREATE VIEW view name AS SELECT FLD 1, FLD 2 .. From table-name WHERE condition (WITH CHECK OPTION)
Example: CREATE VIEW EMPV AS SELECT EMPNO, EMPNAME FROM EMPTAB WHERE dept name = TRG
Alter Table
to Add new column to Add Primary Key to DROP Primary Key & Foreign Key to Add Referential Constraints
Example:
Notes:
Adding a primary key in a table puts the table in an incomplete state until a unique index is created on that particular column. Dropping a primary key on a parent table drops all references to dependent tables.
DB2 does not allow deletion of a column from a table. DB2 does not allow the characteristics of the column to be changed.
DCL Statements
Provides effective control over the resources at required levels, either at the row level or at the column level. Allows centralization / decentralization of control at required levels. DB2 users may be allowed to control the use of resources, without permission/assistance of Database Administrators. Control over DB2 objects and data is achieved through GRANT and REVOKE statements.
Revoke Revokes privileges from authids General format REVOKE authorization ON object object name FROM authid / PUBLIC
Grant
Grants privileges to Authorization Ids. General format GRANT authorization ON object object name to authid / PUBLIC [WITH GRANT OPTION ] Can be executed from an application program or can be executed interactively using SPUFI or QMF Authorization for table can be SELECT, INSERT, UPDATE, DELETE, ALTER, INDEX or ALL For update, authorization can be given on specific columns also. WITH GRANT OPTION allows the grantee to grant privileges to others. Example: GRANT SELECT ON TABLE EMPTAB TO TRG010;
Update
Used to change any value in a table Example: UPDATE TABLE1 SET GRPID=MKR WHERE EMPNO=005;
EMPNO 005
GRPID PRG
DESIG
If WHERE clause is not used, all records are changed to FIN GRPID
Select
WHERE clause is used to specify the search criteria. If no criteria is specified, all the records are retrieved. Example: Table1
DESIG
SALES
SELECT* FROM Table1 * to specify all columns EMPNO NAME 001 002 003 JOSEPH SAM RAJA GRPID MKR FIN SALES SMG GM MGR DESIG
The same answer is got even if we give SELECT EMPNO, NAME, GRPID, DESIG from TABLE1.
Conditional Select
WHERE clause is used to select data from only the rows that meet certain conditions. A WHERE clause specifies a search condition that, when evaluated against a row, yields TRUE or FALSE or UNKNOWN.
Any
Is same as IN when given as =ANY
Joins
Joins are used to select data from multiple tables Example: Table2
We are joining table 1 and table 2 to get the information of manager(s) only.
Cartesian Product
Cartesian product is the result of a join that does not specify matching columns.
Subquery
Used to nest SELECT statements When one or more select statements are nested, it is referred to as sub query
Union
UNION combines two sets of rows into a single set composed of all the rows in either or both of the two original sets. The two original sets must be union-compatible (i.e.)
The two sets must contain the same number of columns. Each column of the first set must be either the same data type as the corresponding column of the second set or convertible to the data type of the corresponding column of the second set.
Outer Join
When we want both matching & non - matching rows returned for one or more of the tables being joined, we use Outer Join.
Note: If there are 50 MKR, 50 FIN & 50 SALES records, they will be grouped together and displayed. Table 3:
AVG
To find the Average of a column specified or expression given as the argument
Count
To count the number of rows, use distinct keyword to count only the number of distinct values for a given column.
Max
Max function returns the largest value in the specified column or expression.
Min
Min function returns the smallest value in the specified column or expression.
Sum This returns the accumulated total of the specified column or expression.
Delete To delete a record or set of records or all records General Format: DELETE FROM table name WHERE search-condition; If WHERE clause is not used, all records are deleted.
SPUFI
SPUFI (SQL Processor Using File Input) reads the SQL statements contained as a text in a sequential file, processes those statements and places the output in the output dataset specified in the browse mode. The screen is self explanatory and the only inputs the user has to give is the input file name where the SQL statements are placed and the output file name where the results have to be placed. SPUFI supports the online execution of SQL statements from a TSO terminal.
DCLGEN Command
The DCLGEN or Declaration Generator Command is used to produce a COBOL copybook, which contains a SQL DECLARE TABLE statement along with the WORKING-STORAGE hostvariable definitions for each column of the table. When the DCLGEN command is issued DB2 reads catalog to determine the structure of the table and builds the COBOL copybook.
Preparation Steps
SOURCE
NON SQL
DB2 PRECOMPILE
SQL
CATALOG
LOAD MOD
PLAN
Program Execution
EXEC
LOAD MOD
CALL
PLAN
OTHER DATA
DL/I or VSAM
DB2
DB2 TABLES
OTHER DATA
DL/I or VSAM
DB2
DB2 TABLES
BIND Comments
Checks for errors, using DB2 catalog Checks authorisation Develops Access strategy for each statement Stores strategies as plan (reusable without repeating bind)
Pre-compilation
Pre-compiler Functions:
Program used: DSNHPC INPUT EMBEDDED SQL PROGRAM OUTPUT Modified source DBRMLIB Database Request Module Other Sources needed: DCLGEN: This produces modules that can be included in a program. It builds a COBOL copybook. Expands the DB2 related INCLUDE statements. Checks the SQL syntax. Separates SQL from NON-SQL statements and places SQL statements in Database Request Module (DBRM) Places TIMESTAMP in the Modified source in the DBRM member and load module.00
Compile
Program Used: IGYCRCTL INPUT Modified Source Program; OUTPUT OBJ Module Other Sources needed: COPYLIBS Process: The COPYLIB members will be expanded. The COBOL syntax will be checked. Places time stamp written in precompiler step in OBJ module.
Link Edit
Program Used: IEWL INPUT OBJ Module; OUTPUT Load Module Process: The executable load module is produced Subprogram exists will be merged along with system libraries Places the timestamp written from the precompiler step in the load module.
Bind
Program Used: IKJEFT01 INPUT DBRM LIB produced in precompiler step OUTPUT PLAN Function: Reads the SQL statements from DBRMs and produces a mechanism to access data from tables. Parameters given while binding: 1. 2. 3. 4. 5. 6. 7. 8. Owner, Qualifier Action (REPLACE/ADD) Isolation (CS/RR) Acquire (USE/DEALLOCATE) Release (COMMIT/DEALLOCATE) Validate (BIND/RN) Explain Flag.
Owner: Name of the owner who created the PLAN Qualifier: Name where the table reside Action: Add or Replace the existing plan (BINDADD & BINDREPLACE) Isolation: This determines the mode of page locking implemented by the programs as it runs.
CS Cursor Stability: Releases the page locks as soon as another page is accessed or an COMMIT is issued. Improves concurrency. RR Repeatable Read (DEFAULT): Does not releases the page locks until a COMMIT is issued.
Acquire: This determines the mode of table space locking implemented by the program as it runs.
Use: Table space locks are acquired when the table space is accessed.
Allocate: Table space locks are acquired when the plan is allocated.
Release: Commit: Table space locks are released at COMMIT or ROLLBACK time. Deallocate: Table space locks are released only when the plan finishes and is deallocated.
Validate: Method of checking for the existence and validity for DB2 access tables and authorization Bind Validates at Bind time; Run Validates at run time.
Bind
Bind reads SQL statements from DBRM s and produces a method to access data as directed by the SQL statements being bound. It stores this access method as Plan Two types of Binds - BIND PLAN, BIND PACKAGE BIND PACKAGE accepts a DBRM as input and produces a SINGLE package. A package is not executable. Packages should be bound into a plan before executing. Output of BIND PLAN is the application plan containing the executable logic giving the access path to DB2 data. BIND also performs Syntax checking of SQL statements, Checking of the DB2 table and column catalog Information, Authorization Validation.
Embedded SQL
SQL statement are embedded anywhere in the application program. These are analyzed by DB2 precompiler & replaced by equivalent statements in host language In addition to data definition/manipulation statements, SQL statements unique to application program coding are provided. These include support for Declaration tables, Include statements, SQL Communications Area, usage of Host Variables within SQL statements & Cursor tables. DB2 application programs may be coded in COBOL, FORTRAN, PL/I and ASSEMBLY language. Each statement is delimited i.e. each SQL is enclosed in an EXEC SQL, END-EXEC block Data Values can be gathered from host program variables Multiple rows of table can be accessed through a CURSOR DB2 describes results of execution in SQLCA End Delimiter varies by Host Language. For COBOL, it is END-EXEC.
Host program variables are defined here to hold data. These variables are referred in the SQL statements in the Procedure Division. Procedure Division:
SQL statements to access and manipulate data are coded here. statements reference DB2 tables and host program variables. SQL statements begin with EXEC SQL and end with END-EXEC.
These
Notes: For host variables not within a host structure, use COBOL level numbers 01 or 77. Variable character fields are defined as a 02 group level specifier, and two 49 level specifiers. One is the length specifier, and other is the actual data field. While inserting data into a variable character field, the actual number of characters instead is indicated by the length specifier. While retrieving data from a variable character field, the actual number of characters returned in the data field is indicated by the length specifier.
In the above example DCLDEPT is a Host Structure containing corresponding Host variables for DEPTNO, DEPTNAME & LOCATION
Host Structure
A series of variables referenced as a unit is called a structure. In a COBOL 01 (group) level data structure, representing all column selected in SQL statement. The 01 (group) level data name is prefixed by a colon Is coded in a SQL statement in place of elementary level fields. Can have a maximum of two levels. When elementary fields have duplicate data names, they must be qualified with the relevant structure name. The general coding format is: Structure Name.Field Name
Note: For data variable within a host structure use COBOL level numbers 02 through 48. Coding FILLER within a host structure makes the structure invalid.
SQLCODE
Regardless of whether the application program provides an SQLCA or a stand-alone variable, SQLCODE is set by DB2 after each SQL statement is executed. DB2 conforms to the ISO/ANSI SQL standard as follows: If SQLCODE = 0, execution was successful. If SQLCODE > 0, execution was successful with a warning. If SQLCODE < 0, execution was not successful. SQLCODE = 100, "no data" was found. For example, a FETCH statement returned no data because the cursor was positioned after the last row of the result table.
SOURCE MODULE
DBRM
COMPILER COMPILER
PRECOMPILER PRECOMPILER
BIND BIND
OBJECT MODULE EDITOR EDITOR OTHER OTHER OBJECT OBJECT LIST OF LIST OF PACKAGE PACKAGE
Cursor Processing
In the Embedded SQL application program development, if select returns more than one row of information then we have to use cursors, since the program cannot process all the rows at once. It can process only one row at a time. Following operations can be performed on a CURSOR DECLARE OPEN FETCH CLOSE Cursor can return zero, one or many rows depending on the Fetch condition.
Dynamic Embedded SQL used when the SQL statements will be defined at run-time (ie. they are not known in advance)
STATIC
DYNAMIC
query plan and optimization occur during development (therefore, query execution will be faster) need to know all SQL statements in advance
query plan and optimization occur when the query is issued (query execution could be slow) allows for any SQL statement to be issued
database objects do not have database objects must exist to exist during app at BIND time development
__________________________________________ _
With hold Prevents the cursor from being closed as a result of a commit operation Update-col The name of the column for which you want to enable updates
Declare Cursor
To define and identify a set of rows to be accessed. For updating / deleting a row, FOR UPDATE OF option should be specified. Cursor name cannot be a Hyphenated word Example of Cursor Declaration
WITH HOLD clause can be used with DECLARE, to retain the cursor position beyond the commit point.
Sql Fetch statement Exec Sql Fetch cursor-name INTO {:host-var [, :host-var]| :host-structure} End-Exec.
________________________________________________________________ For eg: Exec Sql Fetch custcur INTO :fname, :lname End-Exec.
Open Cursor
Activates the cursor Rows are retrieved and stored in the result table Pointer is positioned before the first record Example
Fetch Cursor
Moves the contents of the selected row into Program Host Variables Points to the next row Cursor must be opened explicitly before it can be fetched. Example
Rows are sorted by invoice in descending sequence Declare Invcur cursor for select invcust, invno ,invtotal form invtable where invcust = :custno order by invtotal des ____________________________________________ __ Declare custcur cursor for select fname, lname from custtable where state = ca order by 2 ____________________________________________ __ Declare invcur cursor for select invno, invdate, invtot+invtax As Total from invtable order by total
EXAMPLE EXEC SQL UPDATE table SET column = scalar-expression [, column = scalar-expression ] .. WHERE CURRENT OF cursor END-EXEC.
The keyword NULL puts a null value in a column, but this cant be used for a column thats defined as NOT NULL. If a Check Constraint has been defined for a column, the value that the column is set to must satisfy the constraint. If the value in the sqlcode field isnt zero it means that the update failed so on changes were made to the table. Sqlerrd(3). The CURRENT OF phrase is used to update the4 current row in a cursor controlled table.
Exec Sql Declare custcur cursor with hold for select custno, addr, city, state from invtable where invcust < 3330 for update of custno, addr, city, state End-Exec. __________________________________________________ Changes the row at the current cursor position. Exec Sql update invtable set custno = :custno, addr = :addr, city = :city, state = :state Where current of custcur End-Exec.
syntax for Insert with Values Exec sql Insert into (table-name | view-name) [ col-name [, col-name])] values({ :host-var | literal | null}..) End-Exec. ______________________________________________ Insert into customer values( :cust-no, :custname) If a check constraint has been defined a column the value thats supplied for the column must satisfy the constraint. Sqlcode value of -803 means that the statement failed the row to be inserted contained a duplicate value in a column that is defined unique
Insert with a sub query Exec sql Insert into (table-name | view-name) [ col-name [, col-name])] select-statement [with isolation-level] End-Exec. _________________________________________ Exec sql Insert into oldcust-table select * from customer where custno < 30000 End-Exec.
Close Cursor
CLOSES the Cursor Releases the locks held by the cursor DB2 automatically closes cursor when program terminates COMMIT statement also closes all the cursors
-117
The number of insert values is not the same as the number of object columns -304 The value inserted is out of range -305 The null value cannot be assigned to output host variable number because no indicator variable is specified -805 Program not found in the PLAN -503 A column cannot be updated because it is not identified in the update clause of the select statement of the cursor
-803
Inserted or updated value invalid because index index-name constrains columns of table such that no 2 rows can contain duplicate values in those columns. Rid of existing row is Xrid Result of an Embedded Select Statement Is a Table of More Than One Row, or the Result of the Sub query of a Basic Predicate Is More Than One Value The Insert, Update, Delete, DDL or Authorization Statement Cannot Be Executed Because the IMS/VS Transaction Is Inquiry Only The precompiler-generated timestamp x in the load module is different from the bind timestamp y built from the DBRM
-811
-817
-818
Error Handling
WHENEVER statement used to enable automatic testing of return codes for subsequent SQL statements Affects all subsequent SQL statements until the next WHENEVER statement General Format
Static SQL are SQL statements embedded in a host language and bound before the program runs.
Dynamic SQL are SQL statements that are prepared and executed, while the program is running.
Statements that are allowed as dynamic sql statement _____________________________________________ Statements u may be likely to use DELETE,INSERT,SELECT,UPDATE. Statements are not likely to use ALTER,COMMERNT, COMMIT, CREATE, DROP, EXPLAIN, GRANT, LABEL, LOCK TABLE, REVOKE, ROLLBACK. Cursor Processing Open, fetch, close. Precompiler directives Declare Include Whenever. Dynamic sql Execute Immediate Prepare . Execute Declare.
Static Vs Dynamic
For static SQL, the structure of the statement should be known beforehand, while dynamic SQL allows complete flexibility in the structure of the statement. Static SQL can contain host variables; Dynamic SQL cannot host variables. Static SQL are bound beforehand, while dynamic SQL are bound at runtime and hence runtime overhead is greater. Static SQL select statements can have INTO clause whereas dynamic SQL select statements cannot have INTO clause. Examples of dynamic SQL: SPUFI, QMF These two accept almost any SQL statement, and process and execute the SQL statements dynamically.
Syntax for Sql EXECUTE IMMEDIATE _____________________________________________ Exec Sql Execute Immediate :Host-var End-Exec. Host-var Cobol host variables that contains the statement string to be translated, bound and executed. Must contain data compatible with the DB2 varchar data type.
Where a host var would not used to allowed in a static statement. [as in from ?] As column specifications [ as in select ?,?,?] As both elements in a comparison [as in where ? = ?] As both elements of an arithmetic operation [as in where invtotal =?+?]
Processing for a Dynamic non- Processing for a select statement executed Dynamic non-select once statement executed more than once
Processing for a Dynamic non-select statement that produces a known results table
Processing for a Dynamic non-select statement that produces a unknown results table
Execute Immediate
Prepare Execute
Declare Cursor Prepare Describe Allocate storage for host variables Open Fetch close
Example
Prepare
The SQL statement is prepared and assigned a unique label. A prepared statement can be executed several times in a program General format
Exec Sql Execute prepared-statement-name [using { :host-var[: host-var] Descriptor Sqlda -name} End-Exec. ________________________________________________________ 1. Symbolic name specified on the prepare statement the processed the prepared statement u want to execute. 2. Cobol host variable whose name will be substituted for a parameter marker in the prepared sql statement. 3. Sqlda-name that contain descriptions of the host variables whose values are to be substituted for parameter markers in the prepared statement.
Execute
Executes the SQL statement
Execute Immediate
Prepares and executes the SQL statements in one shot. Syntax.
Steps
Declare a cursor for the statement name. Prepare the statement. Open the cursor Fetch rows from result table. Close the cursor. Example
Declare
Exec Sql
Open
Fetch
Close
Declare cursor-name for prepared-select-name End-Exec. __________________________________________________________ Exec Sql Open cursor-name using {:host-var Descriptor sqlda-name} End-Exec. _________________________________________________________ Exec Sql Fetch cursor-name Into {:host-var[:host-structure]} [using Descriptor sqlda-name] End-Exec.
Example
Steps Involved:
Include an SQLCA Load the input SQL statement into a data area. Prepare and execute the statement Include an SQLDA Declare a cursor & prepare the variable statement Obtain the information about the type and size of each column of the result table Open the cursor; Fetch a row Eventually close cursor and free main storage Handle any errors that may arise. SQLDA must be explicitly coded and DB2 does not provide any include file.
TABLE CONTENTS SYSCOLUMNS Table contains one row for every column of each table and view SYSRELS Table contains one row for every referential constraint. SYSFOREIGNKEYS Table contains one row for every column or every foreign key. SYSVIEWS Table Contains One or More Row for Each View. SYSVIEWDEPT Table records the dependencies of views on tables and other views. ___________________________________________________________ SELECT NAME, COLTYPE, LENGTH FROM SYSIBM.SYSCOLUMNS WHERE TBNAME=DEPT AND TBCREATOR=IKSA01;
SQLDA Format
WITH HOLD option is not available for CURSORS coded in a CICS-DB2 program.
For Update & Delete, use Cursors to improve performance. For non-cursor Update and Delete statements, always use primary key in the WHERE clause. Avoid SELECT * in Embedded SQL program. Filter data using WHERE clause Use joins instead of correlated sub select statements.
INDEX Considerations Using an INDEX can Save a tablespace scan Avoid a sort. DB2 decides on use of an INDEX. A table can have more than one Index on different Columns. Access paths :Tablespace Scan Clustered Index Scan, Non-clustered Index Scan.
CICS provides an interface to DB2, but DB2 requires its own region independent of the CICS region. Both CICS and DB2 require its own unique regions under the OS. CICS application programs can be issue a set of commands for SQL services in order to access the DB2 database.
Locks
DB2 allow more than one application to access the same data at essentially same time. This is known as concurrency. To control such undesirable results such as lost updates, access to uncommitted data and unrepeatable reads, concurrency must be controlled. DB2 uses lock to control concurrency. DB2 locks can be compared in four basic aspects: object, size, duration, and mode. Object of lock is the resource being locked. Size of lock can be page, table or tablespace Duration of lock is the length of time the lock is held. It varies according to when the lock is acquired and released. This is controlled by bind options ACQUIRE, RELEASE and ISOLATION. Mode of lock tells what access to the locked object is permitted to the lock owner and to any concurrent application processes.
Deadlock: A deadlock occurs when 2 or more application processes each hold lock on resources that the others need and without which they cannot proceed. After the end of the preset time interval, DB2 can rollback the current unit of work for one of the processes, or request the process to terminate. That frees the locks and allows the remaining processes to continue. ACQUIRE and RELEASE options: These options of BIND or REBIND are used to specify when DB2 is to lock the tables or tablespaces and when to release the locks. ACQUIRE (ALLOCATE)/RELEASE (DEALLOCATE) This option avoids deadlocks by locking your resource at the beginning. Use this option if processing efficiency is more important than concurrency. All tables or tablespaces are locked when the plan is allocated and released only when the plan is terminated. This option reduces concurrency.
ACQUIRE (USE)/RELEASE (DEALLOCATE): The table or tablespace is locked only when it is needed while running. All tables and tablespaces are unlocked only when the plan terminates.
ACQUIRE(USE)/RELEASE(COMMIT) : This is the default option & provides greater concurrency. A table or tablespace is locked only when needed. The lock is released when a SQL COMMIT or ROLLBACK statement is issued or when the application process terminates. This option can increase the frequency of deadlocks
ISOLATION option.
This option on BIND or REBIND helps DB2 release S or U page locks as soon as possible. The ISOLATION option does not apply to changed data. All changed data remains locked at least until the next commit point. ISOLATION option can be either CS or RR The
REPEATABLE READ(RR)
Page locks are held at least until the next commit point. With this option, if the application process returns to the same page and reads the same row again, the data cannot be changed. The application process is assured that SQL SELECT statements it issues are repeatable. Use Repeatable Read when your application has to retrieve the same rows several times.
Numeric Limits
DB2 Limits
Exercises
Code and execute CREATE statements to create EMPLOYEE & DEPT Tables. Table Specifications Customer Details
Account Details
Using the Basic Select: Display all the rows and columns from Customer table. Display Acc_no,Acc_Type, Name, Balance from Customer table List all the information for any account type where Rate_o_int is unknown Display the Acc_no, Name , Acc_Type, DOB and Balance of all Customers who have less than 4000 Balance. Sort the results by name. List the Accounttypes & MinBal where Rate_o_int is unknown. Show Acc_no, name, address and phone no of customers whose accounttype is either CA or SB. Produce a list of customers whose name begins with V. Sort the list by name. Display name, Acc_type & balance. for customers with a balance greater than 10,000. Produce a list of customers whose Balance is < 3,000. List the output in descending order by of balance.
Using the Complete Select Statement Display the sum of all balances along with the company average balance balance, minimum and maximum balance Which customer name comes first alphabetically? How many different acctypes do customers have? Show the average balance for each acc_no. List the average balance for each acc_type 50,00.00 Display name, Acc_no and acc_type name in all account types whose Acc_type begins with C. Show the average balance for men and the average balance for women for each acc_type. Identify the acc_type by name. Order the results by descending balances within account details. Produce a report that gives the no. of customers and total balance for acc_types that begin with C, S or F . List the results in order by acc_type balance. where the average balance is greater than
Using Select Statement with Subqueries Show the name and acc_type of customers in all phone nos where phone no includes 472 Display name and acc_type of customers in all phone nos where phone no includes 472 List each acc_type that has an average balance less than banks average balance. Display list of all acc_no of female customers and male customers in all acc_type where acc_type includes "SB" Write a program to insert data into the customer and account details table after accepting the required data from the terminal Write a program to list all the customersin the bank and write the result to a sequential file Write a program to update the account details table by accepting the acc type from the terminal
Source Program
SQL
DBRM
Bind Package
Bind Plan
Load Module
Source Program
SQL
Compilation
DBRM
Bind Package
Bind Plan
Load Module