12.07.2015 Views

DB2 data manipulation

DB2 data manipulation

DB2 data manipulation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>DB2</strong> 9 Application Development exam 733 prep,Part 2: <strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Learn the fundamental conceptsSkill Level: IntermediateSunil Sabat (sunil_sabat@yahoo.com)Technical Alliance ManagerIBM15 Feb 2007Learn about the fundamental concepts of <strong>data</strong> <strong>manipulation</strong> in <strong>DB2</strong>® <strong>data</strong>bases. Thisis Part 2 in a series of nine tutorials that you can use to help prepare for the <strong>DB2</strong> 9Application Development Certification exam (Exam 733).Section 1. Before you startWhat is this tutorial about?This tutorial covers the fundamental concepts of <strong>data</strong> <strong>manipulation</strong> in <strong>DB2</strong><strong>data</strong>bases, including the following topics:• Changing <strong>data</strong> (insert, update, delete)• Querying a <strong>data</strong>base across multiple tables or views• Using materialized query tables (MQTs)• Using <strong>DB2</strong> SQL functions and stored procedures• Using common table expressions• Determining when to use cursors in a SQL program• Identifying types of cursors (read only, up<strong>data</strong>bale, scrollable)• Identifying the scopes of cursors<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 1 of 25


developerWorks®ibm.com/developerWorks• Manipulating cursors• Ability to manipulate large objects (LOBs) (such as Character LargeObjects [CLOBs] and Binary Large Object [BLOBs])• Managing a unit of work (for instance, a transaction)This is the second in a series of seven tutorials that you can use to help prepare forthe <strong>DB2</strong> 9.1 Family Application Development Certification exam (Exam 733). Thematerial in this tutorial primarily covers the objectives in Section 2 of the exam,entitled "Data <strong>manipulation</strong>."You do not need a copy of <strong>DB2</strong> to complete this tutorial. However, you candownload a free copy of <strong>DB2</strong> Express-C from the <strong>DB2</strong> Express-C downloads page.Who should take this tutorial?To take the <strong>DB2</strong> 9.1 Family Application Development exam, you must have alreadypassed the <strong>DB2</strong> 9.1 Family Fundamentals exam (Exam 700). You can use the <strong>DB2</strong>Family Fundamentals tutorial series (refer to the Resources section) to prepare forthat test. It is a very popular tutorial series that has helped many people understandthe fundamentals of the <strong>DB2</strong> family of products.This tutorial is one of the tools that can help you prepare for Exam 733. You shouldalso review the Resources section at the end of this tutorial for more information.Application development involves <strong>data</strong> retrieval and <strong>manipulation</strong>. In <strong>DB2</strong>, theseprocesses include several methods. While these methods can be programmed indifferent languages, the concepts remain same no matter what the implementationlanguage. This tutorial is the first step you should take before embarking onprogramming applications for <strong>DB2</strong>.Section 2. Change and access <strong>data</strong>Change <strong>data</strong>Changing <strong>data</strong> is a key process to understand when designing a <strong>data</strong>baseapplication. It depends on several factors:• The <strong>data</strong> model and meta<strong>data</strong> (What are the catalog <strong>data</strong>, types, limits,and checks that you must deal with?)• Business requirements (How do you need to identify and modify the <strong>data</strong>in the <strong>data</strong>base?)<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 2 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®• Authorization and security at the user, table, and column level (Is aparticular change allowed?)• Interfaces to access the <strong>data</strong> (How do you interface with the changing<strong>data</strong>?)Which <strong>DB2</strong> capabilities should you use in the design of an application? Systemcatalog <strong>data</strong> cannot be modified by the user. Catalog tables and views storemeta<strong>data</strong> about the logical and physical definition of the <strong>data</strong>. The SYSIBM schemaowns the tables, while views for these tables are owned by the SYSCAT schema.You can query the catalog to get useful information. In order to make appropriatechoices, you need to consider both the <strong>data</strong>base design and target environments foryour application. For example, you can choose to enforce some business rules inyour <strong>data</strong>base design instead of including the logic in your application.The capabilities you use and the extent to which you use them can vary greatly. Thecapabilities that you need to consider include:• Accessing <strong>data</strong> using:• Embedded SQL, including embedded SQL for Java (SQLJ)• <strong>DB2</strong> Call Level Interface (<strong>DB2</strong> CLI), Open Database Connectivity(ODBC), and JDBC• Microsoft specifications• Perl DBI• Query products• Controlling <strong>data</strong> values using:• Data types (built-in or user-defined)• Table check constraints• Referential integrity constraints• Views using CHECK OPTION• Application logic and variable types• Controlling the relationship between <strong>data</strong> values using:• Referential integrity constraints• Triggers• Application logic• Executing programs at the server using:<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 3 of 25


developerWorks®ibm.com/developerWorks• Stored procedures• User-defined functions• TriggersThe key advantage in transferring logic focused on <strong>data</strong> from the application to the<strong>data</strong>base is that your application becomes more independent of the <strong>data</strong>. The logicsurrounding your <strong>data</strong> is centralized in one place, the <strong>data</strong>base. This means that youcan change <strong>data</strong> or <strong>data</strong> logic once and affect all applications that depend on that<strong>data</strong> immediately.This latter advantage is very powerful, but you must also consider that any <strong>data</strong> logicput into the <strong>data</strong>base affects all users of the <strong>data</strong> equally. You must considerwhether the rules and constraints that you wish to impose on the <strong>data</strong> apply to allusers of the <strong>data</strong> or just the users of a single application.Your application requirements may also help you decide whether to enforce rules atthe <strong>data</strong>base or at the application. For example, you may need to process validationerrors on <strong>data</strong> entry in a specific order. In general, you should do this type of <strong>data</strong>validation in the application code. You should also consider the computingenvironment where the application is used. You need to consider the differencebetween performing logic on the client machines and running the logic on the(usually more powerful) <strong>data</strong>base server machines using either stored procedures,user-defined functions (UDFs), or a combination of both. In some cases, the correctapproach is to include enforcement in both the application (perhaps due toapplication-specific requirements) and in the <strong>data</strong>base (perhaps due to otherinteractive uses outside the application).Access <strong>data</strong>In a relational <strong>data</strong>base, you must use SQL to access your desired <strong>data</strong>. However,you have a choice as to how to integrate that SQL into your application. You canchoose from the following interfaces and their supported languages:• Embedded SQL• C/C++• COBOL• FORTRAN• The Java® language (through SQLJ or JDBC)• REXX• <strong>DB2</strong> CLI and ODBC• Microsoft specifications, including ADO.NET and OLE DB• Visual Basic, Visual C++ , and .NET languages<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 4 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®• Perl DBI• Perl• PHP• Query products like Lotus Approach, IBM Query Management Facility,Microsoft Access, or Microsoft ExcelYour program must establish a connection to the target <strong>data</strong>base server before itcan run any executable SQL statements. This connection identifies both theauthorization ID of the user who is running the program, and the name of the<strong>data</strong>base server on which the program is to be run. Generally, your applicationprocess can only connect to one <strong>data</strong>base server at a time. This server is called thecurrent server. However, your application can connect to multiple <strong>data</strong>base serverswithin a multisite update environment. In this case, only one server can be thecurrent server.Your program can establish a connection to a <strong>data</strong>base server either explicitly, usinga connect statement, or implicitly, by connecting to the default <strong>data</strong>base server. Javaapplications can also establish a connection through a Connection instance.Query a <strong>data</strong>base across multiple tablesYou can query <strong>data</strong> from one or more tables using a SELECT statement. You needproper authorization to access the <strong>data</strong> that you query. The <strong>data</strong> returned is knownas a result set.A SELECT statement only specifies the criteria for the <strong>data</strong> that a result set mustfetch. It does not specify the manner in which <strong>DB2</strong> returns it. The <strong>DB2</strong> optimizermakes the latter decision by constructing an access plan based on current <strong>data</strong>basestatistics from the system catalog tables, and the type of plans it has been instructedto consider.Now look at some sample SELECT statements. The following statement selects allstore names and product names from the store and product tables:SELECT A.STORE_NAME, B.PRODUCT_NAME FROM STORE A, PRODUCT BStore_name is a column in the table named store. Product_name is a column inthe table named product.Now look at another example. In the employee table, you select the departmentnumber (WORKDEPT) and maximum departmental salary (SALARY) for alldepartments whose maximum salary is less than the average salary in all otherdepartments:SELECT WORKDEPT, MAX(SALARY)FROM EMPLOYEE EMP_COR<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 5 of 25


developerWorks®ibm.com/developerWorksGROUP BY WORKDEPTHAVING MAX(SALARY) < (SELECT AVG(SALARY)FROM EMPLOYEEWHERE NOT WORKDEPT = EMP_COR.WORKDEPT)Work with MQTsThe definition of an MQT is based upon the result of a query. MQTs can significantlyimprove the performance of queries. This tutorial introduces you to MQTs, summarytables, and staging tables, and shows you, by way of working examples, how to getup and running with materialized query tables.An MQT is a table whose definition is based upon the result of a query. The <strong>data</strong>that is contained in an MQT is derived from one or more tables on which the MQTdefinition is based. Summary tables (or automatic summary tables [ASTs]), whichare familiar to IBM <strong>DB2</strong> for Linux, UNIX, and Windows users, are considered to be aspecialized type of MQT. The fullselect that is part of the definition of a summarytable contains a GROUP BY clause summarizing <strong>data</strong> from the tables that arereferenced in the fullselect.You can think of an MQT as a kind of materialized view. Both views and MQTs aredefined on the basis of a query. The query on which a view is based is run wheneverthe view is referenced. However, an MQT actually stores the query results as <strong>data</strong>,and you can work with the <strong>data</strong> that is in the MQT instead of the <strong>data</strong> that is in theunderlying tables. MQTs can significantly improve the performance of queries,especially complex queries. If the optimizer determines that a query or part of aquery could be resolved using an MQT, the query might be rewritten to takeadvantage of the MQT. An MQT can be defined at table creation time as maintainedby the system or maintained by the user.The <strong>data</strong> in this type of materialized query table is maintained by the system. Whenyou create this type of MQT, you can specify whether the table <strong>data</strong> is a REFRESHIMMEDIATE or REFRESH DEFERRED. The REFRESH keyword lets you specifyhow the <strong>data</strong> is to be maintained. DEFERRED means that the <strong>data</strong> in the table canbe refreshed at any time using the REFRESH TABLE statement. Neither REFRESHDEFERRED nor REFRESH IMMEDIATE system-maintained MQTs allow insert,update, or delete operations to be executed against them. However, REFRESHIMMEDIATE system-maintained MQTs are updated with changes made to theunderlying tables as a result of insert, update, or delete operations.Following section shows an example of creating a REFRESH IMMEDIATEsystem-maintained MQT. The table, which is named EMP, is based on theunderlying tables EMPLOYEE and DEPARTMENT in the SAMPLE <strong>data</strong>base.Because REFRESH IMMEDIATE MQTs require that at least one unique key fromeach table referenced in the query appear in the select list, you first define a uniqueconstraint on the EMPNO column in the EMPLOYEE table and on the DEPTNOcolumn in the DEPARTMENT table. The DATA INITIALLY DEFERRED clausesimply means that <strong>data</strong> is not inserted into the table as part of the CREATE TABLEstatement. After being created, the MQT is in check pending state and cannot be<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 6 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®queried until the SET INTEGRITY statement has been executed against it. TheIMMEDIATE CHECKED clause specifies that the <strong>data</strong> is to be checked against theMQT's defining query and refreshed. The NOT INCREMENTAL clause specifies thatintegrity checking is to be done on the whole table.CONNECT TO SAMPLE...ALTER TABLE EMPLOYEE ADD UNIQUE (EMPNO)ALTER TABLE DEPARTMENT ADD UNIQUE (DEPTNO)CREATE TABLE EMP AS (SELECT E.EMPNO, E.FIRSTNME, E.LASTNAME, E.PHONENO, D.DEPTNO,SUBSTR(D.DEPTNAME, 1, 12) AS DEPARTMENT, D.MGRNO FROM EMPLOYEE E, DEPARTMENT DWHERE E.WORKDEPT = D.DEPTNO)DATA INITIALLY DEFERRED REFRESH IMMEDIATESET INTEGRITY FOR EMP IMMEDIATE CHECKED NOT INCREMENTALSection 3. Functions and expressionsWhat are <strong>DB2</strong> SQL functions?A <strong>data</strong>base function is a relationship between a set of input <strong>data</strong> values and a set ofresult values. There are two types of functions: built-in and user-defined.• Built-in SQL functions are provided with the <strong>data</strong>base manager. Theyprovide a single result value and are identified as part of the SYSIBMschema. Examples of built-in SQL functions include column functionssuch as AVG, operator functions such as +, casting functions such asDECIMAL, and other functions, such as SUBSTR.• User-defined functions (UDFs) are functions that are registered to a<strong>data</strong>base in SYSCAT.FUNCTIONS (using the CREATE FUNCTIONstatement). UDFs are never part of the SYSIBM schema. One such set offunctions is provided with the <strong>data</strong>base manager in a schema calledSYSFUN.<strong>DB2</strong> allows users and application developers to extend the functionality of the<strong>data</strong>base system by applying their own function definitions in the <strong>data</strong>base engineitself. Applications based on UDFs perform better than applications that retrieverows from the <strong>data</strong>base and apply those functions on the retrieved <strong>data</strong>. Extending<strong>data</strong>base functions also lets the <strong>data</strong>base exploit the same functions in the enginethat an application uses, providing more synergy between application and <strong>data</strong>base.The use of functions contributes to higher productivity for application developersbecause it is more object oriented. For example, you may store the price for aproduct in U.S. dollars, but you may want a particular application to quote the pricein U.K. pounds. You can use a function to accomplish this:<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 7 of 25


developerWorks®ibm.com/developerWorksSELECT UNIT_PRICE, CURRENCY('UK',UNIT_PRICE) FROM PRODUCT WHERE PRODUCT_ID = ?FENCED and NOT-FENCED modesYou can create functions in C/C++, the Java language, or OLE. A function can run inFENCED or NOT-FENCED modes. You should develop a function in FENCED modebefore migrating to NOT-FENCED mode. A NOT-FENCED process is faster, as it uses<strong>DB2</strong> agent memory, whereas a FENCED process runs in its own db2udf process. AFENCED process uses shared memory to communicate with the calling agent.FENCED functions are stored in sqllib/function and unfenced functions arestored in sqllib/unfenced.<strong>DB2</strong>-supplied SQL functionsNow look at examples of a few SQL functions. The first example selects the title andprice for all books in a table. If the price for a given title is NULL, the price shown is0.00SELECT TITLE, COALESCE(PRICE, 0.00) AS PRICEFROM TITLES;Next, you'll see an example that returns a company name and the number ofcharacters in that company's name:SELECT COMPANYNAME, LENGTH(COMPANYNAME)FROM CUSTOMERSNow see how you can return the five right-most characters of each author's firstname:SELECT RIGHT(AU_FNAME, 5)FROM AUTHORSThis next example, using the project table, sets the host variable AVERAGE(decimal(5,2)) to the average staffing level (PRSTAFF) of projects in thedepartment (DEPTNO) called D11.SELECT AVG(PRSTAFF)INTO :AVERAGEFROM PROJECTWHERE DEPTNO = 'D11'There are many other SQL functions defined in the <strong>DB2</strong> SQL Reference manual.You can always write your own SQL function if <strong>DB2</strong> does not provide one.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 8 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®Use common table expressionsA common table expression is a local temporary table that can be referenced manytimes in an SQL statement. This temporary table only exists for the duration of theSQL statements that define it. Every time the common table is referenced, theresults are the same. A temporary table is defined during an SQL statement usingthe WITH clause. Here's the syntax:WITH AS ( ), AS (


developerWorks®ibm.com/developerWorkseducation level greater than 16 are included.The listing also includes a common table expression with the name PAYBYED (shortfor "pay by education"). It uses the PAYLEVEL table to determine the education level,hire year, and average pay of employees hired in the same year who have identicaleducation levels. The columns returned by this table have been given differentnames (EDUC_LEVEL, for example) from the column names used in the select list.Finally, you get to the actual query that produces the desired result. The two tables(PAYLEVEL, PAYBYED) are joined to determine those individuals who have a salarythat is less than the average pay for people hired in the same year. Note thatPAYBYED is based on PAYLEVEL, so PAYLEVEL is effectively accessed twice in thecomplete statement. Both times the same set of rows are used in evaluating thequery.After you define a common table expression, you can use it in an SQL statement asyou would any other table. You can use common table expression as many times asyou wish. You can even create a common table expression based on a previouslycreated common table expression.Section 4. When to use cursors in an SQL programWhat is a cursor? When do you need one?A cursor is the mechanism used to manipulate multiple-row answer sets from a <strong>DB2</strong>query. There are two situations in which you need a cursor:• When a query returns more than one row.A SELECT statement with an INTO clause is very simple to code, butallows only one row to be returned. There is no clause in a SELECTstatement that allows multiple rows to be processed directly, so a cursormust be used.• When you want to update or delete one or more rows, but need toexamine their contents first.The simplest way to update or delete rows is to use a statement like this:UPDATE staff SET salary = salary * 1.10 WHERE id = 100orDELETE FROM staff WHERE id = 100However, these statements, called searched updates or deletes, don'tallow the program to check the contents of the rows before updating or<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 10 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®deleting. You could instead use a cursor in conjunction with a positionedupdate or delete. The latter are also known as "Update Where Current Of"and "Delete Where Current Of", where "Current Of" refers to the row atwhich the cursor is currently positioned.To use a cursor, you must declare it, open it, fetch rows from it one at a time,(optionally) update or delete the rows at which it is positioned, and close it. You'll seemore details and examples in this and later sections.How cursor support varies by <strong>DB2</strong> interfaceCursor support and terminology varies among the various <strong>DB2</strong> programminginterfaces. Now take a brief look at the differences. Later, in the Manipulatingcursors section, you'll see some examples for embedded SQL.Basic support for cursors is provided through the SQL language itself, through theDECLARE CURSOR, OPEN, FETCH, and CLOSE statements.You can perform positioned updates and deletes through the following syntax:UPDATE [OR DELETE] ... WHERE CURRENT OF Various interfaces build on SQL's cursor support in various ways. Traditionalprogramming languages, such as C, C++, and COBOL, provide explicit support forthe declaration and use of cursors in static and dynamic embedded SQL. Rows canonly be processed one at a time in a forward direction.The SQL procedure language supports cursors much like C does, but with the WITHRETURN clause added to support returning a result set to the caller of a storedprocedure.In the <strong>DB2</strong> CLI, cursors are not explicitly declared, but CLI creates themautomatically when the SQLExecute() or SQLExecDirect() functions areinvoked. CLI provides additional capabilities that build on cursor support, such as theability to scroll backwards, to receive an array of rows at one time, and to moveforward by more than one row.In JDBC, a cursor is created automatically when a ResultSet object is created.Additional capability is available that is similar to that in CLI.SQLJ's cursor support is a essentially a blend of what's in JDBC and SQL, but inSQLJ the equivalent of a cursor is called an iterator.Interactive tools, such as the <strong>DB2</strong> Command Line Processor (CLP) and ControlCenter, do not let you use cursors directly. However, the tools themselves usecursors. When you execute a SELECT statement through the CLP, or do a samplecontents request for a table in the Control Center, a cursor is used to return therows.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 11 of 25


developerWorks®ibm.com/developerWorksA simple example: Cursor usage in static embedded SQLBefore cursors are discussed further, look at an example of a very simple cursor instatic embedded SQL (a C program).EXEC SQL DECLARE C0 CURSOR FOR SELECT DEPTNUMB, DEPTNAME FROM ORG;EXEC SQL OPEN C0;EXEC SQL FETCH C0 INTO :DEPTNUMB, :DEPTNAME;WHILE (SQLCA.SQLCODE != 100) /* CONTINUE UNTIL THE END OF THE RESULT SET */{PRINTF(" %8D %-14S\N", DEPTNUMB, DEPTNAME);EXEC SQL FETCH C0 INTO :DEPTNUMB, :DEPTNAME;}EXEC SQL CLOSE C0;This code prints the number and name of every department in the org table. TheDECLARE CURSOR statement provides the query to be used, and the OPENstatement prepares the result set for the query. A FETCH statement is usedrepeatedly to move the values of the result set columns into program variables, onerow at a time, until the end of the result set is reached (SQLCODE = +100), atwhich point the cursor is closed.Section 5. Identify types of cursorsCursor characteristicsThere are three primary characteristics of cursors:• The cursor type: Read-only, up<strong>data</strong>ble, or ambiguous• The cursor direction(s): Forward-only or scrollable• The cursor scopeThese characteristics are discussed in the next few sections.Cursor type<strong>DB2</strong> handles each of the three cursor types somewhat differently, with thedifferences being primarily in the realm of performance. Take a look at each of them.Read-only cursors<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 12 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®When <strong>DB2</strong> knows that a cursor is read-only, certain performance advantages canapply:• <strong>DB2</strong> is usually able to perform record blocking to retrieve multiple rowsfrom the server at one time, and does not need to worry about acquiringlocks that allow rows to be updated.• <strong>DB2</strong> can sometimes choose a better access plan for the query.If you know that a cursor will not be used for updating or deleting rows, you shoulddesignate it as read-only by adding FOR READ ONLY (or FOR FETCH ONLY) to theSELECT statement for the cursor. A cursor is also (automatically) classified asread-only if its SELECT statement is a join of multiple tables or includes such clausesas ORDER BY or GROUP BY.Up<strong>data</strong>ble cursorsA cursor is up<strong>data</strong>ble if the FOR UPDATE clause is specified in its SELECTstatement, meaning that rows are updated through an Update Where CurrentOf statement. There can only be one table (or view) referenced in the SELECTstatement. Because it must maintain <strong>data</strong> integrity, <strong>DB2</strong> can only perform a minimalamount of optimization for up<strong>data</strong>ble cursors.The term deletable cursor is used in the SQL Reference as a way to help defineup<strong>data</strong>ble cursors. The two terms mean almost the same thing. See the descriptionof DECLARE CURSOR in the SQL Reference for details.Ambiguous cursorsAs the name suggests, a cursor is ambiguous when <strong>DB2</strong> cannot determine from thecursor definition whether it is read-only or up<strong>data</strong>ble. In other words, when thecursor's SELECT statement has neither FOR READ ONLY nor FOR UPDATEspecified, it is ambiguous. For an ambiguous cursor, <strong>DB2</strong> chooses whether to dorecord blocking for the select based on the value of the BLOCKING option on theBIND command for the application. If blocking is performed but updates occur, thereis a negative performance impact, so it's always best to avoid ambiguous cursorswhen possible.Cursor directionThe cursor support for <strong>DB2</strong> embedded SQL applications allows only one row to beprocessed at a time, and only in a forward direction. In other words, each FETCHstatement returns the next row of the result set to the application, and there is noother way for the application to obtain rows through the cursor.CLI and the Java platform support scrollable cursors. These cursors can bepositioned at an absolute row number in the result set (either forward or backwardfrom the current position) or moved a relative number of rows (forward or backward)from the current position. For more information on scrollable cursors, see the fourth<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 13 of 25


developerWorks®ibm.com/developerWorksand fifth tutorials in this series (on ODBC/CLI and the Java platform, respectively,refer to the Resources section).Cursor scopeWhen referring to the cursor scope, it means the period during which it is availablefor fetching rows. That period begins when the cursor's OPEN statement hascompleted successfully. By default, the scope of the cursor ends when the cursor isclosed or when a commit is executed. As you'll see on the next panel, having acommit end the scope can be a nuisance, but there's a way around it.WITH HOLD cursorsUsually, applications should be written to have COMMIT statements executed fairlyfrequently. These commit cause locks to be released, and they minimizeconcurrency issues between applications. However, committing can cause aproblem when it comes to cursors. This is where the WITH HOLD clause comes in.As an example, consider an application in which an SQL statement reads 10,000rows using a cursor. The application checks each row's contents and updates therow to set the value of a status column. Waiting to commit until all 10,000 rows havebeen processed could cause lock conflicts, so it commits after every 20 rows. Bydefault, however, the commit closes the cursor, so the position in the result setwould be lost and the application would have to do some special processing tocontinue properly from where it left off.The solution to this problem is to change the cursor definition to include the WITHHOLD clause. This causes a commit to leave the cursor open and avoid releasinglocks necessary to maintain the cursor's positioning. In other words, the WITH HOLDclause extends the scope of a cursor beyond a commit. Here's an example of aWITH HOLD cursor:DECLARE C1 CURSOR WITH HOLD FOR SELECT * FROM STAFFThe WITH HOLD clause has no effect on what happens to a cursor during a rollback.The cursor is closed and all associated locks are released.Section 6. Manipulate cursorsOverview of cursor processing<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 14 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®In this section, you'll look more closely at how cursors are used in embedded SQLapplications. The basic steps, once again, are declare, open, fetch, update/delete(optional), and close.To help understand the concept of a cursor, assume that <strong>DB2</strong> builds a result table tohold all the rows retrieved by executing a SELECT statement. A cursor makes rowsfrom the result table available to an application by identifying or pointing to a currentrow of this table. When a cursor is used, an application can retrieve each rowsequentially from the result table until an end-of-<strong>data</strong> condition (that is, the NOTFOUND condition, SQLCODE +100 or SQLSTATE 02000) is reached. The set of rowsobtained as a result of executing the SELECT statement can consist of zero, one, ormore rows depending on the number of rows that satisfy the search condition.Declare a cursor in embedded SQLThe syntax of the DECLARE CURSOR statement is very simple. Here's an examplefor static SQL:DECLARE C1 CURSOR FOR SELECT * FROM STAFFThe use of the statement can be a bit confusing because the statement is notexecutable. In other words, the statement is handled exclusively by the preparationphase of the embedded application, and when the DECLARE statement is reachedduring program execution, nothing happens. The work gets done when the cursor isopened. The only requirement is that the DECLARE CURSOR statement appearbefore the OPEN statement in the source file. It does not even need to be locatedwithin the same C function. Every cursor name must be unique within the source filein which it is declared.If you're using dynamic SQL, the DECLARE CURSOR statement is a bit different.Instead of including the syntax of the SELECT statement, you would use a statementname. That statement name must match the name used when preparing the relatedSELECT statement. For example:EXEC SQL PREPARE STMT1 FROM :STRINGSTMT;EXEC SQL DECLARE C3 CURSOR FOR STMT1;Open a cursor in embedded SQLOpening a cursor prepares the rows of the query result set for use by the program. Italso positions the cursor before the first result row, though that row is not accessibleby the program until a fetch is executed.Often, the open is where the bulk of the query execution time is spent, particularly ifthere's an ORDER BY or GROUP BY clause in the select.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 15 of 25


developerWorks®ibm.com/developerWorksThe syntax of an OPEN statement is very simple. To open a cursor named "c0", you'duse the following:OPEN C0Fetch a cursor in embedded SQLExecuting a fetch against a cursor causes the next row of the result set to be madeavailable to the program, usually by placing the values of the result set columns intohost variables. The nth column in the result set is placed in the nth host variable inthe fetch.For example, if a cursor c0 is declared for Select name, dept, id fromstaff, the statement Fetch c0 into :hv1, :hv2, :hv3 will place the valueof the name column into hv1, dept into hv2, and id into hv3.If any result set column is nullable, a second host identifier (the null indicator) shouldbe used, and <strong>DB2</strong> will store a negative value in that variable to represent a null valuebeing returned. For example, changing the previous example to Fetch c0 into:hv1, :hv2 :hv2ind, :hv3 would allow the program to know if an employeehas a null department.Usually, a fetch is placed within a program loop that is written to continue until anSQLCODE of +100 is returned. At that point, all rows in the result set will have beenfetched.Update and delete rows with a cursorAs previously mentioned, a positioned update or delete can be done on the row atwhich the cursor is positioned. A fetch must have been performed previously and notreturned a SQLCODE of +100 (or an error). Every row in the result set can beprocessed in this manner -- or none of them can, or any number in between. Here'san example:EXEC SQL DECLARE CURSOR C0 FOR SELECT NAME, SALARY FROM STAFF FOR UPDATE OF DEPT;EXEC SQL FETCH C0 INTO :HVNAME, :HVSAL;/* THERE MIGHT BE PROGRAM LOGIC HERE TO CHECK THE EMPLOYEE NAME AND SALARY *//* AND ONLY EXECUTE THE UPDATE IF SOME CRITERIA APPLY */EXEC SQL UPDATE STAFF SET DEPT = :NEWDEPT WHERE CURRENT OF C0;This code retrieves employee information from the STAFF table and allows anemployee's department to be updated. The DECLARE CURSOR statement providesthe query, listing the name and salary columns as the columns to be retrieved, andindicates that the dept column may be updated in some rows. The FETCHstatement places the employee and salary values in program variables. The UPDATEstatement is used to change the value of the dept column in the previously fetchedrow to the value in the program variable newdept.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 16 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®Although not shown here, program logic would normally be used to loop until the endof the result set was reached, and might have been used to only update certainrows.Close a cursorClosing a cursor frees the internal storage for the cursor and makes the cursorunavailable for further use. The syntax is very simple:CLOSE C0By default, closing a cursor does not free the locks it holds. To do so, add the WITHRELEASE clause:CLOSE C0 WITH RELEASEThis makes <strong>DB2</strong> try to release all read locks. However, <strong>DB2</strong> keeps all locks onupdated rows and may need to keep some read locks for other operations oractivities.The cursor can be closed at any time when it's open. That is, there is no need tofetch the entire result set before closing the cursor. Once a cursor is closed, it canbe opened again, and it behaves as if it had not previously been used.Section 7. Manage a unit of workWhat is a transaction?The term unit of work, (UOW), is synonymous with the concept of a transaction. It isdefined as zero or more SQL queries that execute as a single atomic operation. Forexample, when a customer makes an online purchase from the IBM Web site, thereare three steps that must be carried out:1. The inventory of the IBM Web mall must be updated.2. The customer must be charged for the items purchased.3. Each item purchased must be shipped.What would happen if the inventory records were updated and the customer wascharged, but a shipping order entry was never created? Not only would you have an<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 17 of 25


developerWorks®ibm.com/developerWorksangry customer who never received his or her purchase, but you would introduce aninaccuracy into the inventory. Thus, all SQL queries for the purchase must bedefined as a single atomic operation.Steps in a transactionNow see what steps need to go into a <strong>DB2</strong> transaction. Before you start, aconnection must be established with the <strong>data</strong>base against which the transactionexecutes.Start the transaction with an executable statement. An executable statement alwaysoccurs within a transaction. If a program contains an executable statement after atransaction ends, it starts a new transaction. The following are not executablestatements:BEGIN DECLARE SECTIONEND DECLARE SECTIONINCLUDE SQLCAINCLUDE SQLDADECLARE CURSORWHENEVEREnd the transaction in the following way:COMMITROLLBACKThe COMMIT statement ends the transaction and makes the changes visible to otherprocesses. Remember, you should commit regularly and do so exclusively beforeprogram termination. <strong>DB2</strong> automatically rolls back transactions if you do notexplicitly commit them on Windows operating systems. On other operating systems,<strong>DB2</strong> commits all pending transactions during program termination automatically .The ROLLBACK statement returns the <strong>data</strong>base to the state it was in before thetransaction ran. The rollback prevents the changes being applied to the <strong>data</strong>baseafter the last transaction commits. This ensures that either all operations in thetransaction are committed, or none are. <strong>DB2</strong> rolls back changes under the followingconditions:• A log full condition• A system condition that causes the system process to endTo avoid loop failure, use WHENEVER SQLWARNING CONTINUE or WHENEVERSQLERROR CONTINUE before a ROLLBACK statement. A ROLLBACK statement hasno effect on the contents of host variables.You should terminate your application by taking the following steps:<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 18 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®• End your current transaction by COMMIT or ROLLBACK• Release your connection by issuing a CONNECT RESET statement• Free up resources (such as temporary storage, <strong>data</strong> structures, andshared memory)You can have multiple transactions in an application. Within a transaction, you canhave multiple connections to the <strong>data</strong>bases.Connections and transactionsProgramming interfaces have two types of connections: transactional andnontransactional. Although <strong>DB2</strong> supports these concepts, you should be aware thatthere is really only one type of connection to the <strong>data</strong>base -- a transactionalconnection. Thus, every SQL query is part of a transaction. When you run innontransactional mode, the programming interface you are using has enabled afeature called autocommit, which issues a COMMIT statement implicitly after everySQL operation. You must ensure that you do not have autocommit enabled if yourUOW has multiple queries.Savepoints and transactionsA savepoint is a mechanism for undoing work performed by the <strong>data</strong>basemanagement system (DBMS) when a <strong>data</strong>base request fails. Savepoints makenon-atomic <strong>data</strong>base requests behave atomically. If an error occurs duringexecution, the savepoint can be used to undo changes made by the transactionbetween the time the savepoint was started and the time when the savepointrollback was requested.A savepoint allows you to group several SQL statements into a single executableblock. Before the first substatement of the block is executed, a request to start asavepoint block is required. If one of the substatements ends in an error, only thatsubstatement is rolled back. This provides more granularity than a compound SQLstatement, in which a single error causes the entire block to end in an error and rollsback the entire compound SQL statement. At the end of a savepoint block ofstatements, you can either release the savepoint or roll back to the savepoint.Now look at some SQL statements that enable you to create and control savepoints.To set a savepoint, issue a SAVEPOINT SQL statement. To improve the clarity ofyour code, you can choose a meaningful name for the savepoint. For example:SAVEPOINT SAVEPOINT1 ON ROLLBACK RETAIN CURSORSTo release a savepoint, issue a RELEASE SAVEPOINT SQL statement. Forexample:<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 19 of 25


developerWorks®ibm.com/developerWorksRELEASE SAVEPOINT SAVEPOINT1If you do not explicitly release a savepoint with a RELEASE SAVEPOINT SQLstatement, it is released at the end of the transaction.To roll back to a savepoint, issue a ROLLBACK TO SAVEPOINT SQL statement. Forexample:ROLLBACK TO SAVEPOINT SAVEPOINT1Section 8. Work with large objectsWhat is a large object?Large <strong>data</strong> objects, such as images and audio, frequently need to be stored in a<strong>data</strong>base. <strong>DB2</strong> provides specialized <strong>data</strong> types for storing large <strong>data</strong> objects. These<strong>data</strong> types are known as large objects (LOBs). In this section, you'll be exploring theworld of LOBs. As a developer, you should be able to store and retrieve LOBs inyour applications.<strong>DB2</strong> provides three different types of LOB <strong>data</strong> types. All of these <strong>data</strong> types canhouse up to 2 gigabytes of <strong>data</strong>:• CLOB -- Contains up to 2 gigabytes of character <strong>data</strong>• BLOB -- Contains up to 2 gigabytes of binary <strong>data</strong>. This binary <strong>data</strong>could, in essence, be anything (such as an image or an audio file).• Double-Byte Character Large Object (DBCLOB) -- Contains up to 2gigabytes of double-byte character <strong>data</strong>. Note that this <strong>data</strong> type shouldonly be used if the <strong>data</strong>base you created was configured for double-byte<strong>data</strong>.• XML -- Contains up to 2 gigabytes of XML <strong>data</strong>. XML is not covered inthis tutorial, as they are covered later in the tutorial series.LOB values are not stored in a <strong>data</strong>base table. What is actually stored is adescriptor. This descriptor points to the actual location of the LOB. The actual LOBvalues are stored in tablespaces. Tablespaces are units of physical storage. What isactually housed in a LOB column is not the actual LOB <strong>data</strong>, but in fact a pointer tothe LOB <strong>data</strong>. This pointer is known as a "locator." These so called locators areused to represent your LOB values. When you retrieve <strong>data</strong> in a ResultSet, you'll beretrieving locators and not the actual LOB values that they represent. You have to<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 20 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®explicitly ask for LOB values to be retrieved. This retrieval is known as"materialization."When creating a table, you can specify the option of COMPACT or NOT COMPACT. Ifthe option of COMPACT is specified, then the LOB <strong>data</strong> you store takes a minimumamount of space. However, if you perform an update to the LOB column that wouldincrease the size of the LOB stored, then there is a performance penalty. On theother hand, if you specify NOT COMPACT (the default), your LOB values in essencehave leeway to grow.If you specify the option of LOGGED, then updates to a LOB column are recorded inthe system log. Specifying the option of LOGGED provides the most protection for<strong>data</strong> that is housed, so it can be reconstructed in case of a media failure with aforward recovery process. However, this comes at a cost when it comes to diskspace (not to mention the time costs associated to write to the disk). If you don'tspecify one, the option of LOGGED is chosen by default. Now look at some examplesin Java.PREPAREDSTATEMENT PREPAREDSTATEMENT =CONNECTION.PREPARESTATEMENT("INSERT INTO BOOKCOVERS VALUES(?,?)");FILE IMAGEFILE = NEW FILE("C:\\REDBOOKCOVER.JPG");INPUTSTREAM INPUTSTREAM = NEW FILEINPUTSTREAM(IMAGEFILE);PREPAREDSTATEMENT.SETSTRING(1," 0738425826");PREPAREDSTATEMENT.SETBINARYSTREAM(2,INPUTSTREAM,(INT)(IMAGEFILE.LENGTH()));PREPAREDSTATEMENT.EXECUTEUPDATE();The code snippet above takes a file named redbookcover.jpg located in the root ofthe C: drive, and stores it in the <strong>data</strong>base. Note how you associate your file with anInputStream. This InputStream object is used to populate the value of the preparedstatement standing in for your BLOB column.PREPAREDSTATEMENT PREPAREDSTATEMENT =CONNECTION.PREPARESTATEMENT("SELECT BOOKCOVER FROM BOOKCOVERS WHERE BOOKISBN=?");PREPAREDSTATEMENT.SETSTRING(1, "0738425826");RESULTSET RESULTSET = PREPAREDSTATEMENT.EXECUTEQUERY();WHILE (RESULTSET.NEXT()) {// MATERIALIZATION OF THE BLOBBLOB BLOB = RESULTSET.GETBLOB(1);INPUTSTREAM INPUTSTREAM = BLOB.GETBINARYSTREAM();FILE FILEOUTPUT = NEWFILE("C:\\CLONEDREDBOOKCOVER.JPG");FILEOUTPUTSTREAM FO = NEWFILEOUTPUTSTREAM(FILEOUTPUT);INT C;WHILE ((C = INPUTSTREAM.READ()) != -1)FO.WRITE(C);FO.CLOSE();SYSTEM.OUT.PRINTLN("BLOB RETRIEVED");In the Java code segment above, a prepared statement was executed in which theBLOB inserted in the code segment earlier was selected. The important thing to noteis that the actual BLOB is not materialized until the line:INPUTSTREAM INPUTSTREAM = BLOB.GETBINARYSTREAM();<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 21 of 25


developerWorks®ibm.com/developerWorksThe input stream is used to store the retrieved BLOB in a file calledclonedredbookcover.jpg. To retrieve CLOBs, much of the syntax is similar to that ofretrieving a BLOB.FILE FILEOUTPUT = ;NEW;FILE("C:\\CLONEDREDBOOKABSTRACT.TXT");FILEOUTPUTSTREAM FO = ;NEW; FILEOUTPUTSTREAM(FILEOUTPUT);INPUTSTREAM IS = CLOB.GETASCIISTREAM();INT C;WHILE; ((C = IS.READ()) != -1)FO.WRITE(C);FO.CLOSE();In the Java snippet above, the stored CLOB was materialized to a new file namedclonedredbookabstract.txt. This is done by using the getAsciiStream method of theCLOB. Just like you did when retrieving the BLOB, you assign the stream to anInputStream, which then is read from, and in turn write what was read to theFileOutputStream.Section 9. ConclusionSummaryIn this tutorial, you learned many concepts on <strong>data</strong> <strong>manipulation</strong> strategies. Youlearned how to:• Access the <strong>data</strong> in multiple tables and change them• Use MQTs• Use SQL routines• Use common tables• Use cursors• Use CLOB and BLOB <strong>data</strong> types• Program with transactionsWhile this tutorial focused on concepts, further tutorials in this series describe all theinterfaces used to perform <strong>data</strong> <strong>manipulation</strong>.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 22 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®ResourcesLearn• IBM <strong>DB2</strong> Information Management Training and certification page: Find moreinformation on the <strong>DB2</strong> 9 Family Application Development Certification exam(Exam 733) as well as information on classes, certifications available, andadditional resources.• <strong>DB2</strong> 9 Application Development exam 733 prep tutorial series: As mentionedearlier, this tutorial is just one in of nine tutorials designed to help you preparefor the <strong>DB2</strong> 9 Family Application Development Certification exam (Exam 733).The complete list of all tutorials in this series includes:• Database objects and programming methods• Data <strong>manipulation</strong>• XML <strong>data</strong> <strong>manipulation</strong>• Embedded SQL programming• ODBC/CLI programming• .NET programming• Java programming• Advanced programming• User-defined routines• <strong>DB2</strong> 9 Fundamentals certification prep tutorial series: Before you take the <strong>DB2</strong>9 Application Development certification exam (Exam 733), you should havealready taken and passed the <strong>DB2</strong> 9 Fundamentals certification exam (Exam730). Use this series of seven tutorials to prepare for that exam; topics include:• <strong>DB2</strong> planning• <strong>DB2</strong> security• Accessing <strong>DB2</strong> <strong>data</strong>• Working with <strong>DB2</strong> <strong>data</strong>• Working with <strong>DB2</strong> objects• Data concurrency• Introducing Xquery• <strong>DB2</strong> V9 Database administration certification prep series: Prepare for the <strong>DB2</strong> 9Database Administration for Linux, UNIX, and Windows certification exam(Exam 731). A set of seven tutorials covers the following topics:• Server management<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 23 of 25


developerWorks®ibm.com/developerWorks• Data placement• Database access• Monitoring <strong>DB2</strong> activity• <strong>DB2</strong> utilities• High availability: Backup and recovery• High availability: Split mirroring and HADR• <strong>DB2</strong> Information Center: Learn more about <strong>data</strong> <strong>manipulation</strong>. Look particularlyto these sections:• <strong>DB2</strong> Version 9 CLI Guide and Reference, Part 1. See especially Chapter5: Cursors.• <strong>DB2</strong> Version 9 SQL Reference, Volume 2. This covers the SQLstatements related to cursors.• <strong>DB2</strong> Version 9 Application Development Guide: Programming ClientApplications. This covers cursors in different programming environments.• Also check out the sample programs that come with <strong>DB2</strong>. These are shipped insqllib/samples, with various subdirectories for C, Java, and others.• developerWorks resource page for <strong>DB2</strong> for Linux, UNIX, and Windows: Findarticles and tutorials and connect to other resources to expand your <strong>DB2</strong> skills.• Learn about <strong>DB2</strong> Express-C, the no-charge version of <strong>DB2</strong> Express Edition forthe community.• Stay current with developerWorks technical events and webcasts.• developerWorks Information Management zone: Learn more about <strong>DB2</strong>. Findtechnical documentation, how-to articles, education, downloads, productinformation, and more.Get products and technologies• Download a free trial version of <strong>DB2</strong> Enterprise 9.• <strong>DB2</strong> Express-C: Now you can use <strong>DB2</strong> for free. Download this no-chargeversion of <strong>DB2</strong> Express Edition for the community that offers the same core<strong>data</strong> features as <strong>DB2</strong> Express Edition and provides a solid base to build anddeploy applications.• Build your next development project with IBM trial software, available fordownload directly from developerWorks.Discuss• Participate in the discussion forum for this content.• Participate in developerWorks blogs and get involved in the developerWorks<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 24 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.


ibm.com/developerWorksdeveloperWorks®community.About the authorSunil SabatSunil Sabat is the technical alliance manager at IBM in Information ManagementSoftware Group. He has earned an M.S. in computer engineering, an M.S. incomputer science, and an M.B.A. He is an IBM Certified IT Specialist. He is alsoMicrosoft Certified and CompTIA professional. Having worked on all major <strong>data</strong>basesfor more than 10 years, Sunil has a passion for <strong>data</strong>base technology and itsoptimized use to solve business and human problems. Recently, he has worked withISVs, enabling them for XML technology using <strong>DB2</strong> 9. You can contact Sunil atsunil_sabat@yahoo.com.Trademarks>IBM and <strong>DB2</strong> are trademarks or registered trademarks of IBM Corporation in theUnited States, other countries, or both.This is the second trademark attribution statement.<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>© Copyright IBM Corporation 1994, 2007. All rights reserved. Page 25 of 25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!