DB2 Application Development overview
description
Transcript of DB2 Application Development overview
![Page 1: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/1.jpg)
S E M E S T E R 1 , 2 0 1 3 / 2 0 1 4
DB2 APPLICATION DEVELOPMENT
OVERVIEW
![Page 2: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/2.jpg)
DB2 APPLICATION DEVELOPMENT OVERVIEW
• Server-side development• Stored Procedures• User-defined functions• Triggers
• Client-side development• Embedded SQL• Static vs. Dynamic SQL• CLI/ODBC• JDBC / SQLJ / pureQuery
![Page 3: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/3.jpg)
DB2 APPLICATION DEVELOPMENT OVERVIEW
• Server-side development (at the DB2 database server):• Routines (Stored Procedures, UDFs)• Database objects (Triggers)
• Client-side development (at the client):• May require a DB2 client or driver to be installed• Database applications (in C/C++, .NET, Cobol, Java, etc)
![Page 4: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/4.jpg)
DB2 APPLICATION DEVELOPMENT OVERVIEW
![Page 5: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/5.jpg)
STORED PROCEDURE
![Page 6: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/6.jpg)
STORED PROCEDURE
![Page 7: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/7.jpg)
STORED PROCEDURE
• Usually contain one or more SQL statements as well as
procedural (business) logic• Executed and managed by DB2 (server-side objects)• Can be written using SQL PL, C/C++, Java, Cobol,
CLRsupported languages, OLE, PL SQL, etc.• Benefits for using stored procedures include:• Centralized business logic that promotes code re-use• Improved security• Improved performance
![Page 8: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/8.jpg)
CREATING YOUR FIRST STORED PROCEDURE
• Using the Command Line Processor:db2=> connect to sampledbdb2=> create procedure p1 begin end
![Page 9: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/9.jpg)
BASIC STORED PROCEDURE STRUCTURE
![Page 10: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/10.jpg)
BASIC STORED PROCEDURE STRUCTURE: COMPOUND STATEMENTS
![Page 11: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/11.jpg)
VARIABLE DECLARATION & ASSIGNMENTS
![Page 12: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/12.jpg)
EXAMPLE: STORED PROCEDURE WITH PARAMETERS
![Page 13: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/13.jpg)
EXAMPLE: STORED PROCEDURE PROCESSING A CURSOR
![Page 14: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/14.jpg)
SQLCODE AND SQLSTATE
![Page 15: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/15.jpg)
USER-DEFINED FUNCTIONS
• A user-defined function (UDF) is a database application object that maps a set of input data values into a set of output values. • For example, a function may take a measurement
in inches as input, and return the result in centimeters.• Functions always return a value• Some built-in functions already exist out-of-the-box• Eg: SUM(), AVG(), DIGITS(), etc.
• Can create UDFs in:• SQL PL, C/C++, Java, CLR, OLE, etc.
![Page 16: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/16.jpg)
USER DEFINED FUNCTIONS• Scalar functions• Return a single value• Cannot change database state (i.e. no INSERT, UPDATE, DELETEstatements allowed)• Example: COALESCE( ), SUBSTR( )
• Table functions• Return values in a table format• Used in the FROM clause of a query• Can change database state (i.e. allow INSERT, UPDATE, DELETEstatements)• Example: SNAPSHOT_DYN_SQL( ), MQREADALL( )
• Others type of functions (not covered in this course):• Row functions• Column functions
![Page 17: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/17.jpg)
SCALAR FUNCTIONS
• Scalar functions take input values and return a single value• They cannot be used to modify table data
![Page 18: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/18.jpg)
SCALAR FUNCTIONS
![Page 19: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/19.jpg)
INVOKING A SCALAR FUNCTION
• Scalar UDFs can be invoked in SQL statements wherever a scalar value is expected, or in a VALUES clause
![Page 20: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/20.jpg)
TABLE UDFS
• Returns a table• Used in the FROM clause of a query• Typically used to return a table and keep an audit
record• Example: A function that enumerates a set of
employees of a department
![Page 21: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/21.jpg)
TABLE UDFS
![Page 22: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/22.jpg)
CALLING A TABLE UDFS
• Used in the FROM clause of an SQL statement• The TABLE() function must be applied and must
be aliased.
![Page 23: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/23.jpg)
TRIGGERS
• A trigger is a database object defined on a table and fired when an INSERT, UPDATE, or DELETE operation is performed.• Activate (“fire”) automatically• Operations that cause triggers to fire are called
triggering SQL statements
![Page 24: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/24.jpg)
TYPES OF TRIGGERS
• BEFORE• Activation before row is inserted, updated or deleted
• AFTER• Activated after the triggering SQL statement has
executed to successful completion
• INSTEAD OF• Defined on views• Logic defined in the trigger is executed instead of the
triggering SQL statement
![Page 25: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/25.jpg)
CLIENT-SIDE DEVELOPMENT (AT THE CLIENT):
![Page 26: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/26.jpg)
APPLICATIONS
![Page 27: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/27.jpg)
EMBEDDED SQL
![Page 28: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/28.jpg)
STATIC SQL
![Page 29: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/29.jpg)
DYNAMIC SQL
![Page 30: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/30.jpg)
DYNAMIC SQL
![Page 31: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/31.jpg)
STATIC VS. DYNAMIC SQL
![Page 32: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/32.jpg)
CLI / ODBC
![Page 33: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/33.jpg)
CLI/ODBC
![Page 34: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/34.jpg)
CLI/ODBC
CLI/ODBC characteristics:• The code is easily portable between several
RDBMS vendors• Unlike embedded SQL, there is no need for a
precompiler or host variables• It runs dynamic SQL• It is very popular
![Page 35: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/35.jpg)
JDBC / SQL / PUREQUERYJDBC characteristics:• Like in ODBC, the code is easily portable between several RDBMS
vendors• Dynamic SQL• It is very popular
SQLJ• Embedded SQL in Java• Static SQL• Not that popular
pureQuery• Eclipse-based plug-in to manage relational data as objects• IBM’s paradigm to develop Java database applications• New since mid-2007, available with Optim Development Studio
![Page 36: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/36.jpg)
JDBC / SQLJ – SUPPORTED DRIVERS
![Page 37: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/37.jpg)
JDBC / SQLJ – SUPPORTED DRIVERS
• db2java.zip, db2jcc.jar, sqlj.zip, db2jcc4.jar and sqlj4.zip are included with:• IBM DB2 for Linux, UNIX and Windows servers• IBM Data Server Client• IBM Data Server Runtime Client• IBM Data Server Driver for JDBC and SQLJ
![Page 38: DB2 Application Development overview](https://reader035.fdocuments.net/reader035/viewer/2022070420/56815f40550346895dce18be/html5/thumbnails/38.jpg)
END