MySQL To DB2 Conversion Guide
MySQL To DB2 Conversion Guide
MySQL To DB2 Conversion Guide
MySQL to DB2
Conversion Guide
Guides you through a MySQL database
and application conversion to DB2
Enriches applications through
advanced DB2 features
Converts an application
with detailed examples
Whei-Jen Chen
Angela Carlson
ibm.com/redbooks
SG24-7093-01
Note: Before using this information and the product it supports, read the information in
Notices on page ix.
Contents
Notices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
The team who wrote this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Become a published author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Comments welcome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Summary of changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
December 2009, Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Executive summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Chapter 1. DB2 for Linux, UNIX, and Windows. . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Product overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 DB2 Data Server Editions for the production environment . . . . . . . . . 3
1.2.2 Products for accessing System z and System i host data . . . . . . . . . 7
1.2.3 DB2 for pervasive platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.4 Additional DB2 data server features . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 DB2 for Linux, UNIX, and Windows architecture. . . . . . . . . . . . . . . . . . . . . 9
1.3.1 DB2 9.7 threaded architecture and process model. . . . . . . . . . . . . . . 9
1.3.2 DB2 database objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.3 DB2 catalog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4 DB2 utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5 DB2 database access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.5.1 DB2 clients and drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.5.2 Application access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.5.3 DB2 application programming interfaces . . . . . . . . . . . . . . . . . . . . . 26
Chapter 2. MySQL database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.1 MySQL licensing overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.2 MySQL architecture overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.2.1 Database client and non-client utilities . . . . . . . . . . . . . . . . . . . . . . . 37
2.2.2 Database server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.3 MySQL design and SQL compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.3.1 MySQL directory structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.3.2 MySQL storage engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.3.3 MySQL standard SQL compliance . . . . . . . . . . . . . . . . . . . . . . . . . . 48
iii
iv
Contents
vi
Contents
vii
viii
Notices
This information was developed for products and services offered in the U.S.A.
IBM may not offer the products, services, or features discussed in this document in other countries. Consult
your local IBM representative for information on the products and services currently available in your area.
Any reference to an IBM product, program, or service is not intended to state or imply that only that IBM
product, program, or service may be used. Any functionally equivalent product, program, or service that
does not infringe any IBM intellectual property right may be used instead. However, it is the user's
responsibility to evaluate and verify the operation of any non-IBM product, program, or service.
IBM may have patents or pending patent applications covering subject matter described in this document.
The furnishing of this document does not give you any license to these patents. You can send license
inquiries, in writing, to:
IBM Director of Licensing, IBM Corporation, North Castle Drive, Armonk, NY 10504-1785 U.S.A.
The following paragraph does not apply to the United Kingdom or any other country where such
provisions are inconsistent with local law: INTERNATIONAL BUSINESS MACHINES CORPORATION
PROVIDES THIS PUBLICATION "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR
IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer
of express or implied warranties in certain transactions, therefore, this statement may not apply to you.
This information could include technical inaccuracies or typographical errors. Changes are periodically made
to the information herein; these changes will be incorporated in new editions of the publication. IBM may
make improvements and/or changes in the product(s) and/or the program(s) described in this publication at
any time without notice.
Any references in this information to non-IBM Web sites are provided for convenience only and do not in any
manner serve as an endorsement of those Web sites. The materials at those Web sites are not part of the
materials for this IBM product and use of those Web sites is at your own risk.
IBM may use or distribute any of the information you supply in any way it believes appropriate without
incurring any obligation to you.
Information concerning non-IBM products was obtained from the suppliers of those products, their published
announcements or other publicly available sources. IBM has not tested those products and cannot confirm
the accuracy of performance, compatibility or any other claims related to non-IBM products. Questions on
the capabilities of non-IBM products should be addressed to the suppliers of those products.
This information contains examples of data and reports used in daily business operations. To illustrate them
as completely as possible, the examples include the names of individuals, companies, brands, and products.
All of these names are fictitious and any similarity to the names and addresses used by an actual business
enterprise is entirely coincidental.
COPYRIGHT LICENSE:
This information contains sample application programs in source language, which illustrate programming
techniques on various operating platforms. You may copy, modify, and distribute these sample programs in
any form without payment to IBM, for the purposes of developing, using, marketing or distributing application
programs conforming to the application programming interface for the operating platform for which the
sample programs are written. These examples have not been thoroughly tested under all conditions. IBM,
therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.
ix
Trademarks
IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business
Machines Corporation in the United States, other countries, or both. These and other IBM trademarked
terms are marked on their first occurrence in this information with the appropriate symbol ( or ),
indicating US registered or common law trademarks owned by IBM at the time this information was
published. Such trademarks may also be registered or common law trademarks in other countries. A current
list of IBM trademarks is available on the Web at http://www.ibm.com/legal/copytrade.shtml
The following terms are trademarks of the International Business Machines Corporation in the United States,
other countries, or both:
1-2-3
AIX
AS/400
ClearCase
DB2 Connect
DB2 Universal Database
DB2
developerWorks
Distributed Relational Database
Architecture
DRDA
eServer
Everyplace
HACMP
i5/OS
IBM
IMS
Informix
InfoSphere
iSeries
OpenPower
Optim
OS/390
PartnerWorld
POWER
pSeries
pureXML
Rational Rose
Rational
Redbooks
Redbooks (logo)
System i
System p
System z9
System z
Tivoli
UniData
WebSphere
z/OS
z9
zSeries
Intel Pentium, Intel Xeon, Intel, Itanium-based, Itanium, Pentium, Intel logo, Intel Inside logo, and Intel
Centrino logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United
States, other countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other countries.
Linux is a trademark of Linus Torvalds in the United States, other countries, or both.
Other company, product, or service names may be trademarks or service marks of others.
Notices
xi
xii
Preface
Switching database vendors is often considered an exhausting challenge for
database administrators and developers. Complexity, total cost, and the risk of
downtime are often the reasons that restrain IT decision makers from starting the
conversion project. The primary goal of this book is to show that, with the proper
planning and guidance, converting from MySQL to IBM DB2 for Linux, UNIX,
and Windows is not only feasible but straightforward.
If you picked up this book, you are most likely considering converting to DB2 and
are probably aware of several of the advantages of converting to DB2 data
server. In this IBM Redbooks publication, we discuss in detail how you can
take advantage of this industry leading database server.
This book is an informative guide that describes how to convert the database
system from MySQL 5.1 to DB2 9.7 on Linux, and the steps involved in
enabling the applications to use DB2 instead of MySQL.
This MySQL to DB2 migration guide also presents the best practices in
conversion strategy and planning, conversion tools, porting steps, and practical
conversion examples. It is intended for technical staff involved in a MySQL to
DB2 conversion project.
xiii
xiv
leading-edge technologies. You will have the opportunity to team with IBM
technical professionals, IBM Business Partners, and Clients.
Your efforts will help increase product acceptance and customer satisfaction. As
a bonus, you will develop a network of contacts in IBM development labs, and
increase your productivity and marketability.
Find out more about the residency program, browse the residency index, and
apply online at:
ibm.com/redbooks/residencies.html
Comments welcome
Your comments are important to us.
We want our books to be as helpful as possible. Send us your comments about
this book or other IBM Redbooks publications in one of the following ways:
Use the online Contact us review IBM Redbooks publication form found at:
ibm.com/redbooks
Send your comments in an e-mail to:
[email protected]
Mail your comments to:
IBM Corporation, International Technical Support Organization
Dept. HYTD Mail Station P099
2455 South Road
Poughkeepsie, NY 12601-5400
Preface
xv
xvi
Summary of changes
This section describes the technical changes made in this edition of the book and
in previous editions. This edition might also include minor corrections and
editorial changes that are not identified.
Summary of Changes
for SG24-7093-01
for MySQL to DB2 Conversion Guide
as created or updated on December 1, 2009.
New information
DB2 features and functions of DB2 for Linux, UNIX, and Windows Version
9, 9.5, and 9.7
IBM Data Movement Tool
MySQL 5.1 features
Changed information
DB2 and MySQL features and functions
Conversion scenarios and examples
xvii
xviii
Executive summary
This book describes how to migrate MySQL 5.1 to DB2 Version 9.7 on Linux and
enable your applications on DB2. To further ease your migration, this informative
guide will cover best practices in migration strategy and planning, as well as the
step-by-step directions, tools, and practical conversion. After completing this book, it
will be clear to the technical reader that a MySQL to DB2 migration is easy and
straightforward.
Potential IBM clients seek migration information because DB2 offers performance
and functional capabilities that the competition can't compare with. DB2 Express C,
our lightweight community edition, is free to develop, deploy and redistribute, and is
designed to give the IT community a powerful alternative to the open source or free
databases currently available.
DB2 Express C offers the same high quality, reliable, scalable features that you would
expect from an IBM enterprise database at no charge. Fixed Term License support is
available as well, at a lower price than the competition. The decision to migrate
becomes simple when you consider that DB2 can be easily deployed in the
development stack, while offering many additional features and ease of use.
Enterprise class features aimed to lower the total cost of ownership can be found in
every edition of DB2. DB2 has powerful autonomics which make installation,
configuration, maintenance and administration virtually hands free. DB2 9.7's
compression features help companies manage rising energy costs and reduce
datacenter sprawl by reducing storage requirements and improving I/O efficiency.
IBM is committed to providing products to our clients that are powerful and
affordable. DB2 provides industry leading features, such as pureXML, Workload
Management, and Granular Security. Using DB2 pureXML makes XML data
processing even faster, more flexible, and more reliable. Manage workloads with new
threshold, priority and OS integration features in DB2 9.7. Keep data secure from
internal and external threats using the unparalleled security control in DB2 9.7.
Start taking advantage of these exciting new features and help your business
manage costs and simplify application development. Migrate your database systems
and applications today and discover why DB2 9.7 is a smarter product for a smarter
planet.
Arvind Krishna
General Manager
IBM Information Management
Executive summary
xix
xx
Chapter 1.
1.1 Introduction
IBM has an extremely strong history of database innovation and has developed a
number of highly advanced data servers. It started in the 60s when IBM
developed the Information Management System (IMS), which is a hierarchical
database management system. IMS was used to maintained inventory for the
Saturn V moon rocket and the Apollo space vehicle. In the 70s, IBM invented the
Relational Model and the Structured Query Language (SQL). In the 80s, IBM
introduced DB2 for the mainframe (DB2 for z/OS), which was the first database
that used relational modeling and SQL. DB2 for distributed platforms (DB2 for
Linux, UNIX, and Windows) was introduced in the 90s. Since then, IBM
continues to develop on DB2 for both mainframe and distributed platforms.
Although the relational data model has become more prevalent in the industry;
IBM still realizes that the hierarchical data model is important. Therefore in July
2006, IBM launched the first hybrid (also known as multi-structured) data server.
The release of DB2 for Linux, UNIX, and Windows Version 9 (DB2 9) data server
brought the most exciting and innovative database features to the market; these
features were further enhanced with the release of DB2 9.5 and 9.7. DB2 9
introduced many important features for both database administrators and
application developers. These features included pureXML, autonomics, table
partitioning, data compression, and label-based access control. DB2 9.5
enhanced the manageability of the DB2 data server by introducing the threaded
engine, easier integration with high availability disaster recovery (HADR),
workload management, enhancements to autonomics, and more. The focus of
the DB2 9.7 release is to provide unparalleled reliability and scalability for the
changing needs of your business. Therefore, DB2 9.7 introduces enhancements
to Version 9 and Version 9.5 features, such as enhancements to data
compression, performance, workload management, security, and application
development.
When this book was written, DB2 9.7 had just been released on June 2009. DB2
9.7 is the database version that we use throughout the book. DB2 9.7 is a highly
scalable and easy to install and manage hybrid data server. DB2 was developed
to meet the demands of even the most critical database applications. This is
managed through various autonomics capabilities, such as self-tuning memory
management and automatic storage. DB2 provides a highly adaptable database
environment while optimizing data storage through backups and deep data row
compression. DB2 deep embedded capabilities allow for ubiquitous deployment
in user directories and administrative installations for any size server. In a single
database, DB2 provides native storage and processing of both transactional
XML data in a pre-parsed tree format and relational data using pureXML
technology.
DB2 Personal Edition (PE) provides a single user database engine that is
ideal for deployment to PC-based users. The PE includes the ability for
remote management, the pureXML feature, and the SQL replication feature,
making it the perfect choice for deployment in occasionally connected or
remote office implementations that do not require multi-user capability, that is,
point-of-sale systems.
PE does not accept remote database requests; however, it contains DB2
client components and serves as a remote client to a DB2 Server. The DB2
Personal Edition can also be used for connecting and managing other DB2
data servers in the network.
The Personal Edition includes most of the features included in DB2 Express
Edition and runs in either 32-bit or 64-bit Intel or AMD workstations for
either Windows or Linux operating systems.
DB2 Express-C
DB2 Express-C is the no-charge community version of the DB2 data server. It
is targeted towards developers and Independent Software Vendors (ISVs) to
allow the development and deployment of applications, including the no
charge distribution of DB2 Express-C itself. All applications developed with
this version of DB2 can be moved to a higher edition of DB2 for Linux, UNIX
and Windows and even DB2 for z/OS without any application changes if using
the common SQL API set of the DB2 family.
This version of DB2 is at no charge for download and is therefore perfectly
suited for DB2 educators and students. DB2 Express-C does not restrict the
database size and can be used in a 64-bit memory model. The code is
optimized to use up to a maximum of 2 CPU cores and 2 GB of memory. No
fix pack updates are available for this edition; however, new versions of
Express-C are updated and freely available for download at any time.
While this version does not include all the features of higher editions of DB2,
such as storage optimization, replication services, or high availability, it
comes with the award-winning pureXML technology to leverage both
relational and XML data by being able to natively store XML data in a single
database. Several of these features can be activated by purchasing the DB2
Express Fixed Term License (FTL). Obtaining the FTL provides 1 year of
24x7 support, plus the ability to use high availability and disaster recovery.
At any point, users of DB2 Express-C can receive advice on the IBM DB2
Express Forum, which is monitored by IBM DB2 developers, by accessing the
following link:
http://www.ibm.com/developerworks/forums/forum.jspa?forumID=805
DB2 Express-C runs on Windows or Linux for both Intel and AMD on 32-bit or
64-bit architecture, as well as on Linux on Power (IBM System p and
System i).
You can download DB2 Express-C from this link:
hhttp://www-01.ibm.com/software/data/db2/express/
DB2 Express
DB2 Express edition is specifically tailored for small and medium businesses
(SMBs). It is designed for independent software vendors who need an
easy-to-install database integrated into their application software solution. It is
a multi-user version that supports local and remote applications in
stand-alone and local area network (LAN) environments.
DB2 Express utilizes up to 4 GB of memory and can be installed on a server
with up to 200 processor value units. For more information about processor
value units, visit this Web site:
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0611zikopoulos
2/
Several key features and strengths of the Express Edition are simplified
deployment, SQL replication, backup compression, autonomic management
DB2 Enterprise Server Edition meets the database server needs for any size
business. This product is the ideal foundation for building data warehouses,
transaction processing, or Web-based solutions, as well as for a back-end for
packaged solutions, such as enterprise resource planning (ERP), customer
relationship management (CRM), and supply chain management (SCM). In
addition, the DB2 Enterprise Server Edition offers connectivity and integration
for other enterprise DB2 and Informix data sources.
The Enterprise Server Edition does not pose limits to the maximum memory
or number of CPU cores. It can be licensed with either authorized user
licenses or processor value unit licenses.
In addition to the features offered in the Workgroup Server Edition, the
following features are also available: Query Parallelism, Multidimensional
Clustering, Materialized Query Tables, Table Partitioning, and Connection
Concentration. With the DB2 feature pack, you can add the following
features: Performance Optimization Feature, Advanced Access Control
Feature, Storage Optimization Feature, and Geodetic Data Management
Feature.
DB2 Enterprise Server Edition runs on Windows (32-bit and 64-bit), Linux
(Intel/ AMD 64-bit, System i, System p, System z), AIX, Solaris (Sparc and
x64) and HP-UX (ia64).
InfoSphere Warehouse
From a client/server perspective, the client code and the server code are
separated into separate address spaces. The application code runs in the client
process, while the server code runs in a separate process. The client process
can run on either the same machine as the data server or another machine,
accessing the data server through a programming interface. The memory units
are allocated for database managers, databases, and applications.
Because DB2 is running with a threaded architecture, all threads within the
engine process share the same address space, meaning all threads can
immediately see new memory allocations. This design creates a simplified
memory model by allowing memory growth and shrinkage through control of a
single memory parameter for an entire instance. This control is automatically
performed by the Self-Tuning Memory Manager, which can also tune other
memory parameters for best performance without DBA intervention. Its adaptive
algorithm is able to react to unforeseen memory requirements in DB2 caused by
workloads running against it.
To enable access to a specific database, the DB2 instance process responsible
for the database must be running on the DB2 server. When an instance process
is started, several processes are created, which interact with one another to
maintain connected applications and the database. There are several
background processes in DB2 that are pre-started; other processes start on a
need-only basis. There are several important background processes:
The main process is the DB2 System Controller (db2sysc), which runs the
entire DB2 engine infrastructure. The DB2 data server activities are
performed by Engine Dispatchable Units (EDU), which are defined as
threads running within a single operating system process.
A second DB2 background process is started together with the system
controller and is called the DB2 Watch Dog (db2wdog). Its responsibility is to
watch and monitor the system controller and to react to error conditions.
For autonomic tasks, another process is initialized when the database is
activated, which can happen either manually or by being triggered by a client
connection. This process is the Autonomic Computing Daemon (db2acd) and
runs autonomic tasks, such as health-monitoring, auto-runstats, and the
administration scheduler on the client side.
There is also the db2fmp process, which, decoupled from the DB2 system
controller process, serves thread-safe stored procedures and user-defined
functions (UDFs).
10
11
logical and physical structure of the object in the database, a configuration file
containing the parameter values configured for the database, and a recovery
log. Figure 1-4 shows the relationship between instances, databases, and
tables.
12
13
space, each container is a directory in the file system of the operating system.
This type of table space allows the operating systems file manager to control
the storage space. In a DMS table space, each container is either a re-sizable
file or a pre-allocated physical device, such as a disk, which the database
manager must control.
When using SMS or DMS in combination with container files, you can choose
how DB2 handles these files. For example, you can choose to enable various
optimization features if supported by the operating systems, that is, Direct I/O
(to bypass file system caching; always enabled with raw and block devices),
Vector I/O (reading contiguous data pages from disk into contiguous portions
of memory), and Async I/O (non-sequential processing of read and write
requests across multiple disks to avoid delays from synchronous events).
When using the Automatic Storage feature in DB2, you can simply specify
folders where the database can automatically create and manage DMS table
spaces. When more space is required, the database manager automatically
allocates more space. Table spaces can be automatically resized using this
feature. This feature provides a convenient and worry-free operation
scenario. You can perform manual operations without having to specify
container files.
Containers
A container is a physical storage device. It can be identified by a directory
name, a device name, or a file name. A container is assigned to a table
space. A single table space can span many containers, but each container
can belong to only one table space.
14
Buffer pools
A buffer pool is the amount of memory allocated to cache table and index
data pages. The purpose of the buffer pool is to improve system performance.
Think of it as a database-controlled file system cache. Data can be accessed
much faster from memory than from disk. Therefore, the fewer times the
database manager needs to read from or write to a disk (I/O) synchronously,
the better the performance of the application. The size of the buffer pool is the
single most important performance tuning area to help reduce the delay
caused by synchronous I/O.
Buffer pool memory can be automatically tuned online in the same way as
most other memory-related parameters. The feature responsible for
automatic tuning is called the Self-Tuning Memory Manager, which allocates
and releases new memory from the OS by shifting unused memory within
DB2 to components.
Schemas
A schema is an identifier, by default, the user ID, which qualifies tables and
other database objects. A schema can be owned by an individual, and the
owner can control access to the data and the objects within it. A schema
name is used as the first part of a two-part object name. For example, a
schema named Smith might qualify a table named SMITH.PAYROLL.
Tables
A database presents data as a collection of tables. Data within a table is
arranged in columns and rows. A table can contain XML documents that are
natively stored as a parsed hierarchical format, as shown in Figure 1-7 on
page 16. The data in the table is logically related, and relationships can be
defined between tables. Table data is accessed by using Structured Query
Language (SQL) or XQuery with XPath expressions. Both products are
standardized query languages for defining and manipulating both relational
and XML data in a database. A query is used in applications or by users to
retrieve data from a database. A typical query for relational data uses SQL to
create a statement in the form of:
SELECT
<column_name>
FROM
<table_name>
A typical XQuery for the table that is shown in figure 1-7 looks like this query:
xquery
db2-fn:sqlquery("SELECT INFO FROM XMLEmployeeInfo
WHERE EmpID=1001")/customerinfo/name
This query allows us to iterate through all our table rows and return customer
information from each XML document stored in each row for the example
table that is outlined in Figure 1-7 on page 16. DB2 and the XQuery language
also allow us to modify and update a subtree of an XML document in place
without having to rewrite the whole document. This query is possible,
15
When using the Deep Data Row Compression feature, DB2 is able to
transparently compress and decompress table rows (for each table with
compression turned on). This feature can effectively save 45-80% of the
space on disk. Compressed rows in a table are compressed when
pre-fetched to buffer pool memory and left in a compressed state until they
are actually used. Although decompression of the data when it is fetched
adds a slight overhead, I/O bound workloads will have a performance gain
due to the reduced amount of data we actually need to read and write from or
to disk, as well as saved memory.
Views
A view provides another way of looking at data from one or more tables; it is a
named specification of a result table. The specification is a SELECT
statement that runs whenever the view is referenced in an SQL statement. A
view has columns and rows just like a base table. All views can be used just
like base tables for data retrieval. Figure 1-8 on page 17 shows the
relationship between tables and views.
16
Indexes
An index is a set of keys, each pointing to rows in a table. For example, table
A has an index based on the first column in the table (Figure 1-9 on page 18).
This key value provides a pointer to the rows in the table: value 19 points to
record KMP. If searching for this particular record, a full table scan can be
avoided, because we have an index defined. Except for changes in
performance, users of this table are unaware that an index is being used.
DB2 decides whether to use the index or not. DB2 also provides tools, such
as the Design Advisor, that can help decide what indexes will be beneficial.
An index allows efficient access when selecting a subset of rows in a table by
creating a direct path to the data through pointers. The DB2 SQL Optimizer
chooses the most efficient way to access data in tables. The optimizer takes
indexes into consideration when determining the fastest access path.
Indexes have both benefits and disadvantages. Be careful when defining
indexes and take into consideration costs associated with update, delete, and
insert operations and maintenance, such as reorganization and recovery.
17
18
Description
Example
db2start
db2start
db2stop
db2stop -f
db2icrt
Creates an instance
db2idrop
Drops an instance
db2idrop -f db2inst1
db2ilist
db2ilist
db2imigr
db2iupdt
Example
Deactivates a database
db2set
db2set DB2AUTOSTART=yes
Lists utilities
19
Description
Example
db2admin
db2admin start
dasauto
dasauto -on
dascrt
dascrt -u dasusr1
dasdrop
dasdrop
dasmigr
dasmigr
Description
Example
db2level
db2level
db2look
db2look -d dep -a -e -o
db2look.sql
db2dart
db2dart dbaddr
db2pd
Troubleshooting tool
20
Command
Purpose
Optim Development
Studio
Optim Database
Administrator
DB2 installer
db2setup
db2isetup
Creates instances
Control Center
db2cc
Replication Center
db2rc
db2cc
Command line
processor (CLP)
db2 or
db2cmd
Health Center
db2hc
Task Center
db2cc tc
Journal
db2cc -j
Configuration Assistant
db2ca
Note: The Control Center and its associated components have been
deprecated in Version 9.7 and might be removed in a future release. We
recommend that you use the new suite of GUI tools for managing DB2 data
and data-centric applications. These new tools include the IBM Data Studio,
the Optim Development Studio, and the Optim Database Administrator.
and
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0804zikopoulos
21
The IBM Data Server Runtime client offers the basic client functionality and
includes drivers for ODBC, CLI, ADO.NET, Object Linking and Embedding (OLE)
DB, PHP, Ruby, Perl-DB2, JDBC, and SQLJ. This client already includes the
drivers and the capabilities to define data sources. Furthermore, the Lightweight
Directory Access Protocol (LDAP) is available, as well.
Additionally, the IBM Data Server Client provides vast amounts of sample code
in various languages, header files for application development and graphical
administration and development tools, such as the DB2 Control Center, the IBM
Data Studio, the MS Visual Studio Tools, and more.
Figure 1-10 illustrates how to connect to a DB2 data server using the IBM data
server clients.
22
built on the extensible Eclipse framework, this IDE includes a number of plug-ins
to support programming languages, such as Java, C/C++, PHP, Ruby, Perl, and
so on. Other plug-ins are available to maintain the written application sources in
various source code repositories, for example, the Concurrent Versions System
(CVS) or IBM Rational ClearCase from within IBM Optim Data Studio.
If running mixed versions of DB2 servers and clients, it is good to know that DB2
Clients from DB2 UDB Version 8 and DB2 9.1 or 9.5 for Linux, UNIX, and
Windows are still supported and able to connect to a DB2 9.7 data server. In the
reverse direction, the newer IBM data server clients from Version 9.7 can also
connect to the earlier DB2 9.1 and DB2 UDB Version 8 servers using the IBM
Data Server Driver for ODBC, CLI, and .Net. In this case, however, new DB2
Version 9.7 functionality is not available.
Communication protocols
DB2 primarily uses these protocols to communicate:
TCP, IPv4, IPv6, and Named Pipes (Windows only) for remote connections
Interprocess Communication (IPC) for local connections within a DB2
instance
23
For client/server communication, DB2 supports TCP/IP and Named Pipes for
remote or local loopback connections and uses IPC for client connections, which
are local to the DB2 server instance. Local and remote DB2 connections are
illustrated in Figure 1-11.
50000/tcp
From the command line, this information can be then updated in the database
manager with the following DB2 command:
db2 UPDATE DBM CFG USING SVCENAME db2icdb2
These tasks can also be performed using the DB2 Configuration Assistant utility.
24
The service name registered in the server or the port number can be specified in
the SERVER option. To catalog a database under this node, the command used
is:
db2 CATALOG DATABASE database-name AS alias-name AT NODE node-name
When using the Configuration Assistant GUI tool to add a database connection,
a database discovery can be started to find the desired database.
For information about how to enable and configure DB2 in an LDAP
environment, have a look at the Lightweight Directory Access Protocol (LDAP)
section within the Database fundamentals chapter in the DB2 Information
Center. The DB2 Information Center contains searchable and structured Web
pages and can be installed locally. Additionally, the Information Center is also
publicly available online at this Web site:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
Note: DB2 Discovery method is enabled at the instance level using the
DISCOVER_INST parameter, and at database level using the
DISCOVER_DB parameter.
25
DB2 client takes over the task of accessing the data over the network. In
certain cases, such as browser-based access or Java-based access, it is not
necessary to have the DB2 client running on the same machine where the
application executes.
DB2 provides exceptional flexibility for mixing and matching client and server
platforms in a heterogeneous environment. DB2 client and server code is
available for a wide variety of platforms. For example, the application can
execute on a Windows-based machine with a DB2 client for Windows, which
can then access a DB2 database on a Linux server. Likewise, the Linux
machine can act as a client and access data from UNIX servers or
mainframes.
Multi-tier
In a multi-tier configuration, the application, DB2 client, and the data source
typically reside on separate systems. Table 1-6 provides examples of these
configuration scenarios.
Table 1-6 Multi-tier configuration examples
Client
Middle-tier
Server
Web-browser
Web server
DB2 database
Application client
Application server
DB2 client
Application
DB2 Client
System z, System i
Application
DB2 Client
DB2 server
IBM recognizes that in many cases there might be a need for accessing data
from a variety of distributed data sources rather than one centralized
database. The data sources can be from IBM, such as DB2 or Informix, from
non-IBM databases, such as Oracle, or even from non-relational data, such
as files or spreadsheets. As illustrated in the last scenario in Table 1-6, IBM
offers the most comprehensive business integration solution by allowing
federated access to a variety of distributed data sources.
26
27
embedded SQL statements into DB2 runtime API calls that a host compiler
can process to create a bind file. The bind command creates a package in the
database. This package then contains the SQL operation and the access plan
that DB2 will use to perform the operations.
Dynamic SQL
Dynamic SQL statements in an application are built and executed at run time.
For a dynamically prepared SQL statement, the syntax has to be checked and
an access plan has to be generated during the program execution.
Examples of embedded static and dynamic SQL can be found in the DB2
home directory: sqllib/samples/.
28
When using DB2 CLI, the application passes dynamic SQL statements as
function arguments to the database manager for processing. Because of this
design, applications use the common access packages that are provided with
DB2. DB2 CLI applications do not need to be pre-compiled or bound. Only
compiling and linking the application are needed. Before DB2 CLI or ODBC
applications can access DB2 databases, the DB2 CLI binds files, which come
with the IBM Data Server Client, to each DB2 database that will be accessed.
This binding occurs automatically with the execution of the first statement.
Typically, when building an ODBC application, an ODBC driver manager is
needed, which is provided by platform vendors, such as Microsoft and others. An
ODBC driver manager is available for Linux at this Web site:
http://www.unixodbc.org/
29
Tip: If prototyping CLI calls before placing them in a program, use the
db2cli.exe (Windows) or db2cli (Linux) file in the sqllib/samples/cli
directory.
Type 4 driver
The JDBC type 4 driver can be used to create both Java applications and
applets. To run an applet that is based on the type 4 driver, a Java-enabled
browser is required, which downloads the applet and the JDBC driver
(db2jcc4.jar). To run a DB2 application with a type 4 driver, an entry for the
JDBC driver in the class path is required and no DB2 client is required.
30
The JDBC driver is included in the IBM Data Server Client Driver for JDBC
and SQLJ and is architected as an abstract JDBC processor that is
independent of driver-type connectivity or target platform. Examples of JDBC
calls can be found in sqllib/samples/java/jdbc.
Figure 1-15 illustrates the java type 4 driver connectivity environment.
31
ADO.NET
DB2 supports the Microsoft ADO.NET programming interface through a native
managed provider. These applications can use the DB2 .Net, the OLE DB .Net,
or the ODBC .NET data provider. High performing Windows Forms, Web Forms,
and Web Services can be developed using the ADO.NET API. DB2 supports a
collection of features that integrate seamlessly into Visual Studio 2003, 2005,
and 2008 to make it easier to work with DB2 servers and to develop DB2
procedures, functions, and objects.
The IBM Data Server Provider for .Net extends data server support for the
ADO.NET interface and delivers high performing, secure access to IBM data
servers:
DB2 Version 9 (or later) for Linux, UNIX, and Windows
DB2 Universal Database Version 8 for Windows, UNIX, and Linux
DB2 for z/OS and OS/390 Version 6 (or later), through DB2 Connect
DB2 for i5/OS Version 5 (or later), through DB2 Connect
DB2 Universal Database Version 7.3 (or later) for VSE and VM, through DB2
Connect
IBM Informix Dynamic Server, Version 11.10 or later
IBM UniData, Version 7.1.11 or later
IBM UniVerse, Version 10.2 or later
When used in conjunction with stored procedures and the federated database
capabilities of DB2 data servers and DB2 Connect servers, this data access can
be extended to include a wide variety of other data sources, including non-DB2
mainframe data and Informix Dynamic Server (IDS), Microsoft SQL Server,
32
Sybase, and Oracle databases, as well as any data source that has an OLE DB
Provider available.
For more information about developing ADO.NET and OLE DB, refer to DB2 for
Linux, UNIX, and Windows Developing ADO.NET and OLE DB Applications,
SC23-5851-01, which is available at this Web site:
http://www.ibm.com/support/docview.wss?uid=pub1sc23585101
Perl DBI
DB2 supports the Perl Database Interface (DBI) specification for data access
through the DBD::DB2 driver. The Perl DBI module uses an interface that is
similar to the CLI and JDBC interfaces, which makes it easy to port Perl
prototypes to CLI and JDBC. As of DB2 9.5, Perl DBI comes with support for
DB2 pureXML technology, which allows you to insert XML documents without
the need to parse or validate XML. The Perl driver also supports multi-byte
character sets, which means your application does not have to deal with the
conversion itself when interacting with the database.
You can obtain more information about Perl DBI at this Web site:
http://www.ibm.com/software/data/db2/perl/
PHP
The PHP Hypertext Preprocessor is a modular and interpreted programming
language intended for the development of Web applications. Its functionality can
be customized through the use of extensions. DB2 supports PHP through an
extension called pdo_ibm, which allows DB2 access through the standard PHP
Data Objects (PDO) interface. In addition, the ibm_db2 extension offers a
procedural API that, in addition to the normal create, read, update, and write
database operations, also offers extensive access to the database metadata.
The most up-to-date versions of ibm_db2 and pdo_ibm are available from the PHP
Extension Community Library (PECL):
http://pecl.php.net/
For more information about the PHP application development support that DB2
data server for Linux, UNIX, and Windows offers, refer to this Web site:
http://www.ibm.com/software/data/db2/ad/php.html
Ruby on Rails
DB2 9.5 has drivers for Ruby, which is an object-oriented programming
language. Combined with the open source Ruby framework called Rails, the
development of Web-based and database-driven applications can be extremely
quick. Included in DB2 is the IBM_DB Ruby adapter, which allows any
database-backed Ruby application to interface with IBM data servers.
33
For more information about IBM Ruby projects and the RubyForge open source
community, refer to the following Web site:
http://rubyforge.org/projects/rubyibm/
34
Chapter 2.
MySQL database
When planning a conversion project, it is critical to understand how the source
data server operates to successfully convert all functionality to the destination
data server. The goal of this chapter is to discuss the MySQL database
architecture and design, while keeping in mind how the features will be converted
to DB2.
This chapter discusses the following MySQL details:
35
36
always connects using TCP/IP. If the client is running on the local server, any of
the supported connection protocols can be used.
Figure 2-1 illustrates the conceptual architecture of the MySQL database. The
next several sections cover the functionality of the integrated components in
more detail.
37
Query users
Query users can interact with the database server through a query interface
called mysql, which allows users to issue SQL statements and view the results
returned from the server using the command line. There is also a graphical tool
called MySQL Query Browser that provides a graphical interface to create and
execute database queries.
In DB2, you can use the command line process for the same functionality. Use
the db2 or db2cmd command to start the command line processor.
Administrators
Administrators use the administrative interface and utilities, such as mysqladmin
and MySQL Administrator. Theses tools can be used for creating or dropping
databases and users, as well as managing the MySQL server. These tools
connect to the database server using the native C client library. There are also
utilities that can be used for administrative purposes, but they do not connect to
the MySQL server. Instead, they work directly with the database files. These
tools are myisamchk for table analysis, optimization, and crash recovery and
myisampack for creating read-only compressed versions of MyISAM tables.
DB2 offers a rich set of database management GUI tools, such as the DB2
Control Center, the Optim Database Administrator, and the IBM Optim Data
studio. These tools simplify database administration by providing one single tool
to completely manage your entire database environment. Also, you can use
these tools to query the database. The GUI tools are discussed in detail in 9.8,
Database management tools on page 312.
Applications
Applications communicate with the database server through MySQL APIs that
are available for various programming languages, such as C++, PHP, Java, Perl,
.NET, and so on. We discuss these APIs in more detail later in the chapter.
38
Optimizer
Caches and buffers
Database management utilities
Storage engines
Physical resources
Connection pool
The connection pool assigns user connections to the database and
corresponding memory caches. The utilities and programs that are included with
MySQL connect using the Native C API. Other applications can connect using
the standard drivers that MySQL offers, such as C++, PHP, Java, Perl, .NET,
and so on. MySQL supports TCP/IP, UNIX socket file, named pipe, and shared
memory networking protocols, depending on the type of operating system that is
used. For more details about application programming interfaces, see 2.5,
MySQL application programming interfaces on page 51.
SQL interface
The SQL interface accepts and conveys the SQL statements from the
connecting user or MySQL application. This layer is independent of the storage
ending layer, and, therefore, SQL support statements are not dependent on the
type of storage engine being used. The SQL statement is then passed to the
SQL parser for further processing.
SQL parser
The parser analyzes the SQL statements and validates the SQL query syntax.
The parser breaks up the statement and creates a parse tree structure to
validate the SQL query syntax and prepare the statement for the optimizer.
SQL optimizer
The SQL optimizer verifies that the tables exist and that the records can be
accessed by the requesting user. After security checking, the query is analyzed
and optimized to improve the performance of the query process.
39
40
Physical resource
This is the bottom layer of the MySQL architecture and represents the secondary
storage or physical disk. This layer is accessed through the storage engines to
store or retrieve data. These types of data are stored in this layer:
In the next section, we describe how the database objects and data are
physically stored on the server.
41
Windows default
directory
Linux default
directory
UNIX default
directory
Default
installation
directory
C:\Program Files\
MySQL\MySQL Server
5.1
Databases
and log files
<Default Dir>\data
/var/lib/mysql
<Default Dir>/data
Client
programs
and scripts
<Default Dir>\bin
/usr/bin
<Default Dir>/bin
mysqld
server
<Default Dir>\bin
/usr/sbin
<Default Dir>/bin
Manual
<Default Dir>\Docs
/usr/share/info
<Default Dir>/docs
Example
scripts
<Default Dir>\examples
Include files
<Default Dir>\include
/usr/include/mysq
l
<Default Dir>/include
Libraries
<Default Dir>\lib
/usr/lib/mysql
<Default Dir>/lib
Utilities and
scripts
<Default Dir>\scripts
Error
message
files
<Default Dir>\share
/usr/local/mysql
<Default Dir>/scripts
/usr/share/mysql
<Default
Dir>/share/mysql
In the example in Figure 2-2 on page 43, there are two databases on this MySQL
server. The first database is the mysql database, which, by default, holds the
security information. The second database is the sample database inventory,
which is discussed in more detail in Chapter 4, Conversion scenario on
page 75.
42
The following files are created for each database directory in the MySQL home
directory:
Files with the frm extension contain the structural definition of the table and
the view, which is known as the schema.
Files with the MYD extension contain the table data.
Files with the MYI extension contain the table indexes.
If there are triggers, there also are files with the TRN and the TRG extensions.
Example 2-1 shows the files that are created for each table in our sample
database.
Example 2-1 MySQL example table files
mysqlServer:/var/lib/mysql/inventory # ls -lrt
total 428
-rw-rw---- 1 mysql users 8670 Jul 21 23:11 locations.frm
-rw-rw---- 1 mysql users
65 Jul 21 23:11 db.opt
-rw-rw---- 1 mysql users 4976 Jul 21 23:15 locations.MYD
-rw-rw---- 1 mysql users 4096 Jul 21 23:18 locations.MYI
-rw-rw---- 1 mysql users 8918 Aug 7 13:21 owners.frm
-rw-rw---- 1 mysql users 8804 Aug 7 13:29 inventory.frm
-rw-rw---- 1 mysql users 1584 Aug 7 14:21 empGroup.frm
-rw-rw---- 1 mysql users 1585 Aug 7 14:22 techGroup.frm
-rw-rw---- 1 mysql users 1587 Aug 7 14:23 bossGroup.frm
-rw-rw---- 1 mysql users 8746 Aug 7 17:23 groups.frm
-rw-rw---- 1 mysql users 1595 Aug 10 01:42 managerGroup.frm
-rw-rw---- 1 mysql users 1596 Aug 10 01:43 generalGroup.frm
-rw-rw---- 1 mysql users 1587 Aug 10 02:58 testGroup.frm
-rw-rw---- 1 mysql users 128 Aug 10 02:58 groups.MYD
-rw-rw---- 1 mysql users 8886 Aug 10 03:29 services.frm
43
-rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----rw-rw----
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
mysql
users
users
users
users
users
users
users
users
users
users
users
users
users
users
users
8620
2048
160
8692
2048
37876
27648
212
40
378
2048
45576
37888
30720
61144
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Aug
Sep
Sep
Sep
Sep
10
10
10
10
10
11
11
24
24
24
27
9
11
11
11
03:54
03:54
03:54
03:56
15:31
15:45
16:39
16:07
16:08
16:08
20:21
03:08
00:46
13:41
13:41
status.frm
status.MYI
status.MYD
severity.frm
groups.MYI
inventory.MYD
inventory.MYI
severity.MYD
updateDate.TRN
services.TRG
severity.MYI
services.MYD
services.MYI
owners.MYI
owners.MYD
Log files, by default, are created in the mysql home directory. The security data
tables in the mysql database are in the /< mysql home directory>/mysql
directory. Table 2-2 lists the files of the security data tables.
Table 2-2 Permission information stored in tables
44
File
Description
columns_priv.MYD,
columns_priv.MYI,
columns_priv.frm
db.MYD,
db.MY,
db.frm
func.MYD,
func.MYI,
func.frm
host.MYD,
host.MYI,
host.frm
procs_priv.MYD,
procs_priv.MYI,
procs_priv.frm
tables_priv.MYD,
tables_priv.MYI,
tables_priv.frm
user.MYD,
user.MYI,
user.frm
By default, DB2 uses a better approach for the logical and physical distribution of
database objects. DB2 differs from MySQL in that DB2 stores all database
objects in table spaces. The benefits of table spaces are increased performance
and simplified management. To take advantage of this advanced database
distribution, refer to 6.2.1, Database manipulation on page 123, where we
discuss in detail the conversion of a MySQL database structure to DB2.
MyISAM
Memory
Merge
Archive
Comma separated value (CSV)
Federated
Blackhole
45
MySQL supports transactions with the InnoDB and NDB transactional storage
engines. Although both storage engines provide full ACID compliance, the
performance and throughput are often enough of a concern to justify converting
to DB2.
In DB2, all tables support transactions. Therefore, tables that are managed by
MySQL InnoDB, MyISAM, ARCHIVE, and CSV storage engines can all be
converted to a DB2 regular table. We discuss the details of converting MySQL
tables to a DB2 table in 6.2.2, Table manipulation on page 128.
46
.MRG file contains the list of MyISAM tables that make up the new Merge table. If
a table created with this storage engine is dropped, the Merge .frm and .MRG files
are removed and the individual MyISAM tables still exist. Tables created with the
merge storage engine support SELECT, UPDATE, INSERT, and DELETE
operations if the user has the privileges on the underlying MyISAM tables.
47
...
...
MySQL supports transactions with the InnoDB and NDB transactional storage
engines. Both engines provide full ACID compliance. In contrast, all tables in
DB2 support transactions and provide full ACID compliance.
Referential integrity
Referential integrity is the state in which all values of all foreign keys are
valid. The relationship between certain rows of the DEPT and EMP tables, as
shown in Figure 2-3 on page 49, illustrates referential integrity concepts and
48
terminology. For example, referential integrity ensures that every foreign key
value in the DEPT column of the EMP table matches a primary key value in
the DEPTNO column of the DEPT table.
49
DB2 has a full set of utilities available for working with and managing the
database environment. We describe DB2 utilities in 1.4, DB2 utilities on
page 18.
mysqld
mysqld-nt
mysqld_safe
mysqld-debug
mysqld_multi
mysql.server
mysqld-max
mysqlmanager
Setup programs
The rest of the programs are used for setting up operations during the installation
or upgrade of the MySQL server:
mysql_install_db
mysql_fix_privilege_tables
make_binary_distribution
mysqlbug
comp_err
make_win_bin_dist
mysql_secure_installation
mysql_tzinfo_to_sql
mysql_upgrade
50
mysqldump
mysqlimport
mysqlshow
mysqlaccess
mysqlbinlog
mysqlhotcopy
myisamchk
myisampack
innochecksum
my_print_defaults
myisamlog
mysql_convert_table_format
mysql_fix_extensions
mysql_tableinfo
mysql_setpermissions
mysql_waitpid
mysql_zap
replace
perror
msql2mysql
mysql_config
MySQL Administrator
MySQL Query Browser
51
The first approach is to connect the Java application using Java Database
Connectivity (JDBC) and the Connector/J, which is officially supported by
MySQL. This connector is written in Java and does not use the C client library to
implement the client/server communication protocol.
The second approach is to connect the .NET application using Connector/NET,
which is written in C# and does not use the C client library to implement the
client/server communication protocol. This approach is officially supported by
MySQL AB.
The third approach is to connect using Connector/ODBC for applications that use
ODBC standards. This connector uses the embedded C client libraries to
implement the client/server communication protocol. This approach is officially
supported by MySQL.
The fourth approach is to use the third-party APIs that are provided by
programming languages, such as PHP, Perl, or Python. These APIs will use the
embedded C client libraries to implement the client/server communication
protocol. The third-party APIs are not officially supported by MySQL. The
following list shows several of the APIs that are available for MySQL:
C API
The API to connect from C programs to a MySQL database. You can obtain
more details at this Web site:
http://dev.mysql.com/doc/refman/5.1/en/c.html
52
C++ API
The API to connect to a MySQL database from C++. You can obtain
information at this Web site:
http://forge.mysql.com/wiki/Connector_C%2B%2B_Binary_Builds
PHP API
PHP contains support for accessing several databases, including MySQL.
You can obtain information about MySQL access in the PHP documentation,
which can be downloaded from this Web site:
http://www.php.net/download-docs.php
PERL API
The Perl API consists of a generic Perl interface and a special database
driver. The generic interface in Perl is called Database Interface (DBI). For
MySQL, the driver is called DBD::mysql. You can obtain information about the
DBI at this Web site:
http://dbi.perl.org/
Python API
The API to connect to MySQL for Python is called MySQLdb. You can obtain
the API at this Web site:
http://sourceforge.net/projects/mysql-python/
Ruby API
You can obtain the API to access MySQL servers from Ruby programs at this
Web site:
http://tmtm.org/en/mysql/ruby/
DB2 supports the most frequently used MySQL programming languages. These
languages include PHP, Java, Perl, Python, Ruby, C#, C/C++, and Visual
Basic. With proper planning and knowledge, you can convert these applications
to DB2 with minimal effort. In Chapter 8, Application conversion on page 205,
we discuss and provide examples for converting applications from MySQL to
DB2.
53
54
Chapter 3.
55
A conversion assessment provides you with the overall picture of the conversion
tasks and efforts needed. From a conversion assessment, a conversion project
plan can be created to manage each conversion step.
To execute the typical conversion steps, various tools are available to help you
save time in your conversion project. IBM offers the no charge conversion tool,
the IBM Data Movement Tool, for converting from various relational database
systems to DB2.
The process of database and application conversion consists of the following
major steps, which are discussed in detail later:
Porting preparation and installation database structure porting
Data porting
Application porting
56
Basic administration
Testing and tuning
User education
Experienced IBM specialists can support you during any phase of the conversion
project with special conversion offerings that are provided worldwide by IBM.
Performance optimization
DB2 has dominated key performance benchmarks many times over the years,
including both Online Transaction Processing (OLTP) and Online Analytical
Processing (OLAP) benchmarks. This world-class performance means that
for businesses running DB, they can do more work with their existing
hardware, thereby avoiding or delaying costly server upgrades:
http://www.ibm.com/software/data/db2/9/editions_features_perf_ent.html
Reliability
For many businesses, database downtime causes both money and
opportunity loss. DB2 can minimize the downtime that is associated with
many planned activities, such as altering a table, and many unplanned
events, such a power outage, by either eliminating the downtime completely
or reducing its duration to a few seconds. The DB2 High Availability Feature
provides 24x7 availability for your DB2 data server through replicated failover
support and data recovery modules. For more details, visit this Web site:
http://www.ibm.com/software/data/db2/9/editions_features_ha.html
57
Industrial scalability
DB2 has the best scalability by being the only DBMS to have transaction
processing and business intelligence scaling up to 1,000 nodes. For more
information about scalability, see this Web site:
http://www.ibm.com
Security
Unauthorized data access is an ever present threat that can cost businesses
considerable sums of money, their reputation, or both. DB2 offers a
comprehensive suite of security features that effectively and decisively
minimizes this threat. DB2 provides additional peace of mind with lightweight
security audit mechanisms that check the validity of any unauthorized data
access:
http://www.ibm.com/software/data/db2/9/editions_features_advaccess.html
pureXML
DB2 pureXML revolutionizes the management of XML data with breakthrough
native XML support provided only by DB2. It eliminates much of the work
typically involved in the management of XML data, and it serves data at
unmatched speeds. If you work with XML data, you need to know about DB2
pureXML. Now, DB2 9.7 for Linux, UNIX, and Windows opens new
opportunities to efficiently analyze XML data in data warehouses:
http://www.ibm.com/software/data/db2/xml/
58
Center help to easily capture and monitor the overall health of the database.
The Replication Center is a tool that is used to set up and administer a
replication environment. The Configuration Assistant and the Control Center
can be used to assist in configuring and maintaining database objects. These
tools are just a few of the management tools that are available for DB2. For a
list of all of the tools and for more information about the tools, see this Web
site:
http://www.ibm.com/software/data/db2imstools/products/db2-luw-tools.html
59
60
If you are a client and have a conversion project in mind, contact one of the
following contacts according to your geography:
In North America and Latin America, contact [email protected]
In the U.K., Europe, Middle East, and Africa, contact [email protected]
In Japan, India, and Asia Pacific, contact [email protected]
You can obtain more information about the DB2 conversion team at the Software
Migration Project Office (SMPO) Web site:
http://www.ibm.com/software/solutions/softwaremigration/dbmigteam.html
You can obtain the most up-to-date details about current offerings, success
stories, literature, and other information about DB2 Migrate Now! at this Web
site:
http://www.ibm.com/software/data/db2/migration/
3.1.3 Education
DB2 provides an easy-to-use, feature-rich environment. Therefore, it is important
that those individuals involved in the conversion process are appropriately
trained to take full advantage of its offerings.
There is extensive training material available, such as courses, self-study guides,
and IBM Redbooks publications. IBM also offers a variety of DB2 courses; one
extremely useful course is the DB2 9.7 Bootcamp. For course details and
scheduled courses near you, visit this Web site:
http://www.ibm.com/developerworks/wikis/display/im/DB2+9.7+Bootcamp
For further information regarding DB2 training, visit the DB2 Web site:
http://www.ibm.com/software/data/education/
This DB2 for Linux, UNIX, and Windows conversion Web site can help you find
the information that you need to port an application and its data from other
database management systems to DB2. The porting and conversion steps,
which are described in this chapter, appear in the order that they are commonly
performed. In addition to the technical information that is available at this site,
IBM clients and IBM Business Partners need to check out the Information for IBM
clients and Information for IBM partners links:
http://www.ibm.com/developerworks/db2/zones/porting/partners.html
http://www.ibm.com/developerworks/db2/zones/porting/customers.html
Here, you will find additional links and information regarding assistance or
available resources for your porting project:
http://www.ibm.com/developerworks/db2/zones/porting/index.html
61
Number of databases
Number of tables
Number of indexes
Users, access rights, and privileges
PHP
Perl
Java
C/C++
Any other programming language
Database interface:
Direct database access through an API
Database access layer, such as ODBC or JDBC
Operating system:
62
Linux
AIX or UNIX
Windows
Any other operating system
Hardware used:
CPU
Memory
Hard disk
In a client/server environment, be sure to describe the application in both the
client environment and the server environment.
63
If you decide to use a new machine for the conversion program, you need to plan
what kind of hardware you want to use and which operating system you want to
install on the new machine.
In either case, check if the hardware of your target system meets the minimum
requirements, paying particular attention to the following areas:
Operating system
DB2
Application
Data
Conversion tools (if used)
3.3.1 Software
You must determine which software must be installed on your target system,
including:
Operating system (Linux, AIX, UNIX, Windows, or others)
DB2 version
Application to be converted
Conversion tools (if used and installed on the target system)
Any software on your source system that is required by your application to run
properly, including but not limited to:
HTTP server
Web application server
Development environment
Additional software (such as Lightweight Directory Access Protocol
(LDAP) or others)
Be sure to have the latest versions and fix packs of the planned products. Ensure
that the chosen operating system supports the chosen software.
3.3.2 Hardware
When starting the conversion process, it is important to have a target platform
that meets the minimum requirements of all the software that will be installed on
it. Check the supported hardware platforms, depending on the chosen software.
64
Your application also requires hardware resources. Be sure to have enough disk
space for your application and the transformed data.
Virtual images are a great option for operating enablement environments. Virtual
images reduce the hardware impact and allow simple test environments for the
enablement effort without incurring additional costs. Keep in mind that the disk
space is still needed.
65
Command-line interface
The command-line interface offers a way to operate the IBM Data Movement
Tool from the command line. The command-line interface is intended for
experienced users, who want to run end-to-end conversions without user
interaction.
For our conversion scenario, we use the new IBM Data Movement Tool to
convert database objects and data from MySQL to DB2. If you want to download
the IBM Data Movement Tool or receive more information about it, refer to:
http://www.ibm.com/developerworks/data/library/techarticle/dm-0906datamovement/
index.html
66
67
68
69
The scripts with the DB2 commands for creating users and granting privileges
must be run.
70
access layer is used, the adoption is not that complicated; otherwise, the effort to
enable the application will likely be higher.
Database interface
Regardless of the interface used between an application and a database
application, the access to the database must be changed, because the database
server has been modified.
If standardized interfaces, such as ODBC or JDBC are used, the changes will be
less significant than if the application uses the native API of the database
product.
Handling conditions
Depending on the implementation of your application, there might be changes in
the condition handling part of the application.
Additional considerations
DB2 offers rich, robust functions, which you can take advantage of in your
applications. The following list shows several of these features that you might
want to consider using in your application and which differ in MySQL:
Concurrency
Locking
Isolation-level transactions
Logging
National language support
XML support
The steps listed in this section are performed with various sample application
code and explained in great detail in Chapter 8, Application conversion on
page 205.
71
Log files
DB2 logs differently than MySQL, so database administrators must be aware of
the logging level that can be set, where log information is stored, and how to read
these logs.
We describe DB2 database administration in detail in Chapter 9, Database
administration on page 279.
Checking data
Aside from the basic data checks that must be performed when exporting and
importing data, checking that your application handles your data correctly and
manipulates the expected fields on inserts, updates, or deletes is vital.
Performance checking can be done manually, or a script can be used to have the
data checked.
72
Troubleshooting
Whenever the conversion leads to a problem, such as incorrect data or
unexpected application behavior, you have to determine the problem in order to
fix it.
You must understand error messages from the application, as well as DB2 error
messages. The troubleshooting process includes studying the DB2 log files.
See the DB2 technical support Web site for help with specific problems:
http://www.ibm.com/software/data/support/
Basic tuning
When your new system is working perfectly, you might want to tune it for even
better performance. With the correct database configurations, and hints from
DB2 tuning tools, you can speed up your queries quite easily.
DB2 provides tools, such as Design Adviser, Performance Monitor, or Index
Advisor, to support you in speeding up your DB2 system. DB2 also provides the
Self-Tuning Memory Manager, which keeps your database at an optimal state at
all times without any DBA intervention.
We discuss testing and initial tuning of a DB2 database in detail in Chapter 10,
Testing and tuning on page 321.
73
74
Chapter 4.
Conversion scenario
To illustrate the process of converting a database from MySQL to DB2, we have
developed a small sample Web application written in PHP. We created the
back-end database using MySQL 5.1 MyISAM tables. This application scenario
is a hardware inventory and services tracking application. Although, the
application is written in PHP, it is possible to convert other popular programming
languages, such as Perl, Java, and C/C++. Chapter 8, Application conversion
on page 205 also provides samples of these popular programming languages.
We discuss the following topics in detail:
Application description
Database structure
System environment
75
76
When a user enters the Web site, the login page (Figure 4-2) provides three
functions:
User Login: To log in as an already registered user
New Users: To create a new user account
Reset Password: To request resetting the password to a default password
With User Login, a registered user uses their user ID and password to log in. The
application verifies the user name, password, and user permissions against the
registered users in the database. The lowest level of permissions allows the user
to view, edit, and create inventory and service requests. If the user has
permissions to view, create, and edit groups and users, the user is given extra
functionality to do so.
From New Users, a new user can create an account. Completing the registration
form (Figure 4-3 on page 78) creates a new user account in the application. The
application verifies that the user name provided by the user is unique. By default,
new users have the lowest level of permissions. A user who is allowed to edit
groups can add a user to a group with more than default-level permissions.
77
Using Forget Password, a user can reset the users account password by
entering the users first, last, and user name, as shown in Figure 4-4 on page 79.
The new password will be displayed to the user.
78
When successfully logged in, the management options are presented to the
user. A set of management options is displayed and can vary, depending on the
type of permissions held by the logged in user. Figure 4-5 shows the welcome
page options for a user with the highest level of permissions.
Figure 4-5 Welcome menu for the administration login of the sample application
Using the View/Edit Account Info option, users can view account details, as
shown in Figure 4-6 on page 80. Users can update their details by editing the
fields and submitting the form.
79
With Add Inventory, the user can associate new inventory with their user
account. Figure 4-7 shows a typical completed Add New Inventory form.
Using View/Edit Inventory List, users have the ability to view their assigned
inventory and other users inventory using owner, location, inventory type, or
service created against the inventory (Figure 4-8 on page 81). From this page,
80
users can select to update inventory records by selecting Edit. To see the Edit
button, the user must have permissions to update the inventory record.
Using Create Service Ticket (Figure 4-9), users can open service tickets against
their assigned inventory.
81
By clicking the View/Edit Service Tickets option, users have the ability to view
their created and assigned service tickets, as shown in Figure 4-10. A user can
also view all service tickets based on the user, inventory location, inventory type,
and service type. From this page, the user can select to update the service ticket
by selecting Edit. To see the Edit button, the user must have the permissions to
update the inventory record.
With the View Group Users option, users with administration permissions can
view all users within a specific group. From this page, the user can update any
given user account by selecting Edit. Figure 4-11 on page 83 shows a user
viewing the manager group user details.
82
Using the Create/Edit Group option (Figure 4-12), a user with administration
permissions can create and edit groups.
83
84
Figure 4-13 Inventory database structure diagram for the sample application
85
In our conversion scenario, we set up two servers. Our original server had the
following software installed on the VMware image:
The second VMware image, the destination server, has the following software
installed on the VMware image:
For more information about the VMware workstation and working with VMware
images, go to this Web site:
http://vmware.com/
DB2 9.7 Express-C for Linux, UNIX and Windows can be downloaded from this
Web site:
http://www.ibm.com/software/data/db2/express/
The IBM Data Movement Tool is used to simplify and greatly decrease the time
that it takes to convert from MySQL to DB2. This tool is available at no charge
from IBM at the following URL:
http://www.ibm.com/developerworks/data/library/techarticle/dm-0906datamovement/
index.html
With the IBM Data Movement Tool, the conversion of database objects, such as
tables and data types, and the conversion of data can be done automatically into
equivalent DB2 database objects.
We discuss the installation and configuration of DB2 and the IBM Data
Movement Tool in the next chapter.
86
Chapter 5.
Installation
In this chapter, we discuss the target system environment setup. For the
database server, we guide you through the installation process of DB2 9.7 for
Linux, including the hardware and software prerequisites. The application server
has to be examined to ensure that the existing software has the proper DB2
support. If this is a completely new system setup, make sure that all the required
software is included in the installation list. Furthermore, we describe the
download and steps required to set up the IBM Data Movement Tool.
87
88
Distribution
Kernel
Library
x86
Red Hat
Enterprise
Linux (RHEL) 5
2.6.1892
libstdc++.so.
5
SUSE Linux
Enterprise
Server (SLES)
11
2.6.27.
19-5
glibc-2.9-13.2
SUSE Linux
Enterprise
Server (SLES)
10 SP2
2.6.16
glibc-2.4-31
Ubuntu 8.0.4
2.6.2419
glibc-2.7.so
Red Hat
Enterprise
Linux (RHEL)
52.6.18
-92l
libstdc++.so
SUSE Linux
Enterprise
Server (SLES)
11
2.6.27.
19-5
glibc-2.9-13.2
SUSE Linux
Enterprise
Server (SLES)
10 SP2
2.6.16
glibc-2.4-31
Ubuntu 8.0.4
2.6.2419
glibc-2.7.so
x86_64
AMD64/
EM64T
Comment
Hardware requirements
DB2 products are supported on the following hardware:
HP-UX
Itanium-based HP Integrity Series Systems
Linux
x86 (Intel Pentium, Intel Xeon, and AMD) 32-bit Intel and AMD
processors
x64 (64-bit AMD64 and Intel EM64T processors)
Chapter 5. Installation
89
90
installed is in fact insufficient, installation will stop, and the setup program will
need to be aborted if additional space cannot be provided.
Remember to include disk space for required databases, software, and
communication products.
On the Linux and UNIX operating systems, 2 GB of free space in the /tmp
directory is recommended.
Chapter 5. Installation
91
Communication requirements
When using TCP/IP as the communication protocol, no additional software is
needed for connectivity. For more supported communication protocols, refer to
the DB2 manual Quick Beginnings for DB2 Servers 9.5, GC10-4246, at this Web
site:
http://publibfp.boulder.ibm.com/epubs/pdf/c2358642.pdf
Or, you can visit the IBM DB2 Database for Linux, UNIX, and Windows
Information Center at this Web site:
https://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
Linux
UNIX
Windows
Yes
Yes
Yes
db2_install
Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
No
For this particular project or any other conversion project in general, the DB2
Data Server Client is required. It provides libraries for application development. If
the application server and the database server are to be placed on the same
system, you can install both the DB2 server and Data Server Client in one step
by selecting the Custom installation type.
For this project, we perform the following steps to install DB2 9.7 on Linux:
1. Log on to Linux as a root user.
2. Download DB2 9.7 Express-C from this Web site:
http://www.ibm.com/software/data/db2/express/download.html
92
7. Launch the DB2 setup wizard, which opens the panel that is shown in
Figure 5-1:
./db2setup
Note: Starting in DB2 9.5, you can also perform a non-root installation of DB2.
8. When the DB2 Launchpad opens, choose Install a Product, as shown in
Figure 5-2 on page 94.
Chapter 5. Installation
93
9. Select Install New under the option to install the server to launch the DB2
setup wizard.
10.Go to the Software License Agreement panel, and read the Software License
Agreement, as shown in Figure 5-3 on page 95. If you agree with the
agreement, select Accept, and click Next.
94
11.In the Installation Type panel, click Custom, as shown in Figure 5-4 on
page 96, and click Next.
Chapter 5. Installation
95
12.Click Next again to get the Features panel. Select the Application
Development tools option, as shown in Figure 5-5 on page 97, and click
Next.
96
Figure 5-5 DB2 custom installation with application development tools selected
13.In the Languages panel, choose the type of languages to install, and click
Next.
14.In the Documentation panel, choose where to access the DB2 Information
Center. You can choose to install it as part of this process, or you can access
the online DB2 Information Center at any time. Click Next.
15.In the Database Administration Server (DAS) panel, enter the DAS user
information. Linux group and user accounts do not have to be created prior to
this step; DB2 will create the required Linux system group and user
automatically. For the example installation, we use the default name dasusr1
and choose a password for this user, as shown in Figure 5-6 on page 98, and
click Next.
Chapter 5. Installation
97
16.In the Instance setup panel, you can choose whether you want to set up an
instance during the DB2 installation. By selecting Create a DB2 instance and
clicking Next, we let DB2 create the instance for us.
17.Enter the instance owner information in the Instance Owner panel. Linux
group and user accounts do not have to be created prior to this step; DB2 will
create the Linux group and user. For the example installation, we use the
default db2inst1 settings and create a password for this user, as shown in
Figure 5-7 on page 99, and click Next.
98
18.In the Fenced user panel, we allow DB2 create the ID for us, as shown in
Figure 5-8 on page 100, and click Next.
Chapter 5. Installation
99
19.In the Instance Communication panel, configure the DB2 instance TCP/IP
communication. For the example installation, we use the default settings and
click Next.
20.In the DB2 Tools Catalog Configuration panel, we create a local tools catalog
by selecting the db2inst1 instance. We also use the default local database
TOOLSDB and default schema SYSTOOLS and then, click Next. Figure 5-9 on
page 101 shows the DB2 Tools Catalog Configuration panel.
100
21.At the end, the setup wizard provides a summary of the installation options
selected. Review it and click Finish to start the installation.
Chapter 5. Installation
101
problem if the installation failed. By default, db2setup starts the graphical user
interface (GUI) and picks up the language flag from the operating systems
settings.
If the log file option is not specified, the db2setup.log file and db2setup.err file
are stored in the /tmp directory on a Linux operating system. Example 5-1 shows
an example of the db2setup.log file.
Example 5-1 DB2setup log file
DB2 Setup log file started at: Fri Jul 24 15:20:15 2009 EDT
============================================================
Operating system information: Linux 2.6.16.60-0.21-smp.#1 SMP Tue May 6
12:41:02 UTC 2008 i686
Product to install:
Installation type:
...
DB2 Express-C
Custom
102
The command to create the DAS user is the dascrt command. Use it in the
following way:
dascrt -u dasadm1
As part of the GUI instance creation, the installer suggests three users identified
as db2inst1, db2fenc1, and dasadm1. These are default names for the instance
users. If you do not want to use the default names, you can choose your own
names by creating the system user IDs and groups ahead of time and inputting
these parameters in the wizard when prompted. The installer will also add the
following entry to the /etc/services file in order to allow communication from
DB2 clients:
db2c_db2inst1 50000
In this entry, db2c_db2inst1 indicates the service name, and 50000 indicates the
port number. DB2 allows for multiple instances on one server installation to allow
for various environments, that is, test, production, development, and so on.
Subsequent instances can be created on the same server simply by using one of
the methods introduced here.
Chapter 5. Installation
103
All clients are supported on Linux, AIX, HP-UX, Solaris, and Windows operating
systems.
Note: Typically, in a production environment, DB2 clients are installed on
separate physical machines from the DB2 server. However, for an application
development environment, it can be useful to have everything, such as the
DB2 database server plus the clients, on the same machine.
To access a remote DB2 database, you can either run the easy to use graphical
tool Configuration Assistant or use the catalog commands to provide entries for
the following three directories:
NODE directory: A list of remote DB2 instances
ADMIN NODE directory: A list of remote DB2 Administration servers
DATABASE directory: A list of databases
To use the command-line tools, first catalog the DB2 node. The DB2 node is the
server where the database resides. Then, catalog the database. See
Example 5-2.
Example 5-2 Cataloging the DB2 node and the database
--- catalog database node
-CATALOG TCPIP NODE db2node REMOTE server1 SERVER 50001
--- catalog the DAS on the remote node
-CATALOG ADMIN TCPIP NODE db2das remote SERVER1
-- catalog database
-CATALOG DATABASE invent AS inventdb AT NODE db2node
After installing your DB2 Client, configure it to access a remote DB2 server using
the Configuration Assistant. The graphical interface can be launched through the
DB2 Control Center or run on its own by using the command db2ca. For more
details, refer to the IBM DB2 manual Quick Beginnings for DB2 Clients,
GC10-4242.
104
Installation steps
The following steps explain how we install Apache2 on SUSE 10 SP2:
1. Download the Apache package.
The source code for Apache package is available at this Web site:
http://httpd.apache.org/download.cgi
Chapter 5. Installation
105
Where option:
--prefix
--enable-so
--enable-cgi
--enable-info
--enable-rewrite
--enable-speling
--enable-usertrack
--enable-deflate
--enable-ssl
--enable-mime-magic
Automatically determines Multipurpose Internet
Mail Extensions (MIME) type
You can run the configure command with the --help option to get a full list of
options for configuring Apache.
106
7. Compile Apache.
After configuring the source files, the compile process is started using the
make command. We pipe the output to a log file in order to check for failure
afterward:
db2server:/usr/local/src/httpd-2.2.11 # make > apacheMake.out
8. Install Apache.
After Apache compiles successfully, it can be installed as the root user:
db2server:/usr/local/src/httpd-2.2.11 # make install
Chapter 5. Installation
107
Note: All commands and procedure descriptions that are provided in this
section refer to SUSE Linux Enterprise Server 10 SP2. The commands and
procedures can vary for other versions or Linux distributions.
Installation steps
In order to use the IBM DB2 libraries, you must recompile PHP. These
installation steps explain how to update your PHP install and how to install PHP
from the beginning:
1. Back up the httpd.conf and php.ini files.
To ensure the configuration files for Apache and PHP are not lost when
installing the new PHP version, we recommend backing up the
/etc/httpd/httpd.conf and if you have a previous version of PHP installed,
back up the /etc/php.ini files.
2. Download the PHP package.
The source code for PHP is available at this Web site:
http://www.php.net/downloads.php
108
In our conversion scenario, we use Version 5.3.0 of PHP, and the package
that we downloaded was php-5.3.0.tar.gz.
Download the ibm_db2 PECL extension at this Web site:
http://pecl.php.net/package/ibm_db2
In our conversion scenario, we use Version 1.2.8, and the package that we
downloaded was ibm_db2-1.8.2.tgz.
Download the PDO_IBM PECL extension at this Web site:
http://www.pecl.php.net/package/PDO_IBM
In our conversion scenario, we use Version 1.3.0, and the package that we
downloaded was PDO_IBM-1.3.0.tgz.
3. Uncompress the source package.
The following command decompresses the contents of the source package to
a directory called php-5.3.0:
db2server:/usr/local/src # tar xzf php-5.3.0
Chapter 5. Installation
109
Where option:
110
--prefix
--with-IBM_DB2
--with-pdo-ibm
--with-pdo-odbc
--with-ibm-db2
--with-apxs2
--with-config-file-path
Allows you to specify the path to the php.ini file
If a particular extension is not required, it can be removed from the
configure command and the install.
14.Compile PHP.
After configuring the source files, start the compile process by using the make
command. We pipe the output to a log file in order to check for failures
afterward:
db2server:/usr/local/src/php-5.3.0 # make > phpMake.out
15.Install PHP.
After PHP has compiled successfully, install it as the root user:
db2server:/usr/local/src/php-5.3.0 # make install
16.Configure Apache.
If Apache was already configured for PHP on your server, you only have to
make certain changes. As part of the installation process, PHP automatically
modifies the Apache configuration file /usr/local/apache2/conf/httpd.conf.
Confirm that the httpd.conf has the following lines. If the lines are missing,
update the file:
Chapter 5. Installation
111
Software requirements
We describe the software that is required to use the IBM Data Movement Tool in
this topic.
General requirements
In general, you need the following software:
Latest version of the IBM Data Movement Tool.
MySQL: Ensuring that MySQL is running (usually the daemon can be started
with the command safe_mysqld & from an account with root permissions).
DB2 V9.7 needs to be installed on the target server. Use the command
db2start to ensure that the DB2 Server is up and running.
Java Version 1.5 or higher must be installed on your target server. To verify
your current Java version, run the java -version command. By default, Java
is installed as part of DB2 for Linux, UNIX, and Windows in
<install_dir>\SQLLIB\java\jdk (Windows) or /opt/ibm/db2/V9.7/java/jdk
(Linux).
You must have the Java Database Connectivity (JDBC) drivers for the
MySQL source database (mysql-connector-java-5.1.8-bin.jar or the latest
driver) and the DB2 target database (db2jcc.jar, db2jcc_license_cu.jar, or
db2jcc4.jar, db2jcc4_license_cu.jar) installed on the server with the IBM Data
Movement Tool.
112
Operating system
IBM Data Movement Tool supports the following operating systems:
Windows
z/OS
AIX
LINUX
Solaris
HP-UX
MacIntosh
For the purpose of this document, we have used the IBM Data Movement Tool
with DB2 Version 9.7 and MySQL Version 5.1.36. We recommend that you install
the IBM Data Movement Tool on the DB2 server side to achieve the best data
movement performance.
2. Create a directory for the IBM Data Movement Tool, and copy
IBMDataMovementTool.zip to this directory:
db2server:/opt/ibm # mkdir IBMDataMovementTool
db2server:/usr/local/src # cp IBMDataMovementTool.zip
/opt/ibm/IBMDataMovementTool/
Chapter 5. Installation
113
114
Chapter 6.
Database conversion
After you create the conversion plan and install and set up all of the required
software, it is time to translate the source MySQL database structure into DB2.
In this chapter, we discuss the process of converting the database structure from
the MySQL 5.1 server to the DB2 9.7 server. Before this discussion, we must
evaluate the differences between the MySQL database structure and the DB2
database structure.
In the first section, we discuss data type mapping, taking a closer look at MySQL
and DB2 data types and the differences between them. Following this section,
we provide Data Definition Language (DDL) differences, providing a syntax
comparison between MySQL and DB2.
In the succeeding section, we provide additional considerations for users while
porting the database schema from MySQL to DB2.
In the last section, we provide detailed information regarding the database
schema porting steps using the following approaches:
Porting using the IBM Data Movement Tool
Manual porting
Metadata transform
115
Figure 6-2 on page 117 shows the built-in data types that are supported by DB2.
116
MySQL data types are grouped into three categories and can be converted to
DB2 data types following these suggested rules:
Numeric type:
TINYINT
A tiny integer is a single-byte integer in MySQL that can be mapped to a
DB2 SMALLINT for similar functionality.
SMALLINT
A small integer is a two-byte integer with a precision of five digits. With
MySQL, the range of signed small integers is -32768 to 32767, making it
replaceable by DB2 SMALLINT. For unsigned MySQL small integers, the
range is 0 to 65535, making it replaceable by DB2 INTEGER.
BIT, BOOL, and BOOLEAN
These types are synonyms for TINYINT(1). Instead of BIT, BOOL, and
BOOLEAN, DB2 uses SMALLINT with check constraint.
117
MEDIUMINT
This type is a medium-sized integer with a signed range of -8388608 to
8388607 or 0 to 16777215 for the unsigned range. DB2 uses an INTEGER
for this type, which has a range of -2147483648 to 2147483647.
INTEGER and INT
An integer is 4-byte integer for both MySQL and DB2. The range of a
signed MySQL INTEGER and DB2 INTEGER is -2147483648 to
2147483647; therefore, the DB2 INTEGER can be used. For the unsigned
MySQL INTEGER, the range is 0 to 4294967295. This type can be
replaced by DB2 BIGINT.
BIGINT
A big integer is an 8-byte integer for both MySQL and DB2. A DB2 BIGINT
can be used for a signed MySQL BIGINT, because the range of a signed
MySQL BIGINT and a DB2 BIGINT is -9223372036854775808 to
9223372036854775807. For unsigned MySQL BIGINT, the range is 0 to
18446744073709551615; DB2 DECIMAL will cover the range.
FLOAT
A FLOAT in MySQL is a single precision floating-point number ranging
from -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38
to 3.402823466E+38. Whereas in DB2, it is a double precision
floating-point number ranging from -1.79769E+308 to -2.225E-307, or
from 2.225E-307 to 1.79769E+308. Hence, a FLOAT value in MySQL can
be directly mapped to a DOUBLE in DB2.
DOUBLE
A DOUBLE is a double precision floating-point number for both DB2 and
MySQL. A signed MySQL DOUBLE can be directly mapped to DOUBLE in
DB2. An unsigned MySQL DOUBLE can be mapped to DECIMAL in DB2.
REAL
This type is a synonym for DOUBLE in MySQL and, therefore, can be
mapped to the same DB2 data types as the MySQL DOUBLE data type.
DECIMAL, NUMERIC, and FIXED
A DECIMAL in MySQL is mapped to a DECIMAL in DB2. Although MySQL
and DB2 implement decimals differently, externally, they behave the same
way.
Date and time type:
DATE
A DATE in MySQL and DB2 DATE both use four bytes (first two bytes for
the year, the third byte for the month, and the last byte for the day). The
118
range of the MySQL date for the year is 1000-9999, whereas DB2
supports a date range from 0001-9999, allowing DB2 to map to this
MySQL data type.
DATETIME
A date and time combination in MySQL is displayed as YYYY-MM-DD
HH:MM:SS, ranging from year 1000 - 9999. In DB2, TIMESTAMP is used
for a similar purpose, which is a seven part value (year, month, day, hour,
minute, second, and microsecond).
TIMESTAMP
A time stamp in MySQL is a date/time combination with a range of
1970-01-01 00:00:00 to the year 2037. It is automatically set to the date
and time of the most recent operation if you do not give it a valid value.
This data type can be mapped to the DB2 TIMESTAMP, or an optional
choice is the DB2 TIME data type.
TIME
MySQL time represents a clock ranging from -838:59:59 to 838:59:59. It is
mapped to DB2 TIME, which is a 24-hour clock.
YEAR
The MySQL year can be in either a two-digit or four-digit format,
representing the year from 1901 - 2155 mapped to SMALLINT or
CHAR(4) in DB2.
String and character types:
CHAR
A fixed length string in MySQL is represented with the same name in DB2,
mapping to a CHAR in DB2.
VARCHAR
A variable-length string in MySQL has a maximum length of 65,535, and a
DB2 variable-length string has a maximum length of 32,672. A MySQL
VARCHAR can be mapped to a DB2 VARCHAR when the MySQL
VARCHAR variables are shorter than 32,672; otherwise, VARCHAR can
be mapped to a character large object (CLOB).
BINARY
MySQL BINARY stores binary byte strings, which can be mapped to DB2
CHAR(I).
VARBINARY
MySQL VARBINARY stores binary byte strings, which can be mapped to
DB2 VARCHAR(I).
119
TINYBLOB
MySQL TINYBLOB is a binary large object column with a maximum length
of 255, which can be mapped to DB2 BLOB(255).
TINYTEXT
MySQL TINYTEXT is a character stream of maximum length 255, which
can be mapped to DB2 CLOB(255).
BLOB
MySQL BLOB is a binary data column with a maximum length of 65,535,
which can be mapped to DB2 BLOB(65 KB).
TEXT
TEXT is a TEXT column with a maximum length of 65,535 and is mapped
to DB2 CLOB(65 KB).
MEDIUMBLOB
MySQL MEDIUMBLOB is a blob column with a maximum length of
16,777,215, which can be mapped to DB2 BLOB(16 MB).
MEDIUMTEXT
MySQL MEDIUMTEXT is a text column with a maximum length of
16,777,215, which can be mapped to DB2 CLOB(16 MB).
LONGBLOB
MySQL LONGBLOB is an extremely large BLOB column with a maximum
length of 4,294,967,295, which can be mapped to DB2 BLOB(2 GB).
LONGTEXT
MySQL LONGTEXT is a text column with a maximum length of
4,294,967,295, which can be mapped to DB2 CLOB(2 GB).
ENUM
MySQL has a special ENUM type, which is a string object that can have
only one value chosen from a list of values, 'value1', 'value2', ..., NULL,
which can be mapped to DB2 VARCHAR() with check constraints.
SET
MySQL has another special SET type, which is a string object that can
have zero or more values, which must be chosen from the list of values,
'value1', 'value2',..., which can be mapped to DB2 VARCHAR() with check
constraints.
Table 6-1 shows the default data type mappings between the two databases that
are used by the IBM Data Movement Tool. We use this mapping for our sample
conversion.
120
DB2 9.7
TINYINT
SMALLINT
TINYINT
UNSIGNED
SMALLINT
SMALLINT
SMALLINT
SMALLINT
UNSIGNED
INTEGER
Optional: SMALLINT
BIT
SMALLINT
BOOLEAN
SMALLINT
MEDIUMINT
INTEGER
MEDIUMINT
UNSIGNED
INTEGER
INTEGER/INT
INTEGER
INTEGER/INT
UNSIGNED
BIGINT
Optional: INTEGER
BIGINT
BIGINT
BIGINT
UNSIGNED
DECIMAL
Optional: BIGINT
FLOAT
DOUBLE
FLOAT
UNSIGNED
DOUBLE
DOUBLE
DOUBLE
DOUBLE
UNSIGNED
DECIMAL
Optional: DOUBLE
REAL
DOUBLE
REAL
UNSIGNED
DOUBLE
NUMBERIC
DECIMAL
DEC
DECIMAL(31,0)
NUMERIC(P)
NUMERIC(P,0)
DECIMAL(P)
DECIMAL(P,0)
DEC(P)
DEC(P,0)
DECIMAL(min(P,31),0)
DECIMAL
UNSIGNED
DECIMAL
121
NUMERIC
UNSIGNED
DECIMAL
NUMERIC(p,s)
DECIMAL(p,s)
DEC(p,s)
where:
s > 0 && p >= s
s > 0 && p < s
s<0
DECIMAL(min(p,32), min(s,32))
DECIMAL(min(s,32), min(s,32))
DECIMAL(min(p,32),0)
DATE
DATE
DATETIME
TIMESTAMP
Optional: TIME
TIMESTAMP
TIMESTAMP
TIME
TIME
YEAR
CHAR(4)
Optional: SMALLINT
CHAR
CHAR
VARCHAR
VARCHAR
BINARY
VARBINARY
TINYBLOB
BLOB(255)
Optional: VARCHAR(255)
TINYTEXT
CLOB(255)
BLOB
BLOB(65535)
TEXT
CLOB(65535)
MEDIUMBLOB
BLOB(16777215)
MEDIUMTEXT
CLOB(16777215)
LONGBLOB
BLOB(2GB)
LONGTEXT
CLOB(2GB)
ENUM
SET
122
Both MySQL and DB2 follow the structured query language (SQL), which is a
standardized language that is used to access databases and their objects, as
defined by the American National Standards Institute (ANSI)/International
Organization for Standardization (ISO).
The data definition language is a set of SQL statements. You can use these
statements for a variety of tasks, including the creation or deletion of databases
and database objects (tables, views, and indexes), definitions of column types,
and definitions of referential integrity rules.
Then, create a symbolic link to the newly created directory using these
statements:
bash> cd /var/lib/mysql
bash>ln -sf <file system with space>/mysqldata data
Then, create a database from the mysql prompt using this statement:
mysql>CREATE DATABASE inventory
MySQL users can distribute tables using symbolic linking or the data and index
directory options of the CREATE TABLE statement.
MySQL stores data in single files, multiple files, or table spaces, depending on
the table type being used. Figure 6-3 shows an example of storage engines that
fall under one of these three types.
123
The tables on the left side of the diagram are managed by the MyISAM storage
engine. For MyISAM tables, MySQL creates a .MYD file for data and a .MYI file to
store indexes, with only one file for all data and indexes. The tables in the middle
of the diagram are managed by the Merge storage engine. With Merge tables,
the .MRG file contains the names of the tables to be used and a .FRM file for table
definition. In a Merge table, various tables are used, each of these tables having
its own data file. However, as a whole, a Merge table uses multiple data files.
The tables on the right side of the diagram are managed by the InnoDB storage
engine. For InnoDB tables, MySQL stores data in a table space identified by the
path parameter, innodb_data_file_path. Multiple data files can be used for
InnoDB.
MySQL also has a feature called user-defined partitioning, which allows for table
data to be horizontally split across file systems depending on a specific set of
data defined by the user. For each partition that is created, there is a
corresponding .MYD file for data and .MYI file for the index.
In contrast to MySQL, DB2 stores everything in table spaces. Table spaces are
logical representations of physical containers on the file system. DB2 uses a
better approach for the logical and physical distribution of the database and the
database elements in different sectors, as shown in Figure 6-4 on page 125.
After completing the conversion of your database from MySQL to DB2, you can
use these features to enhance the performance of your application.
124
Instance
A DB2 server can have more than one instance. One instance can have multiple
databases. One instance per application database has the advantage that the
application support and the database support do not have to coordinate with one
another to take the database or the instance offline. For conversion purposes, a
single instance can be created for your database application environment using
the db2icrt command:
bash> db2icrt -u db2fenc1 db2inst1
Database
A database represents your data as a collection of data in a structured fashion. It
includes a set of system catalog tables that describes the logical and physical
structure of the data, a configuration file containing the environment parameter
values that are used by the database, and a recovery log with ongoing
transactions and transactions that can be archived.
Database partition
A database partition is part of a database, containing its own data, indexes,
configuration files, and transaction logs. A database partition is sometimes called
a node or a database node. A partitioned database environment is a database
installation that supports the distribution of data across database partitions. This
125
can be used if you want to spread your DB2 database across multiple servers in
a cluster or along multiple nodes. There are no database partition group design
considerations when using a non-partitioned database. The database partition
group can be created within a database by using the following command:
db2> CREATE DATABASE PARTITION GROUP MaxGroup ON ALL DBPARTITIONNUMS
Creating a database
The database in DB2 can be created simply by issuing the following command:
db2>CREATE DATABASE invent
This command generates a new database with a default path, and it generates
table spaces. It creates three initial table spaces and the system tables, and it
creates the recovery log.
You can use the CREATE DATABASE statement with options to personalize the
database and take advantage of DB2 advanced features, such as automatic
storage, which simplifies the storage management for table spaces, as shown in
Example 6-1. When using automatic storage, it is possible to specify a group of
storage devices for DB2 to use for your database. This specification allows DB2
to allocate and grow this specified space as table spaces are created and
populated. Automatic storage is turned on by default when creating a database.
Example 6-1 The CREATE DATABASE statement
db2> CREATE DATABASE invent AUTOMATICE STORAGE YES on '/db2fs/invent'
Dropping a database
In MySQL, you can drop the database using this statement:
mysql> DROP DATABASE [if exists] inventory
This statement removes all of the database files (.BAK, .DAT, .HSH, .ISD, .ISM,
.ISM, .MRG, .MYD, .MYI, .db, and .frm) from your file system.
DB2 has a similar command:
db2> DROP DATABASE invent [at DBPARTITIONNUM]
This command deletes the database contents and all log files for the database,
uncatalogs the database, and deletes the database subdirectory.
Alter database
The MySQL alter database command allows you to change the overall
characteristics of a database. For example, the character set clause changes the
126
database character set, and the collation clause changes the database collation.
Use this basic syntax for altering the database:
mysql>ALTER DATABASE inventory CHARACTER SET charset_name COLLATE
collation_name
In DB2, you can use the UPDATE DATABASE CONFIGURATION and UPDATE
DATABASE MANAGER CONFIGURATION commands to set the database and
database manager configuration parameters. These commands allow
modification of various configuration parameters, such as log file size, log file
path, heap size, cache size, and many others. You can take advantage of the
DB2 autonomic features by enabling the automatic maintenance and Self-Tuning
Memory Manager features. Automatic maintenance allows for scheduling
database backups, keeping statistics current, and reorganizing tables and
indexes. The Self-Tuning Memory Manager provides constant tuning of your
database without the need for DBA intervention. The following examples show
how these commands can be set in DB2:
db2> UPDATE DATABASE MANAGER CONFIGURATION using diaglevel 3
db2> UPDATE DATABASE ONFIGURATION for invent
using auto_maint on
auto_tbl_maint on
auto_runstats on
auto_reorg on
self_tuning_mem on
This example does not show all of the parameters that are available in DB2. For
more information about how to set up automatic maintenance and Self-Tuning
Memory Manager, visit this Web site:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
In addition, you can use these commands to change the physical and logical
partitioning of a database and to allocate table spaces and paging
configurations.
Table space
A table space is a storage structure containing tables, indexes, large objects, and
long data. Table spaces reside in database partition groups and allow the
assignment of database location and table data directly onto containers. DB2
allows for two types of table spaces: System Managed Space (SMS), where the
operating system allocates and manages the space where the tables are stored,
and Data Managed Space (DMS), where the database administrator has the
ability to decide which devices or files to use and allows DB2 to manage this
space. Another option is to enable automatic storage for the table spaces. No
container definitions are needed in the latter case, because the DB2 database
manager assigns and manages the container automatically.
127
Any DB2 database must have at least the following three table spaces:
One catalog table space, which contains system catalog tables
One or more user table spaces, which contain user-defined tables
One or more temporary table spaces, which contain temporary tables
You can create more table spaces by using the following commands:
db2> CREATE REGULAR TABLESPACE tblsp1 PAGESIZE 8192 MANAGED BY SYSTEM using
('/home/db2inst1/database/user8K') extentsize 8 prefetchsize 8 bufferpool
bp8k
db2> CREATE TABLESPACE tblsp2 MANAGED BY DATABASE using (device
'/dev/rhdisk6' 10000, device '/dev/rhdisk7' 10000, device '/dev/rhdisk8'
10000) overhead 12.67 transferrate 0.18
db2> CREATE TABLESPACE tblsp3 MANAGED BY AUTOMATIC STORAGE
Schema
A schema is an identifier, such as a user ID, that helps group tables and other
database objects. A schema can be owned by an individual, and the owner can
control access to the data and the objects within it. A schema is also an object in
the database. It can be created automatically when the first object in a schema is
created. We can create a schema:
db2>CREATE SCHEMA inventschema AUTHORIZATION inventUser
MySQL tables
As shown in Figure 6-5 on page 129, MySQL supports two types of tables:
transaction-safe tables and non-transaction-safe tables. Transaction-safe tables
(managed by InnoDB or NDB storage engines) are crash safe and can take part
in transactions providing concurrency features that allow commit and rollback.
Alternatively, non-transaction-safe tables (managed by MyISAM, MEMORY,
MERGE, ARCHIVE, or CSV storage engines) are less safe but are much faster
and consume less space and memory.
128
NDB
MyISAM is the default table type in MySQL. It is based on the ISAM code and
supports concurrent insert, big files on the operating system and file system
that support big files, better indexing, index compression, and table
compression.
MyISAM is a default MySQL table, so it can be created either by specifying
type=myisam or by not specifying any type:
mysql>CREATE TABLE tblmyisam (i int ,f float );
129
MERGE
The MERGE storage engine is a group of MyISAM tables in one table,
grouped together across the same disk or multiple disks. The tables must
have exact column and index structures.
MEMORY (HEAP)
MEMORY was previously called the HEAP storage engine. Tables that are
created in the MEMORY storage engine are stored in memory. The table
definition is stored on disk, but the rows are stored in memory. Therefore, the
table still exists after a reboot; however, all rows within this table are lost.
ARCHIVE
ARCHIVE stores the data in compressed format on disk and is typically used
to store large amounts of data. There are no indexes in the ARCHIVE storage
engine.
CSV
The CVS storage engine, which stands for comma separated value, allows
data to be stored in text files. This data can then be accessed via SQL calls.
FEDERATED
The FEDERATED storage engine works with non-transactional tables and
allows tables to be accessed on a remote server as though they were stored
locally. The table definition is stored locally; however, no data is actually
stored on the local server.
130
Example 6-4 shows how to create a table using the MyISAM storage engine.
Example 6-5 is the DB2 conversion. Note the changes:
Changes in the data type according to data type mapping
Instead of auto_increment, generated by default because identity is used
Example 6-4 Creating MySQL MyISAM table
mysql>CREATE TABLE tblmysiam (
wk_id int(11) unsigned NOT NULL auto_increment,
user_id int(11) unsigned default NULL,
cnt int(10) unsigned default NULL,
cat_id int(12) unsigned default NULL,
status varchar(10) default NULL,
PRIMARY KEY (wk_id)) type=MyISAM;
Example 6-5 Conversion of MySQL MyISAM table creation
db2>CREATE TABLE tblmysiam (
wk_id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
user_id INT default NULL,
cnt INT default NULL,
cat_id INT default NULL,
status VARCHAR(10) default NULL,
PRIMARY KEY (wk_id));
Example 6-6 is a MySQL table creation example using the ARCHIVE engine.
Example 6-7 is the DB2 conversion using row compression.
Example 6-6 Creating MySQL ARCHIVE table
mysql>CREATE TABLE tblarchive(col1 int, col2 char(10)) type=ARCHIVE;
Example 6-7 DB2 conversion of creating MySQL ARCHIVE table
db2>CREATE TABLE tblarchive(col1 int, col2 char(10)) COMPRESS YES
Example 6-8 on page 132 shows how to create a MySQL table with partitioning.
Example 6-9 on page 132 is the DB2 conversion; again, no major change is
required.
131
Example 6-8 Creating MySQL table using partitioning with the default MyISAM storage
engine
mysql>CREATE TABLE partsales (id INT, item VARCHAR (20) )
PARTITION BY RANGE (id)(
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (20)
);
Example 6-9 DB2 conversion of MySQL table creation with partitioning
db2>CREATE TABLE partsales (id INT, item VARCHAR (20)
PARTITION BY RANGE (id)(
PARTITION p1 STARTING FROM (MINVALUE) ENDING (10),
PARTITION p2 ENDING (20)
);
Alter table
ALTER TABLE is a statement that is used to modify one or more properties of a
table. The syntax of the ALTER TABLE statement for MySQL and DB2 is quite
similar and is shown in Example 6-10.
Example 6-10 MySQL and DB2 ALTER TABLE example
mysql>ALTER TABLE partsales modify status varchar(20);
db2>ALTER TABLE partsales alter column status set data type varchar(20)
ALTER TABLE in DB2 now supports dropping columns. DB2 drops columns
using a temporary table. The syntax of ALTER TABLE for MySQL and DB2 is
similar, as shown in Example 6-11.
Example 6-11 MySQL and DB2 dropping column example
mysql> ALTER TABLE sales drop column c1, drop column c2;
db2> ALTER TABLE sales drop c1 drop c2
Drop table
Tables can easily be deleted from the database by issuing the DROP TABLE
statement as shown.
For MySQL:
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name,...] [RESTRICT
|CASCADE]
132
For DB2:
DROP table tbname
DB2 uses the update-able UNION ALL view to achieve the merge table
feature. UNION ALL views are commonly used for logically combining
different but semantically related tables. The UNION ALL view is also used for
unification of like tables for better performance, manageability, and
integrating federated data sources.
The following example shows using the UNION ALL command for views:
db2>CREAT VIEW UNIONVIEW as SELECT * FROM table1 UNION ALL SELECT * FROM
table2
MEMORY table
A MEMORY table is a hashed index that is always stored in memory. Memory
tables are fast but are not crash safe. When MySQL crashes or has a
scheduled reboot, the MEMORY table will still exist on the reboot but the data
is lost.
A MySQL Memory table can be created using the following command:
mysql> CREATE TABLE memtable type=MEMORY SELECT * FROM table1;
133
Temporary table
DB2 temporary tables are tables that are used for storing data in
non-persistent, in-memory, session-specific tables. When a session is
over, the table definition for the table is lost. When your application uses
the MEMORY table in this fashion, temporary tables can be declared in
your application by calling these statements:
Create user temporary table space if it does not exist by using this
statement:
db2>CREATE USER TEMPORARY TABLESPACE discompose MANAGED BY SYSTEM
using ('usertemp1')
134
Typed tables are tables that are defined with a user-defined structured type.
With typed tables, you can establish a hierarchical structure with a defined
relationship between tables called a table hierarchy. The table hierarchy is
made up of a single root table, supertables, and subtables. Example 6-13
shows the creation of a typed table.
Example 6-13 Usage of typed/hierarchy table
//Here is the SQL to create the BusinessUnit typed table
db2>CREATE TABLE BusinessUnit OF BusinessUnit_t (REF IS Oid USER GENERATED)
//Here is the SQL to create the tables in the Person table hierarchy
db2>CREATE TABLE Person OF Person_t (REF IS Oid USER GENERATED)
db2>CREATE TABLE Employee OF Employee_t UNDER Person INHERIT SELECT PRIVILEGES
(SerialNum WITH OPTIONS NOT NULL, Dept WITH OPTIONS SCOPE BusinessUnit )
db2> CREATE TABLE Student OF Student_t UNDER Person INHERIT SELECT PRIVILEGES
db2> CREATE TABLE Manager OF Manager_t UNDER Employee INHERIT SELECT PRIVILEGES
db2> CREATE TABLE Architect OF Architect_t UNDER Employee INHERIT SELECT
PRIVILEGES
135
Views
Views are the named specification of a result table. This specification is a
select statement that is run whenever the view is referenced in an SQL
statement. It can be used just like a base table.
You can create a simple view by issuing a create statement, as shown in
Example 6-14.
Example 6-14 Create view example
db2>CREATE TABLE table1(col1 INTINT, col2 INT, col3 CHAR(20),col4 FLOAT,
col5 CHAR(30))
db2>CREATE TABLE itable2(col6 INT, col7 INT, col8 CHAR(20),col9 FLOAT,
col10 CHAR(30))
db2>CREATE VIEW myview(col1,sum1,col4,col10) AS
SELECT col1,col1+col6,col4,col10
FROM table1, table2
WHERE col1<10 AND col6>10
Primary key
Unique
Non-unique
Fulltext
Spatial
DB2 supports all of the index types that are supported by MySQL with the same
terminology, allowing them to map directly during conversion.
Create index
The following example shows the MySQL CREATE INDEX syntax:
CREATE [ONLINE|OFFLINE] [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[index_type]
ON tbl_name (index_col_name,...)
index_col_name:
col_name [(length)] [ASC | DESC]
136
Drop index
This example shows the DROP INDEX statement for MySQL and DB2:
mysql> DROP INDEX index_name ON tbl_name;
db2> DROP INDEX index_name
137
Multiple servers
In certain cases, multiple MySQL servers are placed on the same machine.
Possible reasons include user management, testing, or potentially differentiating
applications. MySQL provides an option to run multiple servers on same
machine using several operating parameters.
There are several ways to configure a new server; we used this method:
bash> /path/to/mysqld --socket=file_name --port=port_number
138
139
Figure 6-7 MySQL application using multiple DBs instead of multiple schema
The tables created in the particular schema can be accessed using the full table
qualifier table schema.table name.
140
Table placement
MySQL does not support table spaces for managing physical location and page
size or distributing tables onto the different table spaces, except with the optional
InnoDB storage engine, which supports multiple table spaces by distributing
them into separate files.
DB2 supports table spaces to establish the relationship between the physical
storage devices that are used by your database system and the logical
containers that are used to store data. Table spaces reside in database partition
groups. They allow you to assign the location of table data directly onto
containers.
Prior to the conversion of your database structure, you must create proper table
spaces of various sizes in DB2. Individual or multiple tables can then be
assigned to the tables spaces. If the table space design is done effectively, it can
greatly increase performance. We discuss this topic in more detail in Chapter 10,
Testing and tuning on page 321.
List information
MySQL provides a show command to list the information about databases,
tables, columns, or status information about the server.
DB2 provide commands for getting information about instances, databases, table
spaces, and other objects. The DB2 system catalogs contain all of the necessary
information about tables, columns, indexes, and other objects. You can use the
describe and list commands to display database and table structure or use the
select statement to get the details of the table definition.
Table 6-2 shows examples of MySQL and corresponding DB2 statements or
commands to list database or table-related information.
Table 6-2 MySQL to DB2 conversion of list information statement
MySQL
DB2
Show databases
List tables
141
Referential integrity refers to the constraints that are defined on the table and its
columns, which help you to control the relationship of data in various tables.
Essentially, referential integrity involves primary keys, foreign keys, and unique
keys.
Primary keys and unique keys are treated similarly in MySQL and DB2.
However, MySQL currently only parses foreign key syntax in the CREATE
TABLE statements. MySQL does not use or store the information about foreign
keys, except in InnoDB tables, which support checking foreign key constraints,
including CASCADE, ON DELETE, and ON UPDATE.
DB2 provides full support for foreign keys. With the full referential integrity
functionality from DB2, your application can be released from the job of taking
care of the data integrity issues. Example 6-16 shows the creation and usage of
foreign key constraints in DB2.
Example 6-16 Foreign key constraint usage
db2> ALTER TABLE table1 ADD CONSTRAINT foreign1 FOREIGN KEY (id)
REFERENCES table2 ON DELETE SET NULL
We discuss foreign key creation in more detail in 6.5.2, Manual database object
conversion and enhancements on page 158.
142
143
The following steps briefly describe the database structure conversion process
when using the IBM Data Movement Tool:
1. Specify source and DB2 database server connection information.
2. Test the connection to the source and target database. Click Connect to
MySQL to test the connection and Connect to DB2 to test the DB2
connection.
3. Specify the working directory to where the DDL and the data will be extracted.
4. With the IBM Data Movement Tool, you have the option to extract only the
database objects, only the data, or both. Choose if you want DDL and DATA.
5. Click Extract Data to extract the DDL and DATA and automatically convert to
DB2 syntax. You can monitor the progress in the console window.
6. After the data extraction completes successfully, review the result output files
for the status of the data movement, any warnings, errors, and other potential
issues. Optionally, you can click View Script/Output to check the generated
scripts, DDL, data, or the output log file.
7. Click Deploy Data to automatically create tables and indexes in DB2 and to
load data that was extracted from the source database. Optionally, you can
click Interactive Deploy to deploy database objects one at a time.
In this chapter, we only discuss the database conversion portion of the
conversion process.
144
145
146
To create the database, invoke this SQL script from the DB2 command-line
window or bash shell by using this command:
db2>@create-database.sql
bash>db2 -f create-database.sql
We then use the database object creation statements from the output of
mysqldump and the mysqlobjects.ddl file to create the db2objects.ddl script.
Change the DDL statements and data types based on the discussion in 6.1,
Data type mapping on page 116 and 6.2, Data definition language differences
on page 122. You can create any additional statements that are required.
Now, we execute the newly created DDL scripts from the bash shell, as shown in
Example 6-18.
Example 6-18 Create database object from the bash shell
bash>db2 CONNECT to invent
bash>db2 -tf db2objects.ddl
bash>db2 DISCONNECT invent
147
6.5.1 Converting database objects with the IBM Data Movement Tool
The following steps describe the process of converting the MySQL sample
Inventory database to DB2 using the IBM Data Movement Tool.
148
For this example, we use the following command to create our new DB2
database:
db2> CREATE DATABASE invent AUTOMATIC STORAGE YES ON
'/home/db2inst1/invent'
149
You can also run the IBM Data Movement Tool in command-line mode. The
tool automatically switches to command-line mode if it is not able to start the
GUI. If you want to run the tool in command-line mode, you can run the
following command:
<IBM Data Movement Tool Installation directory>/IBMDataMovementTool.sh
-console
db2inst1@db2server:/opt/ibm/IBMDataMovementTool>
./IBMDataMovementTool.sh -console
150
Figure 6-9 Testing database connection using IBM Data Movement Tool
151
movement using the command-line approach, you can control the number of
threads by modifying NUM_THREADS in the unload script. You can monitor
the progress in the console window, as shown in Figure 6-10.
After the data extraction is complete, read through the result output files for the
status of the data movement, warnings, errors, and other potential issues. You
can click View Script/Output from the Extract/Deploy window to check the
generated scripts, DDL, data, or output log file.
Table 6-3 on page 153 shows the command scripts that are regenerated each
time that you run the tool in GUI mode. These scripts can also be issued in
console mode without the GUI, which is helpful when you want to embed this tool
as part of a batch process to accomplish an automated data movement.
152
Description
IBMExtract.properties
geninput
This script is the first data movement step where you will
create an input file that contains the names of the tables to
move. You can edit this file manually to exclude tables that
you do not want to move.
genddl
unload
rowcount
This file will be used after you have moved the data to perform
a sanity check for the row count for tables in source and target
database servers.
153
From the Interactive Deploy window, you can perform a number of tasks:
Refresh the database object list.
Select the refresh button (circled in Figure 6-12 on page 155) to refresh the
list of database objects in the DB2 Objects view on the left side of the window.
Edit the object definition.
You can select the database object that you want to modify and edit in the
right panel, as shown in Figure 6-12 on page 155. To save and deploy
changes, deploy the object before selecting a new object. After deployment,
you can return to refine any objects that failed to deploy.
154
You can also edit the scripts that were extracted into the conversion directory.
To change the table definition, edit the db2tables.sql file:
db2inst1@db2server:/opt/ibm/IBMDataMovementTool/migr> vi db2tables.sql
155
156
Important: You must not reduce the size of any field, because it can cause
an error while converting the data.
Deploy objects to target.
The final step of the database conversion is to deploy the database objects.
You use Deploy All (circled red in Figure 6-13 on page 158) to deploy all
objects in the database object view or select Deploy Selected (circled blue in
Figure 6-13 on page 158) to deploy the database object currently selected.
You can view the status of the database object deployment and error
messages in the lower-right panel, as shown in Figure 6-13 on page 158.
157
Views
You must manually port views from MySQL to DB2. You can extract the MySQL
view definition from the MySQL database using the mysqldump utility or
selecting from the INFORMATION_SCHEMA.VIEWS table.
The syntax for a view in MySQL and DB2 is extremely similar, which makes it
simple to convert the DDL for this database object. Example 6-20 on page 159
shows the CREATE VIEW syntax for the MySQL views.
For our example, we alter the CREATE VIEW commands to match the DB2
syntax, as shown in Example 6-21 on page 159.
158
159
o.phoneNum,
(SELECT count(*) FROM admin.inventory in, admin.owners u
WHERE u.id = o.id and u.id = in.ownerID) AS inventNum
FROM admin.owners o, admin.locations l
WHERE o.groups = 'tech' and o.locID = l.id;
Trigger conversion
Triggers also require manual conversion to port them from MySQL to DB2. You
can extract the MySQL trigger definition from the MySQL database using the
mysqldump utility or selecting from the INFORMATION_SCHEMA.TRIGGERS
table.
Example 6-22 on page 161 shows the MySQL CREATE TRIGGER statement
and Example 6-24 on page 161 shows the DB2 CREATE TRIGGER statement.
In these examples, note the difference between new and old data values that are
declared and referenced. Refer to 6.2.4, Trigger manipulation on page 137 for
more details about syntax differences between MySQL and DB2.
Also, notice the change in the date function. You must find the equivalent
functions in DB2 when converting DDL and DML statements that contain built-in
functions. We discuss and compare MySQL and DB2 built-in functions and
operators in more detail in 8.1.10, Built-in functions and operators on page 221.
We have found a similar function in DB2 to replace the curDate function in
MySQL. However, the default date format needs to be modified to the ISO format
(YYYY-MM-DD), because the default format for the DB2 current date function is
MM/DD/YY, whereas the MySQL curDate function is YYYY-MM-DD. The
commands that are described in Example 6-23 on page 161 change the default
format to the ISO format (YYYY-MM-DD).
160
= DB2/LINUX 9.7.0
= DB2INST1
= INVENT
161
Foreign keys
Now, you can add any additional enhancement to your database using the DB2
features that might have not been supported in your existing MySQL storage
engine. One example is referential integrity, which is essential to the database by
ensuring consistency of data values between related columns in separate tables.
Referential integrity is usually maintained by using the primary key, unique key,
and foreign keys. MySQL only supports foreign keys in the InnoDB engine.
Primary and unique keys are successfully converted using the IBM Data
Movement Tool, but at the time of writing this book, foreign keys are not
supported for a MySQL conversion project. If you want to create foreign keys in
your database or to convert your foreign keys from an Innodb database, you
need to perform this task manually.
In our sample application, we create referential integrity between tables in a file
called the referential.ddl file, which looks like Example 6-27.
Example 6-27 Add foreign keys
ALTER TABLE admin.owners ADD CONSTRAINT OWNERLOCI FOREIGN KEY (locid)
REFERENCES admin.locations (id) ON DELETE CASCADE;
ALTER TABLE admin.owners ADD CONSTRAINT OWNERGRPI FOREIGN KEY (groups)
REFERENCES admin.groups (GROUPNAME) ON DELETE CASCADE;
162
Now, we have completed the DDL modification. We execute the changed scripts
in Example 6-27 on page 162 to create the DB2 database and the objects, as
shown in Example 6-28.
Example 6-28 Creation of tables and databases in DB2
db2inst1@db2server:~/DB2Scripts> db2 CONNECT TO invent
Database Connection Information
Database server
SQL authorization ID
Local database alias
= DB2/LINUX 9.5.0
= DB2INST1
= INVENT
163
Automatic maintenance
Performing maintenance activities on your databases is essential to ensure that
they are optimized for performance and recoverability. The database manager
provides automatic maintenance capabilities for performing database backups,
keeping statistics current, and reorganizing tables and indexes as necessary.
For users, it can be time-consuming to determine when to run maintenance
activities. Automatic maintenance takes the burden off of the users. With
automatic maintenance, you can specify your maintenance objectives, including
when automatic maintenance can run. The DB2 database manager uses the
objectives that you have specified to determine if the maintenance activities need
to be done and runs only the required maintenance activities during the next
available maintenance window (a user-defined time period for running automatic
maintenance activities). Example 6-29 shows the activities that can be controlled
by the database managers automatic maintenance feature.
Example 6-29 Automatic maintenance database manager variables
Automatic
Automatic
Automatic
Automatic
Automatic
Automatic
Automatic
Automatic
maintenance
database backup
table maintenance
runstats
statement statistics
statistics profiling
profile updates
reorganization
(AUTO_MAINT) = ON
(AUTO_DB_BACKUP) = OFF
(AUTO_TBL_MAINT) = ON
(AUTO_RUNSTATS) = ON
(AUTO_STMT_STATS) = OFF
(AUTO_STATS_PROF) = OFF
(AUTO_PROF_UPD) = OFF
(AUTO_REORG) = ON
164
performance, the Self-Tuning Memory Manager can adapt quickly to adjust the
memory configuration.
Self-tuning memory simplifies the task of memory configuration by automatically
setting values for memory configuration parameters and sizing buffer pools.
When enabled, the memory tuner dynamically distributes available memory
resources between several memory consumers, including sort, package cache
and lock list areas, and buffer pools. Example 6-30 shows how to enable
Self-Tuning Memory Manager and how to enable specific parameters to be
automatically tuned.
Example 6-30 Setting up Self-Tuning Memory Manager
db2> UPDATE DATABASE CONFIGURATION FOR invent SELF_TUNING_MEM ON
db2> UPDATE DB CFG FOR invent USING locklist AUTOMATIC
maxlocks AUTOMATIC
pckcachesz AUTOMATIC
sheapthres_shr AUTOMATIC
sortheap AUTOMATIC
165
166
Chapter 7.
Data conversion
There are various considerations around converting data from a MySQL
database to DB2. In this chapter, we focus on several of these considerations
and describe the usage of tools and commands that aid in data movement from a
MySQL database to a DB2 database.
In this chapter, we also discuss the differences in specific data formats and data
types and ways in which they can be converted from MySQL to DB2.
We also describe how user account management (user data, access rights, and
privileges) is implemented in MySQL and how this information can be ported to
implement secure database access within DB2.
Finally, we discuss in detail the steps that we performed to convert the data in
our sample project.
167
168
For a complete description of this tool, run mysqldump --help. Consider these
important command-line options:
--no-data
This option ensures that no data is extracted from the database, just the SQL
statements for creating the tables and indexes. Therefore, this option is used
for extracting DDL statements only.
--no-create-info
This option ensures that no SQL statements for creating the exported table
are extracted from the database. Therefore, it is used for exporting data only.
The output file containing the data can be loaded into a DB2 table at a later
time.
--tab=<outFilePath>
This option creates a text file with the DDL (<tablename>.sql) and a tab
separated text file with the data (<tablename>.txt) in the given path for each
specified table. This option works only when the utility is run on the same
machine as the MySQL daemon. If this option is not specified, INSERT
statements for each row are created.
Example 7-1 shows the usage and output of the mysqldump command using only
the --user and --password options. The output includes DDL statements for table
creation and INSERT statements to insert data into the table.
Example 7-1 Usage of mysqldump with only the --user and --password options
mysqlServer:~ # mysqldump --user root --password inventory severity
------
Host: localhost
Database: inventory
-----------------------------------------------------Server version
5.1.36-log
169
2:11:48
Example 7-2 shows the usage and output of the mysqldump command with the
--no-create-info option, but without the --tab option. The output has only INSERT
statements for each row to insert data into the table.
Example 7-2 Usage of mysqldump with the --no-create-info but without the --tab option
mysqlServer:~# mysqldump--no-create-info --user root -password inventory severity
------
Host: localhost
Database: inventory
-----------------------------------------------------Server version
5.1.36-log
170
(1,'high',NULL,1,2);
UNLOCK TABLES;
2:28:23
Example 7-3 shows the usage and output of the mysqldump command with the
--no-create-info and the --tab options. This command outputs a file in the current
directory named <tableName>.txt that contains only the exported MySQL data.
This file can be read by the DB2 LOAD command.
Example 7-3 Usage of mysqldump with the --no-create-info and the --tab option
mysqlServer:~ # mysqldump --no-create-info --tab=. --user root --password
inventory severity
mysqlServer:~ # cat severity.txt
2
4
3
5
1
high-med
low-med
medium
low
high
\N
\N
\N
\N
\N
4
10
7
14
1
4
10
7
12
2
Example 7-4 shows the usage and output of mysqldump without the
--no-create-info option but with the --tab option. This command outputs two files:
one file contains the DDL statements for table creation (<tableName>.sql) and
the other file contains the exported MySQL data (<tableName>.txt) in the current
directory. The second file can be read by the DB2 LOAD command.
Example 7-4 Usage of mysqldump without the --no-create-info but with the --tab option
mysqlServer:~ # mysqldump --tab=. --user root -password inventory severity
mysqlServer:~ # cat severity.sql
------
Host: localhost
Database: inventory
-----------------------------------------------------Server version
5.1.36-log
171
2:49:30
172
There are a few other scripts that are generated during the DDL extraction phase
that are used to extract the data from the MySQL database and load it into DB2.
The unload script is used to unload the data from the MySQL database and store
it in <schema>_<tableName>.txt files in the <migration output
directory>/data. You can then use the db2load.sh script to load these files into
the DB2 database. The db2load.sql script is executed from the db2gen.sh script,
which also executes other .sql generated scripts to port the database. You can
also execute these scripts using the GUI, which we discuss further in this
chapter.
Load data.
Build indexes.
Delete rows with a unique key violation or a data link violation.
Copy index data from the system temporary table space to the original table
space.
See Example 7-6 for a simplified syntax diagram for the LOAD command. For a
complete syntax description, visit the Information Center at this Web site:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
173
>--+--------------+--+-------------+--+-----------------+------->
'-SAVECOUNT--n-' '-ROWCOUNT--n-' '-WARNINGCOUNT--n-'
>--+------------------------+----------------------------------->
'-MESSAGES--message-file-'
>--+-------------------------------+--+-INSERT----+------------->
'-TEMPFILES PATH--temp-pathname-' +-REPLACE---+
+-RESTART---+
'-TERMINATE-'
>--INTO--table-name--+-------------------------+---------------->
|
.-,-------------.
|
|
V
|
|
'-(----insert-column-+--)-'
.-ALLOW NO ACCESS-----------------------------.
>--+---------------------------------------------+-------------->
'-ALLOW READ ACCESS--+----------------------+-'
'-USE--tablespace-name-'
>--+--------------------------------------+--------------------->
'-CHECK PENDING CASCADE--+-IMMEDIATE-+-'
'-DEFERRED--'
>--+-----------------+------------------------------------------>
'-LOCK WITH FORCE-'
The IBM Data Movement Tool uses the LOAD utility for loading the application
data into DB2. Example 7-7 shows an example of the LOAD command that is
used to load the data into the severity table.
Example 7-7 IBM Data Movement Tool LOAD example
--#SET :LOAD:ADMIN:SEVERITY
LOAD FROM "/opt/ibm/IBMDataMovementTool/migr/data/admin_severity.txt"
OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL""
DELPRIORITYCHAR NOROWWARNINGS
--DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_severity.txt"
METHOD P (1,2,3,4,5)
MESSAGES "/opt/ibm/IBMDataMovementTool/migr/msg/admin_severity.txt"
REPLACE INTO "ADMIN"."SEVERITY"
(
"ID",
"TITLE",
"NOTES",
"ESTDAYS",
"AVGDAYS"
)
--STATISTICS YES WITH DISTRIBUTION AND DETAILED INDEXES ALL
NONRECOVERABLE
INDEXING MODE AUTOSELECT
;
174
Binary Large Objects (BLOB data type) usually contain binary data. Exporting of
binary data into text files is not possible. So, if your BLOBs contain binary data,
you must convert them in a different way than exporting and loading. The IBM
Data Movement tool handles the conversion BLOB data for you.
175
The DB2 LOAD command lets you specify the file-type-modifier clause
TIMESTAMPFORMAT, which determines the formatting of the TIMESTAMP
values. If you want to import MySQL TIMESTAMP values, you must change the
LOAD command in the deploy.sh script to the following syntax:
db2 LOAD from ""<outFilePath>/<tablename>.dat"" |
of DEL
modified by
coldel0x09
timestampformat=\" YYYY-MM-DD HH:MM:SS\"
insert into <schemaname>.<tablename>
If you use the IBM Data Movement Tool, you do not have to worry about this
format, because the tool takes care of this formatting for you.
When extracting the data from the GUI tool, make sure to select No when
requested to recreate the conversion directory and the <database>.tables file.
Otherwise, your changes will be overwritten.
176
User accounts
When assessing your application, be sure to distinguish between the following
user types:
Application user accounts
These users log on to the application, but they do not exist on the database
level. Database access is through the application with the applications
database user ID. Because the information about application users is usually
stored in custom application tables, the porting of application user account
data is done when porting the MySQL application data. In the sample
inventory database, this custom application table is our owners table.
Database user accounts
Database users connect directly to the MySQL database to retrieve and
manipulate data. At least one database user must exist for applications to
connect to the database. Database user accounts are created with the
MySQL server and allow you to grant and restrict access to portions of the
MySQL servers. A database user account is associated with a host name and
user name. The user account information is stored with the mysql.user table
and must be ported in data conversion step. Access rights and privileges for
these users are stored in the mysql.db, mysql.host, mysql.tables_priv,
mysql.columns_priv, and mysql.procs_priv tables.
Passwords
Database users have associated passwords, which are stored encrypted in the
mysql.user table. Encrypted passwords cannot be ported and must be reset on
the new system. The password of the database user, which is used by an
application to access the database, is typically stored in a profile with restricted
rights.
177
Access rights
When accessing a MySQL database, there are two levels of access control.
When you first connect to a MySQL server, you provide a user name and the
associated password. Furthermore, the machine that you are connecting from
must be associated with this user to allow the connection. This requirement is
based on the assumption that a user with a specific user name from one host is
different from a user with the same user name from a separate host.
This access information is stored in the mysql.user table in the fields: user,
password, and host. The MySQL wildcard ampersand character (%) is often
used in the host field to specify that this user can connect from any host. The
wildcard underscore (_) is also sometimes used for single characters.
In Example 7-10, the user1 user can connect from any host, the user2 user can
connect from only the remoteHost.ibm.com host, and db2inst1 can connect from
localhost, myServer, and 127.0.0.1.
Example 7-10 Sample user data for connection verification
mysql> select user, password, host from mysql.user;
+---------+-------------------------------------------+--------------------+
| user
| password
| host
|
+---------+-------------------------------------------+--------------------+
| db2inst1| EE2FD1618DEE42FD1618BB9JD33HD736H5HNNT757 | myServer
|
| db2inst1| GGFD1618BDJ473SKCNGU8JB9JD33HD73J5748SUFL | 127.0.0.1
|
| user1 | 2FD162470C0C06DEE418BDCA2EC9D1E1B99005A9K | %.com
|
| user2 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | remoteHost.ibm.com |
| db2inst1| 06DE005ADCA2EC2470C0C99005*ADCA2EC8BB9900 | localhost
|
+---------+-------------------------------------------+--------------------+
Refer to the MySQL Reference Manual at this Web site for a complete
description of the MySQL connection verification:
http://dev.mysql.com/doc/refman/5.1/en/index.html
You can also find information about how the entries in the mysql.user table are
ordered when the provided connection data meets more than one connection
criteria.
178
Privileges
The second level of access control occurs after a connection to the MySQL
database is established. Each time that a command is run against the database,
MySQL checks if the connected user has sufficient privileges to run this
command.
Privileges exist for selecting, inserting, updating, and deleting data for creating,
altering, and dropping database objects and other operations performed at the
database level.
All privileges are stored in either the mysql.user, mysql.db, mysql.host,
mysql.tables_priv, mysql.columns_priv, or mysql.procs_priv tables.
Privileges in MySQL can be granted on the following levels:
Global level
Global privileges provide control over the overall MySQL server and are
stored in the mysql.user table.
Database level
Database privileges provide control over a specific database and are stored
in the mysql.db (database privileges) and mysql.host (host privileges) tables.
Table level
Table privileges provide control over a specific table and are stored in the
mysql.tables_priv table.
Column level
Column privileges provide control over a specific column in a table and are
stored in the mysql.columns_priv table.
Routine level
Stored routine privileges provide control over a specific stored routine and are
stored in the mysql.procs_priv table.
Privileges can be granted to users with the MySQL GRANT command; they can
be revoked with the REVOKE command.
For more information about MySQL privileges, see the MySQL Reference
Manual at this Web site:
http://dev.mysql.com/doc/refman/5.1/en/
179
User accounts
To create a user for DB2 implies creation of a user in the servers operating
system, assigning the user to a group, and granting specific database privileges
to the user or group.
On Linux systems, you must have root access to the system to create groups
and users. Group information is stored in the /etc/group file and user
information is stored in the /etc/passwd file.
For example, if you want to create a new db2app1 group with one user, db2usr1,
to access a specific DB2 table, perform the necessary steps:
1. Log on to the Linux system with root privileges.
2. Create the group. Make sure that the provided group name does not already
exist and ensure that it is not longer than eight characters:
groupadd [-g 995] db2app1
3. Create the user and assign it to the previously created group. Make sure that
the ID for the user does not already exist and that it is not longer than eight
characters:
useradd [-u
If the user will access the DB2 database locally, continue with the next two
steps:
a. Edit the profile of the created user:
vi /home/db2usr1/.profile
b. Add the following line to the profile. Be sure to specify the path of your
DB2instance owners home directory and to specify a blank between the
dot and the command:
.
/home/db2inst1/sqllib/db2profile
DB2 9.5 introduced roles to simplify the management of authorization. Roles are
equivalent to in-database groups and allow DBAs to group together one or more
privileges, authorities, or security labels. Roles can be assigned to users, groups,
PUBLIC, or other roles by using the GRANT statement.
180
Passwords
The passwords that are used for DB2 are the system passwords of the user. To
set a password in Linux, use the passwd <username> command as root user.
Access rights
The first component in the DB2 security model is authentication. Access to DB2
databases is restricted to users that exist on the DB2 system. When connecting
to a DB2 database, you have to provide a user name and password combination
that is valid against the servers system. Authentication can occur at the DB2
server or the DB2 client using operating system authentication, Kerberos, or an
external security manager.
Administrative authority
A user or group can have one or more of the following administrative authorities.
System-level authorization
The system-level authorities provide varying degrees of control over
instance-level functions:
SYSADM authority
The SYSADM (system administrator) authority provides control over all the
resources created and maintained by the database manager. The system
administrator possesses all authorities for the SYSCTRL, SYSMAINT, and
SYSMON authority. The user who has SYSADM authority is responsible for
both controlling the database manager and ensuring the safety and integrity
of the data.
SYSCTRL authority
The SYSCTRL authority provides control over operations that affect system
resources. For example, a user with SYSCTRL authority can create, update,
start, stop, or drop a database. This user can also start or stop an instance,
but cannot access table data. Users with SYSCTRL authority also have
SYSMON authority.
181
SYSMAINT authority
The SYSMAINT authority controls maintenance operations on all databases
associated with an instance. A user with SYSMAINT authority can update the
database configuration, back up a database or table space, restore an
existing database, and monitor a database. Like SYSCTRL, SYSMAINT does
not provide access to table data. Users with SYSMAINT authority also have
SYSMON authority.
SYSMON authority
The SYSMON (system monitor) authority controls the usage of the database
system monitor.
Figure 7-1 illustrates the instance-level authorities that can be granted to a user
or role.
182
Database-level authorization
The database-level authorities provide control within a database:
DBADM (database administrator)
The DBADM authority level provides administrative authority over a single
database. This database administrator possesses the privileges required to
create objects and issue database commands.
The DBADM authority can only be granted by a user with SECADM authority
and cannot be granted to PUBLIC.
SECADM (security administrator)
The SECADM authority level provides administrative authority for security
over a single database. The security administrator authority possesses the
ability to manage database security objects (database roles, audit policies,
trusted contexts, security label components, and security labels) and grant
and revoke all database privileges and authorities. A user with SECADM
authority can transfer the ownership of objects that they do not own. They can
also use the AUDIT statement to associate an audit policy with a particular
database or database object at the server.
The SECADM authority has no inherent privileges to access data stored in
tables. This authority can only be granted by a user with SECADM authority.
The SECADM authority cannot be granted to PUBLIC.
SQLADM (SQL administrator)
The SQLADM authority level provides administrative authority to monitor and
tune SQL statements within a single database. It can be granted by a user
with ACCESSCTRL or SECADM authority.
WLMADM (workload management administrator)
The WLMADM authority level provides administrative authority to manage
workload management objects, such as service classes, work action sets,
work class sets, and workloads. It can be granted by a user with
ACCESSCTRL or SECADM authority.
EXPLAIN (explain authority)
The EXPLAIN authority level provides administrative authority to explain
query plans without gaining access to data. It can only be granted by a user
with ACCESSCTRL or SECADM authority.
183
184
185
Privileges
A privilege is a permission to perform an action or a task. Authorized users can
create objects, have access to objects they own, and can pass on privileges on
their own objects to other users by using the GRANT statement.
Privileges can be granted to individual users, groups, or PUBLIC. PUBLIC is a
special group that consists of all users, including future users. Users that are
members of a group will indirectly take advantage of privileges granted to the
group, where groups are supported.
Privileges can be assigned to a user in the following three ways:
Explicit
Individual privileges can be explicitly granted to allow a user or group to carry
out specific tasks on specific objects. These privileges can be given and
revoked using the GRANT and REVOKE commands, for example:
db2 GRANT SELECT on TABLE db2inst1.t1 to employee
db2 REVOKE SELECT on TABLE db2inst1.t1 from employee
In this example, the user who issued the CREATE TABLE statement has full
CONTROL privileges over table t2.
Possessing the CONTROL privilege on an object allows a user to access that
database object, and to grant and revoke privileges to or from other users on
that object. The creator of a base table, materialized query table, staging
table, or nickname automatically receives the CONTROL privilege. The
creator of a view automatically receives the CONTROL privilege if the user
186
holds the CONTROL privilege on all tables, views, and nicknames identified
in the fullselect.
Indirect
When a user has the privilege to execute a package or routine, the user does
not necessarily require specific access privileges on the objects handled in
the package or routine. If the package or routine contains static SQL or
XQuery statements, the privileges of the owner of the package are used for
those statements. If the package or routine contains dynamic SQL or XQuery
statements, the authorization ID used for privilege checking depends on the
setting of the DYNAMICRULES BIND option of the package issuing the
dynamic query statements, and whether those statements are issued when
the package is being used in the context of a routine.
A user or group can be authorized for any combination of individual privileges
or authorities. When a privilege is associated with a resource, that resource
must exist. For example, a user cannot be given the SELECT privilege on a
table unless that table has previously been created.
Note: Care must be given to granting authorities and privileges to user names
that do not exist in the system yet. At a later time, this user name can be
created and automatically receive all of the authorities and privileges
previously granted.
Privileges and authorities in DB2 can be granted on the following levels:
Database level:
CONNECT privilege
CREATETAB privilege
LOAD privilege
IMPLICIT_SCHEMA privilege
BINDADD privilege
CREATE_EXTERNAL_ROUTINE privilege
CREATE_NOT_FENCED_ROUTINE privilege
IMPLICIT_SCHEMA privilege
LOAD privilege
QUIESCE_CONNECT privilege
ACCESSCTRL authority
DATAACCESS authority
EXPLAIN authority
DBADM authority
SECADM authority
SQLADM authority
WLMADM authority
Schema level
187
CREATEIN privilege
ALTERIN privilege
DROPIN privilege
Table space level:
USE privilege
Table and view level:
CONTROL privilege
SELECT privilege
INSERT privilege
UPDATE privilege
DELETE privilege
INDEX privilege
ALTER privilege
REFERENCES privilege
ALL PRIVILEGES privilege
Package privileges
Index level privileges
Procedure, function, and method privileges
Sequence privileges
188
Note: Catalogs contain, among other things, statistics about data distribution
in a table, which are needed by the query optimizer to determine the best way
to execute a query. Users can indirectly gain knowledge about certain data
values by accessing the catalogs. When using LBAC, you can add the new
RESTRICT_ACCESS option on the CREATE DATABASE command to create
a database where access to the catalogs is not granted to PUBLIC. Access to
the catalogs can then be granted on a need-to-know basis.
MySQL
scope
DB2 scope
ALL
[PRIVILEGES]
Global
Instance
Database
Database
Table
Table
Global
ALTERIN
For each
schema in the
database
Database
ALTERIN
Schema
Table
ALTER TABLE
Table
Global
CREATE_EXTERNAL_ROUTINE
Database
Database
CREATIN
Schema
Global
CREATETAB
Database
Database
CREATIN
Schema
ALTER
ALTER
ROUTINE
CREATE
189
Table
CONTROL
Table
Global
CREATE_EXTERNAL_ROUTINE
Database
Database
CREATIN
Schema
Global
CREATETAB
Database
Database
CREATIN
Schema
CREATE
USER
Global
SECADMIN authority
Database
CREATE
VIEW
Global
DATAACCESS authority
Database
Database
CONTROL or SELECT
Table
CONTROL or SELECT
Table
Global
DATAACCESS authority
Database
Database
DELETE
Table
DELETE
Table
Global
DROPIN
For each
schema in the
database
Database
DROPIN
Schema
Table
CONTROL
Table
Global
DBADM authority
Database
Database
DBADM authority
Database
Global
DATAACCESS authority
Database
Database
EXECUTE
Routine
EXECUTE
Routine
FILE
Global
LOAD authority
Database
GRANT
OPTION
Global
SECADMIN authority or
DBADM WITH ACCESSCTRL
Database
Database
SECADMIN authority or
DBADM WITH ACCESSCTRL
Database
CREATE
ROUTINE
CREATE
TEMPORARY
TABLES
DELETE
DROP
EVENT
EXECUTE
190
Table
SECADMIN authority or
DBADM WITH ACCESSCTRL
or CONTROL
Database/Table
Routine
SECADMIN authority or
DBADM WITH ACCESSCTRL
or CONTROL
Routine
Global
CREATETAB
Database
Database
CREATIN
Schema
Table
INDEX
Table
Global
DATAACCESS authority
Database
Database
INSERT
Table
INSERT
Table
Column
LBAC
Column
PROCESS
Global
SYSADM, SYSCTRL, or
SYSMAINT authority
Instance
RELOAD
Global
WLMADM
Database
REPLICATION
CLIENT
Global
DBADM authority
Database
REPLICATION
SLAVE
Global
DBADM authority
Database
SELECT
Global
DATAACCESS authority
Database
Database
SELECT
Table
SELECT
Table
Column
LBAC
Column
SHOW
DATABASES
Global
ALL users
All users
SHOW VIEW
Global
SELECT
On
SYSCAT.VIEWS
Database
SELECT
On
SYSCAT.VIEWS
Table
SELECT
On
SYSCAT.VIEWS
Global
SYSADMN, SYSCTRL, or
SYSMAINT authority
Instance
INDEX
INSERT
SHUTDOWN
191
SUPER
Global
Not available
TRIGGER
Global
DBADM authority
Database
Database
ALTERIN
Schema
Table
CONTROL
Table
Global
DATAACCESS authority
Database
Database
UPDATE
Table
UPDATE
Table
Column
LBAC
Column
Global
Database
Table
UPDATE
USAGE
192
193
The root user creates our user and group, as shown in Example 7-13.
Example 7-13 Creation of the user and group
db2server:~ # ./db2addusr.sh inventAppUser db2app1
Changing password for inventAppUser.
New password:
Re-enter new password:
Password changed
The instance owner, db2inst1, grants the privileges with the DB2 command that
is shown in Example 7-14.
Example 7-14 Granting privileges
db2 => connect to invent
connect to invent
Database Connection Information
Database server
SQL authorization ID
Local database alias
= DB2/LINUX 9.7.0
= DB2INST1
= INVENT
194
DB20000I
to user inventAppUSer
to user inventAppUSer
to user inventAppUSer
to user inventAppUSer
to user inventAppUSer
195
Now, you can go into your project directory to check the extracted data files. The
data output files extracted from the MySQL database are located under the
<migration output directory>/data directory. Example 7-15 shows the created
scripts for our inventory scenario.
Example 7-15 IBM Data Movement Tool export files
db2server:/opt/ibm/IBMDataMovementTool/migr/data # ls -l
-rw-r--r-- 1 db2inst1 db2iadm1
114 Aug 28 01:03 admin_status.txt
-rw-r--r-- 1 db2inst1 db2iadm1
82 Aug 28 01:03 admin_severity.txt
-rw-r--r-- 1 db2inst1 db2iadm1 43188 Aug 28 01:03 admin_services.txt
-rw-r--r-- 1 db2inst1 db2iadm1 60137 Aug 28 01:03 admin_owners.txt
-rw-r--r-- 1 db2inst1 db2iadm1 4173 Aug 28 01:03 admin_locations.txt
-rw-r--r-- 1 db2inst1 db2iadm1 45489 Aug 28 01:03 admin_inventory.txt
-rw-r--r-- 1 db2inst1 db2iadm1
143 Aug 28 01:03 admin_groups.txt
Each of the files is tab-delimited, containing the data from the corresponding
MySQL table. This format can be read by the DB2 LOAD command.
196
--#SET :LOAD:ADMIN:LOCATIONS
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_locations.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_locations.txt"
METHOD P(1,2,3,4)MESSAGES "/opt/ibm/IBMDataMovementTool/migr/msg/admin_locations.txt"
REPLACE INTO "ADMIN"."LOCATIONS" ("ID", "ROOMNAME", "FLOORNUM", "PASSCODE")
--STATISTICS YES WITH DISTRIBUTION AND DETAILED INDEXES ALL NONRECOVERABLE
INDEXING MODE AUTOSELECT ;
--#SET :LOAD:ADMIN:SEVERITY
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_severity.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_severity.txt"
METHOD P (1,2,3,4,5) MESSAGES "/opt/ibm/IBMDataMovementTool/migr/msg/admin_severity.txt"
REPLACE INTO "ADMIN"."SEVERITY" ("ID", "TITLE", "NOTES", "ESTDAYS", "AVGDAYS"
)--STATISTICS YES WITH DISTRIBUTION AND DETAILED INDEXES ALL NONRECOVERABLE INDEXING
MODE AUTOSELECT;
197
--#SET :LOAD:ADMIN:OWNERS
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_owners.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_owners.txt"
METHOD P (1,2,3,4,5,6,7,8,9,10,11) MESSAGES
"/opt/ibm/IBMDataMovementTool/migr/msg/admin_owners.txt" REPLACE INTO "ADMIN"."OWNERS"
("ID", "FIRSTNAME", "LASTNAME", "EMAIL", "LOCID", "CUBENUM",
"PHONENUM", "LOGINNAME", "PASSWORD", "FAXNUM", "GROUPS" ) --STATISTICS YES WITH
DISTRIBUTION AND DETAILED INDEXES ALL NONRECOVERABLE INDEXING MODE AUTOSELECT;
--#SET :LOAD:ADMIN:INVENTORY
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_inventory.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_inventory.txt"
METHOD P (1,2,3,4,5,6,7,8) MESSAGES
"/opt/ibm/IBMDataMovementTool/migr/msg/admin_inventory.txt"
REPLACE INTO "ADMIN"."INVENTORY"("ID", "ITEMNAME", "MANUFACTURER", "MODEL", "YEAR",
"SERIAL", "LOCID", "OWNERID")--STATISTICS YES WITH DISTRIBUTION AND DETAILED INDEXES ALL
NONRECOVERABLE INDEXING MODE AUTOSELECT ;
--#SET :LOAD:ADMIN:SERVICES
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_services.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_services.txt"
METHOD P (1,2,3,4,5,6,7,8,9) MESSAGES
/opt/ibm/IBMDataMovementTool/migr/msg/admin_services.txt" REPLACE INTO
"ADMIN"."SERVICES" ("ID", "INVENTID", "DESCRIPTION", "SEVERITY", "SERVICEOWNER",
"OPENDATE", "CLOSEDATE", "TARGETCLOSEDATE", "STATUS" )--STATISTICS YES WITH DISTRIBUTION
AND DETAILED INDEXES ALL NONRECOVERABLE INDEXING MODE AUTOSELECT;
--#SET :LOAD:ADMIN:STATUS
LOAD FROM
"/opt/ibm/IBMDataMovementTool/migr/data/admin_status.txt" OF DEL
MODIFIED BY CODEPAGE=1208 COLDEL~ ANYORDER USEDEFAULTS CHARDEL"" DELPRIORITYCHAR
NOROWWARNINGS --DUMPFILE="/opt/ibm/IBMDataMovementTool/migr/dump/admin_status.txt"
METHOD P (1,2,3) MESSAGES "/opt/ibm/IBMDataMovementTool/migr/msg/admin_status.txt"
REPLACE INTO "ADMIN"."STATUS" ("ID", "TITLE", "NOTES" )--STATISTICS YES WITH
DISTRIBUTION AND DETAILED INDEXES ALL NONRECOVERABLE INDEXING MODE AUTOSELECT;
TERMINATE;
198
also execute this load from the command line by running the db2load.sh script. If
you made changes to the extraction script, be sure to select No when requested
to recreate the output directory or <tableName>.tables file, because it overrides
your changes.
After importing the data into the DB2 tables, execute the RUNSTATS command
to recreate the statistics information about indexes. The query optimizer uses
this statistics information. The IBM Data Movement Tool generates a custom
RUNSTATS script for the new database, which is called db2runstats.sql.
Example 7-17 shows the db2runstats.sql script for our sample conversion. You
can run this script in the IBM Data Movement Tool GUI or command line.
Example 7-17 DB2 RUNSTATS commands for recreating the statistics information
CONNECT TO INVENT;
RUNSTATS ON TABLE "ADMIN"."GROUPS" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."LOCATIONS" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."SEVERITY" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."OWNERS" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."INVENTORY" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."SERVICES" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
RUNSTATS ON TABLE "ADMIN"."STATUS" ON ALL COLUMNS WITH DISTRIBUTION
ON ALL COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS ;
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
loaded
rejected
deleted
committed
=
=
=
=
=
=
140
0
140
0
0
140
199
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
loaded
rejected
deleted
committed
=
=
=
=
=
=
5
0
5
0
0
5
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
loaded
rejected
deleted
committed
=
=
=
=
=
=
504
0
502
2
0
504
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
loaded
rejected
deleted
committed
=
=
=
=
=
=
703
0
703
0
0
703
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
loaded
rejected
deleted
committed
=
=
=
=
=
=
808
0
808
0
0
808
200
= 7
= 0
= 7
= 0
= 0
= 7
TERMINATE
DB20000I The TERMINATE command completed successfully.
= DB2/LINUX 9.7.0
= DB2INST1
= INVENT
CONNECT TO INVENT
Database Connection Information
Database server
SQL authorization ID
Local database alias
= DB2/LINUX 9.7.0
= DB2INST1
= INVENT
201
1 record(s) selected.
TERMINATE
DB20000I The TERMINATE command completed successfully.
= DB2/LINUX 9.7.0
= DB2INST1
= INVENT
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.GROUPS
Y
Y
N
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.LOCATIONS
Y
Y
N
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.SEVERITY
Y
Y
N
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.OWNERS
Y
Y
N
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- ------
202
ADMIN.INVENTORY
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.SERVICES
Y
Y
N
1 record(s) selected.
TABLE_NAME
FK_CHECKED CC_CHECKED STATUS
-------------------------------------------------- ---------- ---------- -----ADMIN.STATUS
Y
Y
N
1 record(s) selected.
Make sure that the number of rows read equals the number of rows committed,
which also needs to equal the number of records in the MySQL source table.
Example 7-19 shows the MySQL command for the record count.
Example 7-19 Retrieving the number of records from MySQL
mysql> select count(*)
+----------+
| count(*) |
+----------+
|
6 |
+----------+
1 row in set (0.04 sec)
from
groups;
from
inventory;
from
locations;
from
owners;
from
services;
203
+----------+
|
808 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*)
+----------+
| count(*) |
+----------+
|
7 |
+----------+
1 row in set (0.00 sec)
from
status;
from
severity;
After you have checked that all the records were loaded into DB2, check the
sample data in each ported table. Pay attention to ensure that the values are
correct, especially if you have any time values or decimal values. Example 7-20
shows the table content checking.
Example 7-20 Sample data of MySQL data
mysql> select * from services where id = 20;
+----+----------+-------------+----------+--------------+------------+------------+----------------+--------+
| id | inventID | description | severity | serviceOwner | openDate
| closeDate |
targetCloseDate | status |
+----+----------+-------------+----------+--------------+------------+------------+----------------+--------+
| 20 |
36 | test
|
1 |
108 | 2009-01-18 | 2009-01-20 |
2009-01-19
|
7 |
+----+----------+-------------+----------+--------------+------------+------------+----------------+--------+
1 row in set (0.00 sec)
204
Chapter 8.
Application conversion
The task of converting an application, its databases, and the associated data
most often requires significant resources and commitment. Simultaneously to the
meticulous planning of the porting project as a whole, it is important to assess the
issues that might affect the highest level of resources. In many porting projects,
evaluating the issues that might affect your resources is part of application
porting. This chapter attempts to provide you with important considerations for
the following areas:
Differences in SQL Data Manipulation Language (DML), built-in functions,
and SQL semantics
Conversion of the application source, application programming interfaces
(APIs), and condition handling
Internals of the Database Management System that might affect the
conversion, such as locking, isolation levels, transaction logging, and national
language support
205
The STRAIGHT_JOIN keyword forces the MySQL optimizer to join tables in the
order that is specified. In DB2, the join order is always determined by the
optimizer. The optimizer choices can be limited by changing the default query
optimization class to a lower level using SET CURRENT QUERY
OPTIMIZATION. However, changing this class does not guarantee that the
optimizer will evaluate the join in the order stated within the SQL statement,
because the DB2 cost-based optimizer usually chooses the best access path for
a given query. For additional information, see 10.5.5, SQL execution plan on
page 366.
206
Options prefixed with SQL are MySQL-specific and do not require a DB2
equivalent.
The DISTINCTROW keyword is a synonym for the DISTINCT clause that is
supported by DB2.
The INTO {OUTFILE | DUMPFILE} 'file_name' export_options selection allows
you to write output data to a file without invoking the mysqldump utility. The DB2
command line processor allows you to direct the output of any SELECT
statement to an operating system file.
The LIMIT [offset,] row_count | row_count OFFSET offset keyword translates to
FETCH FIRST n ROWS ONLY in DB2. Implement an offset to retrieve rows
through the WHERE clause if possible.
With the SQL_SMALL_RESULT or SQL_BIG_RESULT commands, the query
developer can hint to the SQL optimizer the size of the expected result set,
influencing the optimizer access strategy. Example 8-2 shows how the optimizer
hint works.
Example 8-2 MySQL SELECT with optimizer hint
mysql> SELECT sql_small_result * FROM t1;
DB2 has a similar operator to guide SQL optimizer decisions with a different
syntax, as shown in Example 8-3.
Example 8-3 DB2 SELECT with optimizer hint
db2 => SELECT * FROM t1 OPTIMIZE FOR 2 ROWS
DB20000I
The SQL command completed successfully.
207
The STRAIGHT_JOIN keywords force the MySQL optimizer to join tables in the
order that they are specified. In DB2, the join order is always determined by the
optimizer. You can limit the optimizer choices by changing the default query
optimization class by using the SET CURRENT QUERY OPTIMIZATION
command.
A NATURAL join, as its name implies, can be invoked when two or more tables
share exactly the same columns needed for a successful equijoin. It is
semantically equivalent to DB2 INNER JOIN or LEFT OUTER JOIN with the
respective join criteria specified in the ON clause.
According to the SQL ANSI standard when you must join tables that share more
than one column naturally, you must use the JOIN ... USING syntax. You can
compose an equivalent join using the DB2-supported join syntax in the ON
clause.
Cartesian products do happen from time to time, usually as the result of an
equijoin condition that has been missed in a query using DB2 syntax, even
though the developers of applications and database queries spend a significant
amount of time trying to avoid them. However, one of the advantages of the
CROSS JOIN syntax is that a specific keyword is required to create a Cartesian
product. Therefore, when the CROSS JOIN syntax is used in your conversion
project, you can code a regular join in DB2 with a no join condition in the WHERE
clause.
208
209
MYSQL example
DB2 function
DB2 example
Notes
AVG([DISTINCT]
expression)
mysql> SELECT a,
AVG(b)
FROM t1
GROUP BY a
AVG ([DISTINCT |
ALL] expression)
Returns the
average set of
numbers
COUNT([DISTINCT
]
expression,
expression,...)
mysql> SELECT a,
COUNT(b)
FROM t1
GROUP BY a
COUNT([DISTINCT|
ALL] expression).
Returns the
number of rows
or values in a set
of rows or values.
MAX ([DISTINCT]
expression)
mysql> SELECT a,
MAX(b)
FROM t1
GROUP BY a
MAX ([DISTINCT |
ALL] expression)
db2 "SELECT a,
MAX(b)
FROM t1
GROUP BY a"
Returns the
maximum value
in a set of values.
SUM([DISTINCT]
expression)
mysql> SELECT a,
SUM(b)
FROM t1
GROUP BY a
SUM([DISTINCT |
ALL] expression)
GROUP BY on alias
mysql> SELECT a as x
FROM a
GROUP BY x;
Groups data by
the alias name
provided.
GROUP BY on
position
mysql> SELECT a
FROM t1
GROUP BY 1
Groups data by
the position
provided.
HAVING on alias
mysql> SELECT a as x
FROM t1
GROUP BY a
HAVING x > 0
Groups data
meeting the
HAVING
expression.
8.1.6 Strings
Unless you start MySQL in ANSI mode (using mysqld --ansi), MySQL behaves
differently than DB2. As Example 8-7 on page 211 illustrates, MySQL accepts
single, as well as double quotation marks as a string delimiter when started in
default mode.
210
DB2 is designed and implemented according to the ANSI standard and therefore
accepts single quotation marks as a string delimiter. Double quotation marks are
used in DB2 for delimiting SQL identifiers. Example 8-8 shows how DB2 handles
strings. You achieve similar results when MySQL runs in ANSI mode.
Example 8-8 DB2 string handling
db2 => select "redbook", "'redbook'", "''redbook''", "red""book" from t1
SQL0206N "redbook" is not valid in the context where it is used.
SQLSTATE=42703
Table 8-2 provides an overview of a few of the MySQL string related functions,
and how these can be converted to DB2. For a full list of MySQL string functions
and the DB2 equivalent, refer to A.2, String functions on page 402.
Table 8-2 MySQL and DB2 string-related function
MySQL
function
MYSQL example
DB2 function
DB2 example
Notes
ASCII(string)
ASCII(string)
Returns ASCII
code value
1
----------97
1 record(s) selected
211
CHAR_LENG
TH(string) /
CHARACTE
R_LENGTH(
string)
CONCAT_W
S(separator,
string,
string,)
mysql> SELECT
CHAR_LENGTH('Orange');
+-----------------------+
| CHAR_LENGTH('Orange') |
+-----------------------+
|
6|
+-----------------------+
1 row in set (0.00 sec)
mysql> SELECT
CONCAT_WS('-', firstname,
lastname, loginname ) as
FULLNAME from owners
where id = 501;
+-------------------------+
| FULLNAME
|
+-------------------------+
| Angela-Carlson-acarlson |
+-------------------------+
1 row in set (0.01 sec)
CHARACTER
_LENGTH(stri
ng,
CODEUNITS1
6|
CODEUNITS3
2| OCTETS),/
CHAR_LENG
TH(string,
CODEUNITS1
6|
CODEUNITS3
2| OCTETS)
Use || to
implement
CONCAT(list)
1
----------6
1 record(s) selected.
Returns the
number of
bytes for
expression. For
double byte
character set
(DBCS) the
number of
DBCS characters
is
returned
Returns the
concatenation of
string arguments
with separator
FULLNAME
--------------------------------------------------------Angela-Carlson-acarlson
1 record(s) selected.
FORMAT(do
uble, integer)
FORMAT:
select
format(1234.5555, 2)
returns 1,234.56
No equivalent
function.
Implement
using UDF
Returns the
rounded string
INSERT(strin
g, position,
length,
substring)
mysql> SELECT
INSERT('original', 2, 2, 'NEW');
+---------------------------------+
| INSERT('original', 2, 2,
'NEW') |
+---------------------------------+
| oNEWginal
|
+---------------------------------+
1 row in set (0.00 sec)
INSERT(string
, position,
length,
substring)
db2 "VALUES
INSERT('original', 2, 2,
'NEW')"
1
----------oNEWginal
1 record(s) selected.
Inserts a string
into an existing
string
LPAD(string,
length,
substring) /
RDAP( string,
length,
substring)
mysql> SELECT
LPAD('TEST',6,'!!!');
+----------------------+
| LPAD('TEST',6,'!!!') |
+----------------------+
| !!TEST
|
+----------------------+
1 row in set (0.00 sec)
No equivalent
function.
Implement
using UDF
Returns the a
string of the given
length, if the
length is longer
then the string the
substring
characters will be
added to the left or
right end.
212
REPLACE(str
ing1, string2,
string3)
REPLACE(stri
ng1, string2,
string3)
Returns as sting
with all
occurrences of
string2 in string1
with string3
SUBSTRING
(string,
position,
length) / MID
(string,
position,
length) /
SUBSTR(stri
ng, position,
[length])
mysql> select
substring('abcdef', 2, 3);
+---------------------------+
| substring('abcdef', 2, 3) |
+---------------------------+
| bcd
|
+---------------------------+
1 row in set (0.00 sec)
SUBSTR(strin
g, position,
length)
db2 "VALUES('abcdef',
2, 3)
1
--bcd
Returns a
substring of a
string.
TRIM([Both |
Leading |
trailing
[substring]
FROM]
string)
TRIM([Both |
Leading |
trailing
[substring]
FROM] string)
1 record(s) selected.
db2 "VALUES
trim(trailing from
trim(LEADING FROM '
abc '))"
OUTPUT
--------abc
Removes blanks
or occurrences of
another specified
character from the
end or the
beginning of a
string expression
1 record(s) selected.
213
Implicit casting reduces the number of SQL statements that you must modify
when enabling applications that run on data servers other than DB2 data servers
to run on DB2 9.7. In many cases, you no longer have to explicitly cast data
types when comparing or assigning values with mismatched data types.
Example 8-9 shows how MySQL implicitly casts the character value 5 to an
integer value to resolve the query.
Example 8-9 MySQL performs implicit data type casting
mysql> create table t1 (c1 int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into t1 values(5);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t1 where c1='5';
+------+
| c1
|
+------+
| 5 |
+------+
1 row in set (0.00 sec)
As of DB2 9.7, DB2 now supports an implicit casting of incompatible data types.
Example 8-10 shows implicit casting of a character value in DB2 9.7.
Example 8-10 DB2 9.7 implicit data type casting
db2 => create table t1 (c1 int)
DB20000I The SQL command completed successfully.
db2 => insert into t1 values(5)
DB20000I The SQL command completed successfully.
db2 => commit
DB20000I The SQL command completed successfully.
db2 => select * from t1 where c1='5'
C1
----------5
1 record(s) selected.
DB2 versions prior to 9.7 require explicit casting of the character value to an
integer value, as illustrated in Example 8-11 on page 215.
214
Example 8-11 DB2 9.5 and prior versions require explicit casting
db2 => select * from t1 where c1 = '5'
SQL0401N The data types of the operands for the operation "=" are not
compatible. SQLSTATE=42818
db2 => select * from t1 where c1 = cast ('5' as int)
C1
-------5
1 record(s) selected.
Example 8-12 illustrates how MySQL implicitly casts numeric values and DATE,
TIME, or TIMESTAMP values to strings when concatenated.
Example 8-12 MySQL implicit casting using concatenation for strings and DATE
mysql> select concat('ITSOSJ',1234) from t1;
+-----------+
| stringcol |
+-----------+
| ITSOSJ1234|
+-----------+
1 row in set (0.02 sec)
mysql> select concat('ITSOSJ',current_date) as stringdate from t1;
+------------------+
| stringdate
|
+------------------+
| ITSOSJ2009-08-31 |
+------------------+
1 row in set (0.01 sec)
Example 8-13 illustrates how DB2 9.7 implicitly casts numeric values, as well as
DATE, TIME, or TIMESTAMP values, to strings when concatenated.
Example 8-13 DB2 9.7 casting character strings and DATE implicitly
db2 => select concat('ITSOSJ',1234) from t1
1
---------ITSOSJ1234
1 record(s) selected.
db2 => select concat('ITSOSJ',current_date) as stringdate from t1
STRINGDATE
215
---------------ITSOSJ08/31/2009
1 record(s) selected.
DB2 9.5 and prior versions require compatible arguments for the concatenation
built-in functions, as shown in Example 8-14. If the arguments are incompatible,
for example, calling a function with a character data type argument using a
numeric data type, the concatenation will fail with the error SQL0440N No
authorized routine named "CONCAT" of type "FUNCTION" having compatible
arguments was found.
Example 8-14 DB2 9.5 and prior versions casting character strings and DATE explicitly
db2 => select concat('ITSOSJ',1234) from t1
SQL0440N
No authorized routine named "CONCAT" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884
db2 => select concat('ITSOSJ','1234') as stringcol from t1
STRINGCOL
----------ITSOSJ1234
1 record(s) selected.
db2 => select concat('ITSOSJ', current date) as stringdate from t1
SQL0440N
No authorized routine named "CONCAT" of type "FUNCTION" having
compatible arguments was found.
SQLSTATE=42884
db2 => select concat('ITSOSJ',CAST(current date as char(20))) as stringdate
from t1
STRINGDATE
-----------------------ITSOSJ01/23/2004
1 record(s) selected.
216
DB2 follows the ANSI92 standard for concatenation of multiple strings. DB2 also
has a CONCAT(string1, string2), which can be used for concatenation of two
strings. Example 8-16 shows how DB2 handles concatenating strings.
Example 8-16 DB2 concatenation of strings
db2 => VALUES CONCAT('This ', 'is ', 'an ', 'example.')
SQL0440N No authorized routine named "CONCAT" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884
db2 => VALUES CONCAT('This is ', 'an example.')
1
------------------This is an example.
1 record(s) selected.
db2 => VALUES ('This ' ||
'is ' ||
1
------------------This is an example.
1 record(s) selected.
The ANSI92 standard states that if you concatenate a NULL value onto an
existing string, the result set is NULL. Example 8-17 shows you the behavior of
MySQL.
Example 8-17 MySQL concatenation of strings and NULL values
mysql> create table t2 (col1 char(2));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t2 values(NULL);
Query OK, 1 row affected (0.07 sec)
mysql> select concat('abc',col1) as nullstring from t2;
+-------------------+
| nullstring
|
217
+-------------------+
| NULL
|
+-------------------+
1 row in set (0.05 sec)
mysql> select concat('abc', coalesce(col1,'')) as nullstring from t2;
+----------------------------------+
| nullstring
|
+----------------------------------+
| abc
|
+----------------------------------+ 1 row in set (0.00 sec)
As shown, MySQL behaves as ANSI-92 compliant and, therefore, gives you the
same result sets as Example 8-18 for DB2.
Example 8-18 DB2 string and NULL concatenation
db2 => create
DB20000I
The
db2 => insert
DB20000I
The
db2 => select
NULLSTRING
----1 record(s) selected.
db2 =>
NULLSTRING
----abc
1 record(s) selected.
218
(0.00 sec)
The TRUNCATE option is primarily used to delete all records quickly from a table
when no recovery of the deleted rows is required. As of DB2 9.5, you can enable
the support of the TRUNCATE statement using the
DB2_COMPATIBILITY_VECTOR registry variable.
The DB2_COMPATIBILITY_VECTOR registry variable is used to enable one or
more DB2 compatibility features introduced since DB2 Version 9.5.
These features ease the task of converting applications written for other
relational database vendors to DB2 Version 9.5 or later. This DB2 registry
variable is represented as a hexadecimal value, and each bit in the variable
enables one of the DB2 compatibility features. To enable the TRUNCATE
statement, set the DB2_COMPATIBILITY_VECTOR registry variable to 8.
Example 8-20 on page 220 shows the syntax to set the
DB2_COMPATIBILITY_VECTOR registry variable and execute the TRUNCATE
command. You can obtain more information about the
DB2_COMPATIBILITY_VECTOR registry variable at the IBM Information Center
at this Web site:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
219
= DB2/LINUX 9.7.0
= DB2INST1
= ISSODB
220
You can also turn off logging with the following ALTER TABLE statement to
achieve a similar result.
ALTER TABLE <tablename> ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE
221
You can also use the DBI interface to access DB2 using the DBD::DB2 driver.
You can obtain information about the DBI interface and the DBD::DB2 driver, as
well as installation instructions, at the following Web sites:
http://www.ibm.com/software/data/db2/perl/
http://search.cpan.org/~ibmtordb2/DBD-DB2-1.74/DB2.pod
http://www.perl.com/CPAN/modules/by-module/DBD/
http://aspn.activestate.com/ASPN/Modules/
Figure 8-1 illustrates how the Perl interfaces and pluggable drivers connect to
MySQL and DB2 databases.
222
Connecting to a database
Use the following code within your Perl application to connect to a MySQL
database:
$dsn= "dbi:mysql:$database:$host:$port";
$dbh = DBI ->Connect($dsn, $user, $password);
The connection statement consists of the data source name, user ID, and
password. The data source name consists of the vendor-specific database
driver, the database name, the host name, and the port. Optionally, you can omit
the host name and port from the data source name. Example 8-21 shows the
connect syntax to a MySQL database. For simplicity reasons, we do not include
the error handling in following examples.
Example 8-21 MySQL database connection with the Mysql.pm interface
use
use
my
my
my
my
my
DBI;
DBD::mysql;
$host="localhost";
$port="3306";
$database="inventory";
$user="mysql";
$password="password";
my $dns="DBI:mysql:database=$database;host=$host;port=$port";
$dbh =
DBI->connect("$dns","$user","$password");
DBI;
DBI;
DBD::DB2;
DBD::DB2::Constants;
$host="localhost";
$port="50005";
$database="invent";
223
my $user="db2inst1";
my $password="password";
my $dns = "dbi:DB2:DATABASE=$database; HOSTNAME=$host; PORT=$port;
PROTOCOL=TCPIP; UID=$user; PWD=$password;";
$dbh = DBI->connect("$dns","$user","$password");
Because DB2 is more powerful than MySQL, the DB2 connect statement might
require a fourth argument \%attr, which contains the connection attributes, as
shown in Example 8-23.
$dbh=DBI->connect($data_source,
$user,$password,
\%attr);
db2_info_acctstr
db2_info_applname
db2_info_programname
db2_info_userid
db2_info_wrkstnname
db2_longdata_compat
db2_quiet_mode
db2_sqlerrp
db2_txn_isolation
SQLL_MODE_READ_ONLY or SQL_MODE_READ_WRITE
Character string
SQL_CC_NO_RELEASE or SQL_CC_RELEASE
Integer (must be set in DBI->connect method;
it cannot be modified afterwards)
Character string
Integer
One of:
SQL_DB2EXPLAIN_OFF
SQL_DB2EXPLAIN_SNAPSHOT_ON SQL_DB2EXPLAIN_MODE_ON
SQL_DB2EXPLAIN_SNAPSHOT_MODE_ON
Character string
Character string
Character string
Character string
Character string
Boolean
Integer
Character string (read only)
One of the following:
SQL_TXN_READ_UNCOMMITTED
SQL_TXN_READ_COMMITTED
SQL_TXN_REPEATABLE_READ
SQL_TXN_SERIALIZABLE
SQL_TXN_NOCOMMIT
224
$dbh->do($sql_statement);
$username,
$password);
$dbh->disconnect;
225
226
Connecting to a database
Connecting to a MySQL database with the mysql extension consists of two parts.
First, establish a connection to the MySQL server, and then, choose a database.
The following declaration shows the function that is specified to connect the
MySQL server:
resource mysql_connect ( [string server [, string username [, string
password [, bool new_link [, int client_flags]]]]])
Example 8-26 shows the connection part of our sample application using the
MySQL database.
Example 8-26 Connecting a MySQL database with the mysql extension
$host="localhost";
$user="mysql";
$pwd="password";
$database="inventory";
$conn = mysql_connect($host, $user, $pwd)or die("Couldn't connect to server. "
. mysql_error() . "\n");
$dbConn = mysql_select_db($database, $conn)or die("Couldn't select database. "
. mysql_error() .
"\n");
227
Example 8-28 shows the converted connection part of the sample application. In
our connection script, the command after the connection statement sets the
current schema, which is used when querying the database. We use this
approach, because when compared to DB2, MySQL does not have schemas or
instances.
In MySQL, you can reference a table by database.tablename. When no database
name is provided in the table name, the default is the database currently in use.
In DB2, the table name is defined as schema.tablename. Referencing a DB2
table, when no schema is provided, uses the default schema associated with the
user ID that is connected to the database. In an application, you can use the set
schema statement to provide a global schema name for the tables, which do not
have a full table name specified. To simplify application conversion, when porting
the database, create the tables under the admin schema. By using the set
schema after the database connection, we do not need to change every table
name in the application.
Example 8-28 Connect to a DB2 database
$database = 'invent';
$user = 'db2inst1';
$password = 'password';
$conn = db2_connect($database, $user, $password) or die("Couldn't connect to
server. " . db2_conn_errormsg() . "\n");
$sql="SET CURRENT SCHEMA='ADMIN'";
$sqlResult = db2_exec($conn, $sql) or die ("Couldn't execute query.... " . $sql
. "\n" );
228
Note: The default subsystem name (DSN) is the database name, which is
registered in the DB2 catalog. Because this database is cataloged, you do not
have to declare any server information in the connect statement.
You can map the persistent connection functions mysql_pconnect() and
mysqli_pconnect() to the db2_pconnect() function.
229
You can map the mysqli_query() function directly to the db2_exec() function, and
the db2_exec() function nearly corresponds to the mysql_query() function. The
only difference between the two functions is that db2_exec() requires two
parameters. One parameter specifies the connection ID that is returned by the
db2_connect() statement, and the other parameter is the SQL statement:
resource db2_exec ( resource $connection , string $statement [, array
$options ] )
Example 8-32, Example 8-33, and Example 8-34 on page 231 show the
differences between the MySQL and ibm_db2 functions for the INSERT
statement.
Example 8-32 MySQL UPDATE statement using mysql
$updateCMD = "UPDATE services SET serviceOwner = " . $servOwner . " WHERE ID =
" . $srvID ;
$updateOutput = mysql_query($updateCMD) ;
if($updateOutput){
$textOutput = "Service Ticket updated. \n";
}else{
$textOutput = "Service Ticket updated Failed. \n";
}
echo $textOutput . "\n";
Example 8-33 MySQL UPDATE statement using mysqli
$updateCMD = "UPDATE services SET serviceOwner = " . $servOwner . " WHERE ID =
" . $srvID ;
$updateOutput = mysqli_query($conn, $updateCMD)
if($updateOutput){
$textOutput = "Service Ticket updated. \n";
}else{
$textOutput = "Service Ticket updated Failed. \n";
}
echo $textOutput . "\n";
230
231
These three functions execute the same task of disconnecting from the
database, and both DB2 and MySQL return true on success and false on failure.
In rare cases, the return value of the mysql_close() function is not used at all;
therefore, you can perform conversions mostly by simply replacing the function
or inserting the new function at the end of the program execution.
mysqli
ibm_db2
mysql_close
mysqli_close
db2_close
mysql_connect
mysqli_connect
db2_connect
mysql_pconnect
mysqli_pconnect
db2_pconnect
db2_pclose
mysql_query
mysqli_query
db2_exec
mysqli_stmt_prepare
db2_prepare
mysqli_stmt_execute
db2_execute
mysql_fetch_array
mysqli_fetch_array
db2_fetch_array
mysql_fetch_assoc
mysqli_fetch_assoc
db2_fetch_assoc
mysql_fetch_row
mysqli_fetch_row
db2_fetch_row
mysql_fetch_object
mysqli_fetch_object
db2_fetch_object
mysql_field_name
db2_field_name
mysql_errno
mysqli_connection_errno
db2_conn_error
mysql_error
mysqli_connection_error
db2_conn_errormsg
mysql_errno
mysqli_errno
db2_stmt_error
mysql_error
mysqli_error
db2_stmt_errormsg
mysql_affected_rows
mysqli_num_rows
db2_num_rows
232
Connecting to a database
Use this syntax for connecting to a database using PDO:
$conn = new PDO( string $dsn [, string $username [, string $password [,
array $driver_options ]]] )
Example 8-38 and Example 8-39 show the difference between the syntax to
connect to a MySQL and a DB2 database using PDO.
Example 8-38 Connecting to a MySQL database
$database = 'invent';
$user = 'db2inst1';
$password = 'password';
$conn = new PDO("mysql:host=$hostname;dbname=$database", $user, $password)
or die("Could not connect " . errorCode());
Example 8-39 Connecting to a DB2 database
$database = 'invent';
$user = 'db2inst1';
$password = 'password';
$conn = new PDO("odbc:$database", $user, $password)
or die("Could not connect " . errorCode());
$query="SET CURRENT SCHEMA='ADMIN'";
$output = $conn->exec($query);
233
When we discuss ODBC in this section (which is always Unified ODBC), we refer
to the native DB2 driver. Wide similarities between the syntax of Unified ODBC
and other ODBC types and the performance advantages when using Unified
ODBC support are the reasons that we discuss the application conversion with
Unified ODBC support.
Connecting to a database
When connecting to a DB2 database with ODBC, you connect by using a single
ODBC command (odbc_connect()):
resource odbc_connect ( string dsn, string user, string password [, int
cursor_type])
Example 8-42 on page 234 shows how to connect to our sample application
database using the ODBC command.
Example 8-42 Connecting to a DB2 database
$database = 'invent';
$user = 'db2inst1';
234
$password = 'password';
$conn = odbc_connect($database,$user,$password)
or die("Could not connect ". odbc_errormsg());
$query="SET CURRENT SCHEMA='ADMIN'";
odbc_exec($conn,$query) or die(odbc_errormsg($db));
To get each row in an array equal to the mysql_fetch_row() function, you can use
the odbc_fetch_into() function without extensive modifications. There are only
syntax differences in both statements in our case.
Example 8-44 on page 236 illustrates the ODBC functions for the UPDATE
statement. Refer to Example 8-32 on page 230 and Example 8-33 on page 230
for the MySQL UPDATE statements.
235
236
237
Connecting to a database
Connecting to a MySQL database with the MySQL/Ruby API consists of two
parts. First, establish a connection to the MySQL server using the connect()
function, and then, choose a database using the select_db() function.
Example 8-46 on page 238 illustrates the functions that are used to connect to a
MySQL database from Ruby.
Example 8-46 Connecting a MySQL database from Ruby
require 'mysql'
conn = Mysql.init()
conn.connect('localhost', 'password')
conn.select_db('test')
You can create a connection to a DB2 database by using a single function, the
connect() function. Use this syntax for the connect function:
resource IBM_DB::connect ( string database, string username, string
password [, array options] )
238
You can map the MySQL query() function directly to the DB2 exec() function.
Both functions have similar functionality. The only difference between the two
functions is that exec() requires two parameters: one parameter specifies the
connection ID that is returned by the connect() statement, and the other
parameter is the SQL statement:
resource IBM_DB::exec ( resource connection, string statement [, array
options] )
To get each row in an array equal to the MySQL/Ruby fetch_row() function, you
can use the IBM_DB fetch_array() function without extensive modifications.
conn.close()
Example 8-51 Disconnecting from a Db2 database from Ruby
IBM_DB.close(conn)
For more information about developing DB2 applications with Ruby on Rails,
review the Developing Perl, PHP, Python, and Ruby on Rails Applications
Manual, which is available at this Web site:
http://www.ibm.com/support/docview.wss?rs=71&uid=swg27015148
239
240
In order to use the DB2 JDBC type 2 driver, you need following properties:
drivername="COM.ibm.db2.jdbc.app.DB2Driver"
URL="java:db2:dbname"
The user ID and password are implicitly selected according to the DB2 client
setup.
Note: The DB2 JDBC Type 2 Driver for Linux, UNIX, and Windows will not
be supported in future releases. Consider switching to the IBM Data Server
Driver for JDBC and SQLJ, which we describe next.
IBM DB2 Driver for JDBC and SQLJ
The IBM DB2 Driver for JDBC and SQLJ is a single driver that includes both
Type 2 and Type 4 connectivity. This type 4/Native-protocol all-Java driver is
implemented in Java, and it uses the Distributed Relational Database
Architecture (DRDA) protocol for client/server communications.
Figure 8-3 on page 242 shows the JDBC Driver usage in a Java application.
241
DB2 Universal JDBC Driver does not require any service on the client side.
You can find it in the db2jcc.jar package.
To use the DB2 Universal JDBC driver, you need the following properties:
drivername="com.ibm.db2.jcc.DB2Driver"
URL="java:db2://servername:serverport/dbname"
242
In this section, we provide you with information about Java program conversion
from MySQL to DB2.
Connecting to a database
In this part, the Java program tries to establish a connection to the given
database by calling the function DriverManager.getConnection with the proper
URL values as discussed within the driver description in IBM JDBC driver for
DB2 on page 241. After this call, DriverManager selects the appropriate driver
from a set of registered drivers to connect to the database. Example 8-52 and
Example 8-53 show these steps for MySQL and then DB2.
Example 8-52 MySQL JDBC driver loading and connection
import java.sql.*;
public class inventSample {
public static void main(String[] args) throws Exception {
try {
//---------- Load the driver ---------//
Class.forName("com.mysql.jdbc.Driver");
//---------- Connect to database ---------//
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost/inventory", "mysql", "password");
//...
}
}
Example 8-53 DB2 JDBC driver loading and connection
import java.sql.*;
public class inventSample {
public static void main(String[] args) throws Exception {
try {
//---------- Load the driver ---------//
Class.forName("com.ibm.db2.jcc.DB2Driver");
try {
//---------- Connect to database ---------//
243
244
245
application and the database using the specified schema mapping, which is
defined by both DB2 and MySQL for their data types.
Because MySQL does not enforce strict type conversions, the Java programmer
has to take care of data lost because of round-off, overflow, or precision loss. For
more details about how MySQL is mapped to Java data types, refer to the
information at this Web site:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-type-conversions.h
tml
246
Java type
SMALLINT
Short
INTEGER
Int
BIGINT
Long
REAL
Float
DOUBLE
Double
DECIMAL
java.math.BigDecimal
Packed decimal
CHAR
java.lang.String
Fixed-length character
string of length n where n is
from 1 - 254
byte[]
Fixed-length character
string of length n where n is
from 1 - 254
VARCHAR
java.lang.String
Variable-length character
string
byte[]
Variable-length character
string
LONG VARCHAR
java.lang.String
Long variable-length
character string
byte[]
Long variable-length
character string
BLOB
java.sql.Blob
Large object
variable-length binary
string
CLOB
java.sql.Clob
Large object
variable-length character
string
DBCLOB(n)
java.sql.Clob
Large object
variable-length
double-byte character
string
GRAPHIC
java.lang.String
Fixed-length double-byte
character string
VARGRAPHIC
java.lang.String
Non-null-terminating
varying double-byte
character string with 2-byte
string length indicator
LONG VARGRAPHIC
java.lang.String
Non-null-terminating
varying double-byte
character string with 2-byte
string length indicator
Java type
DATE
java.sql.Date
TIME
java.sql.Time
TIMESTAMP
java.sql.Timestamp
247
Converting applications
MySQL C API and DB2 CLI are similar in functionality and mechanisms to
access databases. Both use the function call to pass dynamic SQL statements
and do not need to be precompiled. We recommend converting MySQL C
applications to DB2 CLI. This section describes conversion changes for various
levels of the application:
Connecting to the server
The first step in converting MySQL C applications is to change the include
information, initialize variables, and replace the MySQL connection with a DB2
connection. Example 8-56 shows a typical MySQL C program to initiate MySQL
variables, create a connection, and terminate the connection.
Example 8-56 MySQL C application, initialize MySQL and create connection
#include <mysql/mysql.h>
#include <stdio.h>
int main(){
MYSQL *conn; /* pointer to connection handler
connection = mysql_init(NULL);
*/
if(!mysql_real_connect (
conn, /* pointer to connection handler */
NULL, /* host to connect, default localhost*/
"mysql", /* user name, default local user*/
"password", /* password, default none*/
"inventory", /* database name*/
0, /* port */
NULL, /* socket */
0
/* flags*/
248
) )
{
printf("Connection Failed \n");
fprintf(stderr, "%s\n", mysql_error(conn));
} else{
printf("Successful connection to database.\n");
}
}
mysql_close(conn);
}
Figure 8-4 shows a similar task using DB2 CLI. It shows the initialization tasks,
which consist of: allocation and initializing the environment and connection
handlers; creating the connection; processing of transactions; and terminating
the connection and removing of handlers.
Example 8-57 shows the implementation of the task defined by the figure above.
Example 8-57 DB2 CLI application, connecting to a database
#include <sqlcli.h>
{
SQLRETURN ret =
SQLHANDLE henv;
SQLHANDLE hdbc;
*/ int main()
SQL_SUCCESS; int rc = 0;
/* environment handle */
/* connection handle */
249
*/
ret = SQLAllocHandle(SQL_HANDLE_DBC,
if (ret != SQL_SUCCESS){
/* handle error */
return 1;
}
henv, &hdbc);
*/
Processing a query
A typical MySQL C API program involves three steps in query processing:
Query construction
250
Depending upon your requirement you can construct a null terminated string
or counted length string for the query:
char *query;
On the other hand DB2 CLI provides a more comprehensive set of APIs for doing
similar tasks. One of the essential parts of DB2 CLI is transaction processing,
251
which is supported by all the tables in DB2. Figure 8-5 on page 252 shows the
typical order of function calls of query processing.
252
*/
*/
253
if(ret == SQL_NO_DATA_FOUND){
printf("No data found");
}
while(ret != SQL_NO_DATA_FOUND){
printf("First name: %s \n",firstName);
printf("Last name: %s \n",lastName);
printf("email: %s \n", email);
ret=SQLFetch(hstmt);
}
ret = SQLFreeStmt(hstmt, SQL_UNBIND);
if (ret != SQL_SUCCESS) {
/* handle error */
}
254
Example 8-60 DB2 CLI prepare/execute in one step with SQLGetData and manual commit
SQLHANDLE hstmt; /* statement handle
SQLCHAR firstName [TEXT_SIZE];
SQLCHAR lastName [TEXT_SIZE];
SQLCHAR email [TEXT_SIZE];
*/
FROM admin.owners
/* set AUTOCOMMIT on */
ret = SQLSetConnectAttr(hdbc,
SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, SQL_NTS);
if (ret != SQL_SUCCESS) {
/* handle error */
}
/* allocate a statement handle */
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
if (ret != SQL_SUCCESS) {
/* handle error */
}
/* execute statement 1 directly */
ret = SQLExecDirect(hstmt, stmt1, SQL_NTS);
if (ret != SQL_SUCCESS) {
if(ret == SQL_NO_DATA_FOUND) {
printf("No data found");
}
int count = 0;
while(ret != SQL_NO_DATA_FOUND) {
/* get data from column 1 */
ret = SQLGetData(hstmt, 1, SQL_C_CHAR, (SQLPOINTER)firstName, sizeof(firstName) + 1,
NULL);
count = count + 1;
printf("COUNT %i \n", count);
printf("First Name: %s \n", firstName);
if (ret != SQL_SUCCESS) {
/* handle error */
}
/* get data from column
2 */
255
);
256
The conversion is easy, because DB2 CLI is also based on the ODBC
specification and you can build ODBC applications without using any ODBC
driver manager.
You only have to use DB2 ODBC driver to link to your application with libdb2.
The DB2 CLI driver also acts as an ODBC driver when loaded by an ODBC
driver manager. DB2 CLI conforms to ODBC 3.51.
Figure 8-6 shows the MySQL driver and DB2 ODBC driver in the ODBC
scenario. Figure 8-6 shows the simplicity of converting an application using an
ODBC driver to another driver. It also shows various components that are
involved in the ODBC application and how they map from MySQL
Connector/ODBC to DB2 ODBC.
Figure 8-6 ODBC application conversion from MyODBC to DB2 ODBC driver
257
The IBM Data Server CLI and ODBC Driver does not come with the ODBC
driver manager. When using an ODBC application, you must ensure that an
ODBC driver manager is installed and that users using ODBC have access to
it. The following ODBC driver managers can be configured to work with the
IBM Data Server CLI and ODBC Driver:
unixODBC driver manager
The unixODBC driver manager is an open source ODBC driver manager
supported for DB2 ODBC applications on all supported Linux and UNIX
operating systems.
Microsoft ODBC driver manager
You can use the Microsoft ODBC driver manager for connections to
remote DB2 databases when using a TCP/IP network.
DataDirect ODBC driver manager
You can use the DataDirect ODBC driver manager for DB2 for
connections to the DB2 database.
Connector/ODBC
As shown in Figure 8-6 on page 257, you are not required to use
Connector/ODBC now; instead, you use the DB2 ODBC driver.
ODBC configuration
The ODBC Driver Manager uses two initialization files:
The /etc/unixODBC/odbcinst.ini file, in which you must add the
following lines:
[IBM DB2 ODBC DRIVER]
Driver=/home/<instance name>/sqllib/lib/db2.o
MySQL server
The MySQL database server is replaced by the DB2 server, which has been
discussed in detail in previous chapters.
You can optionally configure the DB2 ODBC Driver to modify the behavior of the
DB2 ODBC Driver by changing the db2cli.ini file.
258
For example, if your application signals SQLSTATE 23000, the DB2 description
reports an integrity constraint violation, which is similar to MySQLs rudimentary
description ER NON UNIQ ERROR or ER DUP KEY. Hence, condition handling for both
database management systems can almost execute the same code.
259
1;
260
*/
*/
printf("%s\n", message);
i++;
}
printf("-------------------------\n");
}
Note: We provide the code snippets in this chapter for illustrative purposes
only. The utilcli.c code is sample code that is included with DB2, which you
can find in the SQLLIB/samples directory.
For exception handling in Java, it is important to know that DB2 provides several
types of JDBC drivers with slightly different characteristics. With the DB2
Universal JDBC Driver, you can retrieve the SQLCA. For the DB2 JDBC type 2
driver for Linux, UNIX, and Windows (DB2 JDBC type 2 driver), use the standard
SQLException to retrieve SQL error information.
SQLException under the IBM Data Server Driver for JDBC and
SQLJ
As in all Java programs, error handling is done using try/catch blocks. Methods
throw exceptions when an error occurs, and the code in the catch block handles
those exceptions.
261
JDBC provides the SQLException class for handling errors. All JDBC methods
throw an instance of SQLException when an error occurs during their execution.
According to the JDBC specification, an SQLException object contains the
following information:
IBM DB2 Driver for JDBC and SQLJ provides an extension to the SQLException
class, which gives you more information about errors that occur when DB2 is
accessed. If the JDBC driver detects an error, the SQLException class provides
you with the same information as the standard SQLException class. However, if
DB2 detects the error, the SQLException class provides you the standard
information, along with the contents of the SQLCA that DB2 returns. If you plan
to run your JDBC applications only on a system that uses the IBM DB2 Driver for
JDBC and SQLJ, you can use this extended SQLException class.
Under the IBM DB2 Driver for JDBC and SQLJ, SQLExceptions from DB2
implement the com.ibm.db2.jcc.DB2Diagnosable interface. An SQLException
from DB2 contains the following information:
A java.lang.Throwable object that caused the SQLException or null if no such
object exists. The java.lang.Throwable class is the superclass of all errors
and exceptions in the Java language.
The information that is provided by a standard SQLException
An object of DB2-defined type DB2Sqlca that contains the SQLCA. This
object contains the following objects:
262
3. After you have DB2Sqlca, use it to get SQLCODE, messages, SQL errors,
and warnings, as shown in Example 8-63.
Example 8-63 Processing an SQLException under the Universal JDBC driver
import java.sql.*;
import com.ibm.db2.jcc.DB2Diagnosable;
import com.ibm.db2.jcc.DB2Sqlca;
try {
// Code that could throw SQLExceptions }
.
catch(SQLException sqle) {
while(sqle != null) {
if (sqle instanceof DB2Diagnosable) {
DB2Sqlca sqlca = ((DB2Diagnosable)sqle).getSqlca();
if (sqlca != null) {
System.err.println ("SqlCode: " + sqlca.getSqlCode());
System.err.println ("SQLERRMC: " + sqlca.getSqlErrmc());
System.err.println ("SQLERRP: " + sqlca.getSqlErrp() );
String[] sqlErrmcTokens = sqlca.getSqlErrmcTokens();
for (int i=0; i< sqlErrmcTokens.length; i++) {
System.err.println (" token " + i + ": " + sqlErrmcTokens[i]);
}
int[] sqlErrd = sqlca.getSqlErrd();
char[] sqlWarn = sqlca.getSqlWarn();
System.err.println ("SQLSTATE: " + sqlca.getSqlState());
System.err.println ("message: " + sqlca.getMessage());
}
}
sqle=sqle.getNextException();
}
}
WHENEVER
WHENEVER
WHENEVER
SQLERROR
SQLWARNING
NOT FOUND
action
action
action
263
264
When DB2 raises a condition that matches a condition, DB2 passes control to
the condition handler. The condition handler performs the action that is indicated
by the handler-type and then executes the SQL-procedure-statement.
DB2 provides three general conditions:
NOT FOUND:
This condition identifies a condition resulting in an SQLCODE of +100 or an
SQLSTATE beginning with the characters '02'.
SQLEXCEPTION:
This condition identifies any condition that results in a negative SQLCODE.
SQLWARNING:
This condition identifies any condition that results in a warning condition
(SQLWARN0 is 'W') or that results in a positive SQL return code other than
+100. The corresponding SQLSTATE value will begin with the characters
'01'.
You can also use the DECLARE statement to define your own condition for a
specific SQLSTATE.
Example 8-64 on page 266 shows the general flow of the condition handler in a
stored procedure.
265
Example 8-65 shows a CONTINUE handler for delete and update operations on
a table named EMP. Again, note that this code is solely intended for illustrative
purposes.
Example 8-65 Example of a DB2 CONTINUE handler
CREATE PROCEDURE PROC1()
LANGUAGE SQL
BEGIN
DECLARE SQLCODE, v_error INT;
DECLARE CONTINUE HANDLER FOR
SQLEXCEPTION,
SET v_error = SQLCODE;
DELETE FROM emp
WHERE empno BETWEEN 100 and 200;
IF (v_error = -147 ) THEN
INSERT . . .
UPDATE staff SET salary = salary * 1.25;
IF (v_error <> 0 ) THEN
RETURN -1;
END IF;
END
266
This information has to be ported into a DB2 table. When a user attempts to
access the data in the DB2 database, the application will verify each users
database access rights, along with the host system information for the host from
which that user connects.
We need two tables for our DB2 conversion: one table to store user privilege
information ported from MySQL and one working table. The table definitions and
sample values are shown in Example 8-67.
Example 8-67 Creation of the tables for host authentication
-- script for creating the tables used by our example application
-- connect to the database
connect to invent user db2inst1 using password;
-- table ACCESSLIST
-- it stores access rights for specific users connecting from specific hosts
-- remark: there should be different access-flags for different functions
------
fields:
username, whom access to the function should be granted
hostname or ip-address, from which the user must connect
select access flag (Y/N), if SELECT is granted
insert access flag (Y/N), if INSERT is granted
267
HOSTvarchar(30),
ACCESS_SELchar(1),
ACCESS_INSchar(1)
);
-- insert some sample values, according to the MySQL values (see above)
insert into ACCESSLIST values('user01', '%', 'Y', 'N');
insert into ACCESSLIST values('user02', '%.ibm.com', 'N', 'Y');
insert into ACCESSLIST values('inventAppUser', 'localhost', 'Y', 'Y');
insert into ACCESSLIST values('inventAppUser', '%.ibm.com', 'Y', 'Y');
-- table APPLACCESS
-- it stores the info about users and their host asking for access
-- this table is filled automatically by the sample application
------
fields:
username, who asks for access to the function
hostname, from which the user connects
ip-address, from which the user connects
timestamp, when the user asks for access
268
269
270
is stored in a table, the SQL LIKE function can be used in the next step. The
LIKE function handles wildcards (% and _) in the host information
correctly.
The third step is to verify if the host name or IP address has access rights by
comparing the entry with the host name that was retrieved in the first step.
If access is allowed, the function is executed. You can also implement a
method that has a return code stating whether access is allowed.
271
272
Description
Uncommitted Read
Cursor Stability
Read Stability
Repeatable Read
Note: Only committed data is returned for the cursor stability isolation level,
which is the currently committed semantics introduced in Version 9.7. Only
committed data was returned in previous releases, but now readers do not
wait for updaters to release the row locks. Instead, readers return data that is
based on the currently committed version, that is, data prior to the start of the
write operation.
273
The isolation levels that are listed in Table 8-5 on page 273 are ordered
descendent according to the number and duration of the locks that are held
during the transaction. Therefore, the degree of concurrency or locking is
required to ensure the desired level of data integrity. However, too much locking
drastically reduces concurrency. Poor application design and coding can cause
locking problems, such as:
Deadlocks
Lock waits
Lock escalation
Lock timeouts
By default, DB2 operates with the cursor stability isolation level. You can specify
transaction isolation at many levels, as we discussed in 8.3.5, Specifying the
isolation level in DB2 on page 276. For good performance, verify the lowest
isolation level required for your converted application.
For additional information about the DB2 concurrency implementation, refer to
the IBM DB2 Database for Linux, UNIX, and Windows Information Center at:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
8.3.4 Locking
Certain MySQL applications, when ported to DB2, appear to behave identically,
and you can ignore the topic of concurrency. However, if your applications
involve frequent access to the same tables, the applications might behave
differently. By default, MySQL runs in a mode that is called autocommit, which
means that MySQL considers each and every SQL statement as an atomic unit
of work or transaction.
In contrast, DB2, by default, considers a group of SQL statements with the
corresponding unit of work boundaries set by a commit, a rollback statement as a
single or atomic transaction, respectively. There are certain interfaces, such as
the DB2 command line processor (CLP) or the JDBC interface, that run in
autocommit mode. For other application interfaces, autocommit is turned off by
default.
Another matter causing controversy among experts is the level of locking that is
required for implementation on the database level. Do you implement the locking
approach with the lowest level of overhead and, therefore, maintain locks on a
table level? Or, is it better to lock on a lower level, for example, on the page
level? Or, do you want even finer granularity with locking occurring on the row
level? As usual, the correct answer to these questions is, It depends.
274
DB2 tables
MyISAM tables
InnoDB tables
Lock level
Row level,
table level only on
explicit request
Commitment
control
Yes
No
Yes
Isolation level
Uncommitted
Reads, Cursor
Stability, Read
Stability, and
Repeatable Reads
No
Read
Uncommitted,
Read Committed,
Repeatable Reads,
Serializable
Rollback on DDL
Yes
No
Yes
275
However, there are concurrency issues that might arise when converting a
MySQL application to DB2 based on the two MySQL table types that we consider
significant:
MyISAM tables provide a high level of concurrency, because SQL processing
occurs in autocommit mode and no row-level locks are maintained. When
converting to DB2, ensure that your application operates in autocommit
mode. Verify the lowest isolation level required for your application and
MyISAM tables.
InnoDB tables provide concurrency control similar to DB2. Note that default
transaction isolation for InnoDB is repeatable read.
276
277
This isolation level overrides the isolation level that is specified for the package in
which the statement appears. You can specify an isolation level for the following
SQL statements:
DECLARE CURSOR
Searched DELETE
INSERT
SELECT
SELECT INTO
Searched UPDATE
Note: Isolation levels for XQuery statements cannot be specified at the
statement level.
278
Chapter 9.
Database administration
In this chapter, we focus on the database administration features that are offered
by DB2. We provide a general introduction and detailed description of DB2
administration programs, utilities, and tools. We also describe a few of the salient
features that are available in DB2 but that are missing in MySQL.
We present key attributes of database administration for DB2, such as:
Database configuration
Data recovery
Database replication
Data movement utilities
High availability
Autonomics
Workload Manager
279
280
DB2 configures the operating environment by checking for registry values and
environment variables, and DB2 resolves them in the following order:
1. Environment variables are set using the set command (or the export
command on UNIX platforms).
2. Registry values are set with the instance node-level profile (using the db2set
-i <instance name> <nodenum> command).
3. Registry values are set with the instance-level profile (using the db2set -i
command).
4. Registry values are set with the global-level profile (using the db2set -g
command).
281
282
Configuration tools
IBM has tools to assist you with configuring your database server. Two of these
tools are the Configuration Assistant and the IBM Data Studio.
You can use the DB2 Configuration Assistant to configure and maintain the
database objects that you or your application will use. The Configuration
Assistant is a graphical tool that is tightly integrated with the DB2 Control Center.
It allows you to update both the DB2 Profile Registry and the DB2 database
manager configuration parameters on the local machine, as well as remotely. It
can be launched from the DB2 Control Center or by calling the db2ca utility. The
Configuration Assistant also has an advanced view, which uses a notebook to
organize connection information by object: systems, instance nodes, databases,
database connection services (DCS), and data sources. Figure 9-2 on page 284
shows how to change the database manager configuration using the
Configuration Assistant.
283
Note: The Configuration Assistant has been deprecated in Version 9.7 and
might be removed in a future release. We recommend that you use the IBM
Integration Management solutions for managing DB2 for Linux, UNIX, and
Windows data and data-centric applications.
IBM Data Studio is part of the IBM Integration Management solutions for
managing your DB2 database. Data Studio simplifies the process of managing
your database objects by supporting instance and database management and by
providing the ability to run database commands and utilities. It provides a simple
user interface to invoke the database administration commands that you use to
maintain and manage your database environment. Figure 9-3 on page 285
shows how to change the database manager and database configuration using
Data Studio.
284
IBM offers a number of automatic tools and DB2 features to make database
administration effortless. You can use the Configuration Advisor to assist with
parameter configuration and to configure your database for optimal performance.
The Configuration Advisor looks at your current database, asks for user input on
the database workload, and suggests the best configuration parameters for
buffer pool size, database configuration, and database manager configuration.
Figure 9-4 on page 286 shows the suggested output for our sample inventory
database.
285
286
287
Database backup
To back up a DB2 database, database partition, or selected table space, you can
use the DB2 backup command. Use this command to create a backup to disk,
tape, or named pipes in UNIX. DB2 supports both offline and online backup:
db2 backup database invent to /home/db2inst1/backup
You can back up an entire database, database partition, or only selected table
spaces.
In addition to backing up the entire database every time, DB2 also supports
incremental backups where you can back up large databases on a regular basis
incrementally. Incremental backups require that the trackmod database
configuration parameter is set to yes. Incremental backup can be a cumulative
backup, which stores data changes since the last successful full backup, or a
delta backup, which is the last successful backup irrespective of whether that
backup was full, delta, or cumulative. Figure 9-6 on page 289 and Example 9-2
on page 289 show the cumulative and delta backup techniques.
288
IBM has tools to assist you with the maintenance activities configuration,
because it can be time-consuming to determine when the configuration is
required and whether maintenance activities, such as backup operations, need
to be run. You can use the Configure Automatic Maintenance wizard within Data
Studio, as shown in Figure 9-7 on page 290, or the DB2 Control Center to
configure the database maintenance activities. With automatic maintenance, you
specify your maintenance objectives, including when automatic maintenance can
run. DB2 then uses these objectives to determine if the maintenance activities
need to be done and then runs only the required maintenance activities during
the next available maintenance window (a user-defined time period for running
automatic maintenance activities).
289
Database recovery
The recover utility performs the necessary restore and roll-forward operations to
recover a database to a specified time, based on information found in the
recovery history file. When you use this utility, you specify that the database is
recovered to a certain point in time or to the end of the log files. The utility will
then select the best suitable backup image and perform the recovery operations.
Example 9-3 shows how to use the RECOVER DATABASE command.
Example 9-3 Recover database
b2inst1@db2server:~>
= invent
= 1
Node number
Rollforward status
Next log file to be read
Log files processed
Last committed transaction
=
=
=
=
=
DB20000I
290
0
not pending
S0000000.LOG - S0000001.LOG
2009-09-11-19.25.01.000000 Local
Or, you can restart the database when a database failure occurs by calling
this command:
db2inst1@db2server:~> db2 RESTART DATABASE invent
DB2 maintains log files, the recovery history file, and the table space change
history file to recover data that is lost or damaged.
Version recovery
291
Roll-forward recovery
Use Roll-forward recovery to reapply changes that were made by
transactions that were committed after a backup was made.
There are two types of roll-forward recovery:
Database roll-forward recovery
With this type of roll-forward recovery, you can tell DB2 to roll forward
a database to the state immediately before the failure or you can
specify the local time to which you want to roll forward your database.
Figure 9-9 on page 293 shows the roll-forward recovery technique of a
DB2 database.
292
Figure 9-11 on page 294 shows how you can use Data Studio to recover a DB2
database.
293
Database restore
DB2 database restore is as easy as backing up the database. Use the
RESTORE utility to perform DB2 database restore. The restore database
command rebuilds the database data or table space to the state that it was in
when the backup copy was made. This utility can overwrite a database with a
separate image or restore the backup copy to a new database. You can also use
the restore utility to restore backup images in DB2 Version 9.7 that were backed
up on DB2 Universal Database Version 8, DB2 Version 9.1, or DB2 Version 9.5.
This RESTORE utility supports full and incremental database restore.
Incremental database restore can be automatic or manual. Example 9-4 shows
automatic incremental restore, and Example 9-5 on page 295 shows manual
incremental restore.
Example 9-4 Automatic incremental restore
db2inst1@db2server:~ > db2 RESTORE DATABASE invent INCREMENTAL AUTOMATIC FROM
/home/db2inst1/backup TAKEN AT 20090911214318
294
If at the time of the backup operation, the database was enabled for roll-forward
recovery, you can take the database to its previous state by invoking the
following ROLLFORWARD command after a successful completion of a restore
operation:
db2inst1@db2server:~ > db2 ROLLFORWARD DATABASE invent COMPLETE
Tip: You can also perform DB2 backup and restore using the Data Studio.
You can also execute he RESTORE and ROLLFORWARD utilities from the Data
Studio. Figure 9-12 on page 296 shows the restore database window.
295
296
297
EXPORT utility
DB2 EXPORT is a powerful tool to export your DB2 data quickly from DB2 to the
external file system. DB2 EXPORT uses SQL select or an XQuery statement to
export tables, views, large objects, or typed tables to one of the three external file
formats:
.DEL: Delimited ASCII format file
.WSF: Worksheet format, such as Lotus 1-2-3
.IXF: Integrated exchange format
298
You can invoke the EXPORT utility by using the following methods:
Through the command line processor (CLP)
You can use the EXPORT utility from CLP by supplying an SQL SELECT or
XQuery statement or by providing hierarchical information for typed tables, as
shown below:
db2inst1@db2server:~ > db2 EXPORT TO invent.ixf of ixf SELECT * FROM
admin.owners
Data Studio
You can extract data from a DB2 database using the graphical user interface
called Data Studio. This tool allows you to set export options for each table
visually. Figure 9-13 shows the usage of the export through the DB2 Control
Center.
299
IMPORT utility
You can use the files created with the same syntax as the EXPORT utility to
populate data into a new DB2 database on the same system. Or, you can
transfer these files to another platform and import or load them to the DB2
database that resides on that platform. The IMPORT utility supports the following
file formats:
Similar to EXPORT, you can use the IMPORT utility with the following methods:
The command line processor (CLP)
Use the IMPORT utility from CLP by supplying an SQL INSERT, INSERT
UPDATE, or REPLACE option. The example shows the simple IMPORT
statement:
db2inst1@db2server:~ > db2 IMPORT FROM invent.ixf OF ixf MESSAGES
msg.txt INSERT INTO admin.owners
Data Studio
Use Data Studio to import data graphically. Figure 9-14 on page 301 shows
the Import wizard.
300
301
DB2MOVE utility
You can copy data using the DB2 EXPORT and IMPORT utilities. But a more
efficient way to copy an entire DB2 database schema is by using the DB2
db2move utility. This utility queries the system catalog tables for a specified
database and exports the table structure and the contents of each table found to
a PC/IXF formatted file. You can use these files to populate another DB2
database. You can run the DB2 db2move utility in three modes:
EXPORT mode
In this mode, the db2move utility invokes the DB2 EXPORT utility to extract
data from one or more tables and write to PC/IXF formatted files. It also
creates a db2move.lst file that contains the names of all of the exported
tables and the names of the files to which the table data was written. Use
EXPORT mode in this way:
db2inst1@db2server:~ > db2move invent EXPORT
IMPORT mode
In this mode, the db2move utility invokes the DB2 IMPORT utility to recreate
tables and indexes from data that is stored in PC/IXF formatted files. You can
use the db2move.lst file that is generated in EXPORT mode to get
information about tables in the exported files. You can import the exported
files by using this command:
db2inst1@db2server:~ > db2move invent IMPORT
LOAD mode
In this mode, the db2move utility invokes the DB2 LOAD utility to populate
tables that already exist with data stored in PC/IXF formatted files. Use the
db2move.lst file that is generated in EXPORT mode to get information about
tables. Load these exported files by using this command:
db2inst1@db2server:~ > db2move invent LOAD -l /home/db2inst1/export
302
There are four modes in which you can execute the LOAD utility:
INSERT
In this mode, the LOAD utility appends input data to the table without making
any changes to the existing data.
REPLACE
In this mode, the LOAD utility deletes existing data from the table and
populates it with the input data.
RESTART
In this mode, an interrupted load is resumed. In most cases, the load is
resumed from the phase in which it failed. If that phase was the load phase,
the load is resumed from the last successful consistency point.
TERMINATE
In this mode, a failed load operation is rolled back.
You can use the LOAD utility with these methods:
The command line processor (CLP)
Use the IMPORT utility from the CLP by supplying an SQL INSERT, INSERT
UPDATE, or REPLACE option. This example shows the simple IMPORT
statement:
db2inst1@db2server:~ > db2 LOAD FROM ownersdata.del OF del REPLACE INTO
admin.owner
Data Studio
Use the Data Studio to load data graphically. Figure 9-15 on page 304 shows
the Load wizard.
303
304
305
For more information and implementation steps, refer to High Availability and
Disaster Recovery Options for DB2 on Linux, UNIX, and Windows, SG24-7363.
9.6 Autonomics
Automated task management allows the automation of database management
jobs by scheduling activities according to specific requirements. Automated task
management is really useful for performing regular maintenance tasks, such as
backup, space monitoring, error checking, maintenance, and so forth.
IBM strives for autonomic computing and the development of highly intelligent
database systems. The DB2 autonomic computing environment is
self-configuring, self-healing, self-optimizing, and self-protecting. By sensing and
responding to situations that occur, autonomic computing shifts the burden of
managing a computing environment from the database administrator to the
technology. Autonomic computing provides users with improved resiliency,
higher return on investment (ROI), and lower total cost of ownership (TCO) by
accelerating the implementation of new capabilities to gain the highest value
possible.
306
Example 9-6 on page 308 shows how to enable Self-Tuning Memory Manager
and how to configure each parameter to be managed by Self-Tuning Memory
Manager.
307
Automatic Storage
The automatic storage feature simplifies storage management for table spaces.
When you create an automatic storage database, you specify the storage paths
where the database manager will place your table space data. Then, the
database manager manages the container and space allocation for the table
spaces as you create and populate them. By default, automatic storage is turned
on:
Automatic storage databases
Automatic storage is intended to make storage management easier. Rather
than managing storage at the table space level using explicit container
definitions, storage is managed at the database level and the responsibility of
creating, extending, and adding containers is taken over by the database
manager.
308
Automatic maintenance
The database manager provides automatic maintenance capabilities for
performing database backups, keeping statistics current, and reorganizing tables
and indexes as necessary. Performing maintenance activities on your databases
is essential in ensuring that they are optimized for performance and
recoverability.
Maintenance of your database can include part, or all, of the following activities:
Backups
When backing up a database, the database manager takes a copy of the data
in the database and stores it on another medium in case of failure or damage
to the original. Automatic database backups help to ensure that your
database is backed up properly and regularly so that you do not have to worry
about when to back up or know the syntax of the BACKUP command.
Data defragmentation (table or index reorganization)
This maintenance activity can increase the efficiency with which the database
manager accesses your tables. Automatic reorganization manages an offline
table and index reorganization so that you do not need to worry about when
and how to reorganize your data.
Data access optimization (statistics collection)
The database manager updates the system catalog statistics on the data in a
table, the data in indexes, or the data in both a table and its indexes. The
optimizer uses these statistics to determine which path is necessary to
access the data. Automatic statistics collection attempts to improve the
performance of the database by maintaining up-to-date table statistics. The
goal is to allow the optimizer to choose an access plan based on accurate
statistics.
309
Statistics profiling
Automatic statistics profiling advises when and how to collect table statistics
by detecting outdated, missing, or incorrect statistics, and by generating
statistical profiles based on query feedback.
It can be time-consuming to determine whether and when to run maintenance
activities, which makes the automatic maintenance extremely convenient. You
can manage the enablement of the automatic maintenance features simply and
flexibly by using the automatic maintenance database configuration parameters.
The easiest way to set up automatic maintenance is to use the graphical
administrative tools. You can access the Configure Automatic Maintenance
wizard from either the Data Studio or the DB2 Control Center. We show
configuring maintenance objectives by using the Data Studio in Figure 9-7 on
page 290. The database manager uses these objectives to determine whether
the maintenance activities need to be done and runs only the required
maintenance activities during the next available maintenance window (a time
period that you define in which the server will not be as heavily used).
Configuration Advisor
You can use the Configuration Advisor to obtain recommendations for values of
the buffer pool size, database configuration parameters, and database manager
configuration parameters.
To use the Configuration Advisor, specify the AUTOCONFIGURE command for
an existing database, or specify AUTOCONFIGURE as an option of the
CREATE DATABASE command.
You can display the recommended values or apply them by using the APPLY
option of the CREATE DATABASE command. The recommendations are based
on input that you provide and system information that the Configuration Advisor
gathers.
When you create a database, this tool is automatically run to determine and set
the database configuration parameters and the size of the default buffer pool
(IBMDEFAULTBP). The values are selected based on system resources and the
intended use of the system. This initial automatic tuning means that your
database performs better than an equivalent database that you might create with
the default values. It also means that you will spend less time tuning your system
after creating the database.
You can run the Configuration Advisor at any time (even after your databases
are populated) to have the tool recommend and optionally apply a set of
configuration parameters to optimize performance based on the current system
characteristics. You can use the graphical database administration tools to run
310
the Configuration Advisor. Figure 9-4 on page 286 shows the Data Studio
Configuration Advisor Wizard.
The values that are suggested by the Configuration Advisor are relevant for only
one database per instance. If you want to use the Configuration Advisor on more
than one database, each database must belong to a separate instance.
Data compression
You can compress both tables and indexes to save storage. Compression is fully
automatic. After you specify that a table or index must be compressed using the
COMPRESS YES clause of the CREATE TABLE, ALTER TABLE, CREATE
INDEX, or ALTER INDEX statements, there is nothing more you must do to
manage compression. Temporary tables are compressed automatically; indexes
for compressed tables are also compressed automatically, by default. We
discuss data compression further in 11.2, Data compression on page 386.
Utility throttling
This feature regulates the performance impact of maintenance utilities so that
they can run concurrently during production periods. Although the impact policy
for throttled utilities is defined by default, you must set the impact priority if you
want to run a throttled utility. The throttling system ensures that the throttled
utilities run as frequently as possible without violating the impact policy.
Currently, you can throttle statistics collection, backup operations, rebalancing
operations, and asynchronous index cleanup.
311
Control Center
Optim Development Studio
Optim Database Administrator
IBM Data Studio
Data Studio Administration Console
312
DB2 Control Center provides a common interface for managing DB2 databases
on various platforms. You can run DB2 commands, create DDL statements, and
execute DB2 utilities. DB2 Control Center provides point-and-click navigation
capabilities to make it easy to find objects, whether you have hundreds or tens of
thousands of objects in your database environment. Use it to administer the
system, instances, tables, views, indexes, triggers, user-defined types,
user-defined functions, packages, aliases, users, or groups.
DB2 Control Center is tightly coupled with other DB2 tools; Figure 9-17 shows a
hierarchy of database objects on the leftmost panel and details on the rightmost
panel. You start the Control Center by entering the db2cc command.
You can start the following tools from the Control Center Tools menu:
Replication Center
Satellite Administration Center Command Center
Command Editor
Task Center
Health Center
Journal
License Center
Configuration Assistant
313
DB2 Control Center also provides a set of wizards for completing specific
administration tasks by taking you through each step, one at a time. The
following DB2 wizards are available through the Control Center:
314
There are two versions, the Optim Database Administrator and the Optim
Development Studio, that you purchase. There is one no-charge version of IBM
Data Studio.
315
316
Working in the Data Source Explorer, you can perform these tasks:
Connect to data sources and browse data objects and their properties.
Use editors and wizards to create and alter data objects.
Modify privileges for data objects and authorization IDs.
Copy database objects.
Drop data objects from databases.
Analyze the impact of your changes.
Work with data, including extracting and loading data and inserting XML data
into XML columns.
Use data diagrams to visualize the relationships between data objects.
If you work on a large team, you can use the following features to enable team
members to share resources:
You can share data development projects using supported source code
control systems.
You can share database connection information by importing and exporting
this information to XML files.
You can customize the user interface to enable and disable visible controls
and defaults.
Optim Development Studio 2.2 is on Eclipse Version 3.4, and here are several
products with which it can share the shell:
Rational Application Developer for WebSphere Software 7.5.x.x
Optim Database Administrator 2.2
InfoSphere Data Architect 7.5.x.x
317
For more information and to download the software, go to this Web site:
http://www.ibm.com/software/data/optim/
318
319
320
10
Chapter 10.
Test planning
Data checking
Code and application testing
Troubleshooting
We also provide information about how you can check that system behavior has
not changed in an undesired way.
Furthermore, we discuss the methods and tools available for DB2 to tune the
database in order to achieve optimal performance.
321
322
Acceptance testing
Post-conversion tests
Prepare Infrastructure
Test Run in
MySQL Environment
Test Run in
DB2 Environment
The time exposure of tests depends on the availability of an existing test plan
and already prepared test items. The efforts depend also on the degree of
changes during the application and database conversion.
323
Note: Test efforts can range between 50% and 70% of the total conversion
effort.
SQL3148W A row from the input file was not inserted into the table. SQLCODE "-545" was
returned.
SQL0545N The requested operation is not allowed because a row does not
satisfy the check constraint "DB2INST1.TABLE01.SQL090915100543100".
SQLSTATE=23513
324
SQL3185W
file.
The previous error occurred while processing data from row "2" of the input
SQL3117W The field value in row "3" and column "1" cannot be converted to a SMALLINT
value. A null was loaded.
SQL3125W The character data in row "4" and column "2" was truncated because the data is
longer than the target database column.
SQL3110N
SQL3221W
SQL3222W
SQL3149N "4" rows were processed from the input file. "3" rows were
successfully inserted into the table. "1" rows were rejected.
Number
Number
Number
Number
Number
Number
of
of
of
of
of
of
rows
rows
rows
rows
rows
rows
read
skipped
inserted
updated
rejected
committed
=
=
=
=
=
=
4
0
3
0
1
4
As shown in the summary, during the import process one record from the input
file was rejected, and three records were inserted into the database. To
understand the nature of the warnings, you must look into the data source file
and the table definition (use the db2look command). The table definition for
Example 10-1 on page 324 is shown in Example 10-2, and the data file for
Example 10-1 on page 324 is shown in Example 10-3.
Example 10-2 Table definition for Example 10-1
db2> CREATE TABLE TABLE01 (
C1 SMALLINT,
C2 CHAR(3),
C3 SMALLINT CHECK( C3 IN (1,2,3)))
Example 10-3 Data file for Example 10-1
1,"abc",1
2,"abc",4
32768,"abc",2
4,"abcd",3
325
The first row from the input file (Example 10-3 on page 325) was inserted without
any warnings. The second row was rejected, because it violated check
constraints (warnings SQL3148W, SQL0545N, and SQL3185W). A value of 32768 from
the third row was changed to null, because it was out of the SMALLINT data type
range (warning SQL3117W) and string abcd from the last row was truncated to abc,
because it was longer than the relevant column definition (warning SQL3125W).
The LOAD utility generates messages in a similar format, but because it is
designed for speed, it bypasses the SQL engine and inserts data directly into
table spaces without constraint checking. Inserting the same table01.unl file
(Example 10-3 on page 325) into table01 (Example 10-2 on page 325) with the
LOAD utility generates messages without SQL3148W, SQL0545N, and SQL3185W
warnings, as shown in Example 10-4.
Example 10-4 LOAD messages
db2inst1@db2server:~> db2 LOAD FROM table01Data.txt OF DEL REPLACE INTO table02
SQL3109N The utility is beginning to load data from file
"/home/db2inst1/DB2Scripts/chp10/table01Data.txt".
SQL3500W The utility is beginning the "LOAD" phase at time "09/15/2009
10:15:23.814793".
SQL3519W
SQL3520W
SQL3117W The field value in row "F0-3" and column "1" cannot be converted to a SMALLINT
value. A null was loaded.
SQL3125W The character data in row "F0-4" and column "2" was truncated
because the data is longer than the target database column.
SQL3227W
SQL3227W
SQL3519W
SQL3520W
SQL3515W The utility has finished the "LOAD" phase at time "09/15/2009
10:15:23.836019".
SQL3107W
326
= 4
= 0
Number
Number
Number
Number
of
of
of
of
rows
rows
rows
rows
loaded
rejected
deleted
committed
=
=
=
=
4
0
0
4
A table that has been created with constraints is left by the LOAD command in
check pending state. Accessing the table with SQL queries generates a warning:
SQL0668N Operation not allowed for reason code "1" on table <TABLE_NAME>.
SQLSTATE=57016.
You need to use the SET INTEGRITY SQL statement to move loaded tables into
a usable state. Example 10-5 shows a way to validate constraints. All rows that
violated constraints will be moved to exception table table01_e.
Example 10-5 Turning integrity checking on
db2inst1@db2server:~> db2 CREATE TABLE table02_exp LIKE table02
db2inst1@db2server:~> db2 SET INTEGRITY FOR table02 IMMEDIATE CHECKED FOR
EXCEPTION IN table02 USE table02_exp
SQL3602W Check data processing found constraint violations and moved them to
exception tables. SQLSTATE=01603
The SET INTEGRITY statement has many options, such as turning integrity on
only for new data, turning integrity off, or specifying exception tables with
additional diagnostic information. To read more about the SET INTEGRITY
command, refer to this Web site:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
327
For each table, you must count the number of rows and store the information in
the CK_ROW_COUNT table. You can use the following INSERT statement for
that purpose:
INSERT INTO ck_row_count SELECT 'tab_name', COUNT(*), 'MYS', sysdate() FROM
tab_name
You can manually convert the table ck_row_count and its data to the target DB2
database. Example 10-7presents the DB2 version of the table.
Example 10-7 Table for storing number of rows (DB2)
CREATE TABLE ck_row_count (
tab_name VARCHAR(30),
row_count INT,
sys_name CHAR(3),
time_ins TIMESTAMP
)
On the DB2 system, repeat the counting process with the equivalent INSERT
statement:
INSERT INTO ck_row_count SELECT 'tab_name', COUNT(*), 'DB2', CURRENT
TIMESTAMP FROM tab_name
328
After performing the described steps, DB2 table CK_ROW_COUNT will contain
information about the number of rows counted in the MySQL and DB2
databases. The records in the table will look similar to Example 10-8.
Example 10-8 Sample table ck_row_count contents
SELECT * FROM ck_row_count
TAB_NAME
ROW_COUNT SYS_NAME TIME_INS
------------- ---------- -------- -------------------------inventory
703
DB2
2009-09-15-10.53.46.573998
services
808
DB2
2009-09-15-10.55.18.782980
groups
6
DB2
2009-09-15-10.55.45.852181
owners
502
DB2
2009-09-15-10.56.02.341948
locations
140
DB2
2009-09-15-10.56.15.269177
severity
5
DB2
2009-09-15-10.56.31.337119
status
7
DB2
2009-09-15-10.56.51.599962
Having the information about the number of rows in an SQL table is convenient,
because with a single query, you can get the table names that contain a different
number of rows in the source and target database:
SELECT tab_name FROM (SELECT DISTINCT tab_name, row_count FROM
ck_row_count) AS t_temp GROUP BY t_temp.tab_name HAVING(COUNT(*)
>
1)
You can extend this approach for comparing the number of rows for additional
checking, such as comparing the sum of numeric columns. Here are the steps
that summarize the technique:
1. Define check sum tables on the source database and characterize the scope
of the computation.
2. Perform the computation and store the results in the appropriate check sum
tables.
3. Convert the check sum tables just as you convert the other user tables.
4. Perform equivalent computations on the target system, and store the
information in the converted check sum tables.
5. Compare the computed values.
Table 10-1 on page 330 provides computations for selected database types. The
argument for the DB2 SUM() function is converted to DECIMAL type, because, in
most cases, the SUM() function returns the same data type as its argument,
which can cause arithmetic overflow. For example, when calculating the sum on
an INTEGER column, if the result exceeds the INTEGER data type range, error
SQL0802N is generated: Arithmetic overflow or other arithmetic exception
occurred. Converting the argument to DECIMAL eliminates the error.
329
MySQL operation
DB2 operation
numeric(<precision>,<scale>)
sum(val)
sum(cast(val as
decimal(31,<scale>)))
date
sum(cast(days(val) as
decimal(31,1)))
sum(length(val))
sum(cast(length(val) as
decimal(31,0)))
sum(length(rtrim(val)))
sum(cast(length(rtrim(val))as
decimal(31,0)))
:
:
:
:
:
:
:
6
703
140
502
808
5
330
331
10.4 Troubleshooting
The first step of problem determination is to know what information is available to
you. When DB2 performs an operation, an associated return code is returned.
The return code is displayed to the user in the form of an informational or error
message. These messages are logged into diagnostic files depending on the
diagnostic level that is set in the DB2 configuration. In this section, we discuss
the DB2 diagnostic logs, error message interpretations, and tips, which might
help with problem determination, troubleshooting, and resolutions to specific
problems.
Perform the following actions when experiencing a DB2-related problem:
Check related messages.
Explain error codes.
Check documentation.
Search through available Internet resources.
Review authorized program analysis reports (APARs) for the current fix pack
level.
Use the available tools to narrow the problem.
Ask IBM for support.
332
All the containers assigned to this DMS table space are full.
This is the likely cause of the error.
[...]
You can find the complete information about the DB2 message format and a
listing of all the messages in the Messages Reference, Volume 1,
SC27-2450-00, and Messages Reference, Volume 2, SC27-2451-00, which are
available online at this Web site:
http://www.ibm.com/support/docview.wss?rs=71&uid=swg27015148
333
334
db2pd
Use the db2pd tool for troubleshooting, because it can return quick and
immediate information from the DB2 memory sets.
db2support
When collecting information for a DB2 problem, the most important DB2 utility
is db2support. The db2support utility automatically collects all of the DB2 and
system diagnostic information that is available. It also has an optional
interactive Question and Answer session, which poses questions about the
circumstances of your problem.
Traces
If you experience a recurring and reproducible problem with DB2, tracing
sometimes allows you to capture additional information about it. Under
normal circumstances, only use a trace when asked by IBM Software Support
to use a trace. The process of taking a trace entails setting up the trace
facility, reproducing the error, and collecting the data.
335
DB2 is not the only product that can write to the notification logs, tools such as
the Health Monitor, Capture and Apply programs, and user applications can also
write to these logs using the db2AdminMsgWrite API function.
db2diag.log
The db2diag.log file is most frequently used file for DB2 problem investigation.
You can find this file in the DB2 diagnostic directory, defined by the DIAGPATH
variable in the database manager configuration. If the DIAGPATH parameter is
not set, by default, the directory is located at this path for Linux and UNIX:
$HOME/sqllib/db2dump
<INSTALL PATH> is the directory where DB2 is installed, and <DB2INSTANCE> is the
name of the DB2 instance.
The database manager configuration parameter DIAGLEVEL controls how much
information is logged to the db2diag.log file. Valid values can range from 0 - 4:
Most of the time, the default value is sufficient for problem determination. In
certain cases, especially on development or test systems, you can set the
parameter to 4 to collect all informational messages. However, ensure that you
focus on the database activities and the size that is available on the file system,
because this information can cause performance issues due to the large
amounts of data recorded in the file. Setting DIAGLEVEL to 4 can also make the
file extremely large and harder to read.
This db2diag.log file includes this information:
A diagnostic message (beginning with DIA) explaining the reason for the error
Application identifiers, which allow you to match error entries with
corresponding application or DB2 server processes
Any available supporting data, such as SQLCA data structures, and pointers
to the location of any extra dump or trap files
Administrative events, such as backup and restore start and finish
336
We next explain the db2diag.log file entries. The numbers bolded in the
example correspond to the following numbers:
1. A time stamp and time zone for the message.
2. The record ID field. The recordID of the db2diag log file specifies the file
offset at which the current message is being logged (for example, 27204)
and the message length (for example, 655) for the platform where the DB2
diagnostic log was created.
3. The diagnostic level associated with an error message, for example, Info,
Warning, Error, Severe, or Event.
4. The process ID.
5. The thread ID.
6. The process name.
7. The name of the instance generating the message.
8. For multi-partition systems, the database partition generating the message.
(In a non-partitioned database, the value is 000.)
9. The database name.
10.The application handle. This value aligns with that used in db2pd output and
lock dump files. It consists of the coordinator partition number followed by the
coordinator index number, separated by a dash.
11.Identification of the application for which the process is working. In this
example, the process generating the message is working on behalf of an
application with the ID 9.26.54.62.45837.070518182042.
The TCP/IP-generated application ID is composed of three sections:
i. IP address: It is represented as a 32-bit number displayed as a
maximum of 8 hexadecimal characters.
337
Trap files
The database manager generates a trap file if it cannot continue processing due
to a trap, segmentation violation, or exception.
All signals or exceptions received by DB2 are recorded in the trap file. The trap
file also contains the function sequence that was running when the error
occurred. This sequence is sometimes referred to as the function call stack or
stack trace. The trap file also contains additional information about the state of
the process when the signal or exception was caught.
A trap file is also generated when an application is forced off the system while
running a fenced thread-safe routine. The trap occurs as the process is shutting
down. This trap file is not a fatal error, and it is nothing to be concerned about.
The files are located in the directory specified by the DIAGPATH database
manager configuration parameter.
On all platforms, the trap file name begins with a process identifier (PID),
followed by a thread identifier (TID), followed by the partition number (000 on
single partition databases), and concludes with .trap.txt.
There are also diagnostic traps, which are generated by the code when certain
conditions occur that do not warrant crashing the instance, but where it might be
useful to verify values within the stack. These traps are named with the PID in
decimal format, followed by the partition number (0 in a single partition
database).
The following example resembles a trap file with a process identifier (PID) of
6881492 and a thread identifier (TID) of 2.
6881492.2.000.trap.txt
338
The following example is a trap file whose process and thread are running on
partition 10.
6881492.2.010.trap.txt
You can generate trap files on demand using the db2pd command with the -stack
all or -dump option. In general, though, only run this command as requested by
IBM Software Support.
Dump files
When DB2 determines that extra information is required for collection due to an
error, it often creates binary dump files in the diagnostic path. The binary dump
file is named with the process or thread ID that failed, the node where the
problem occurred, and ends with the .dump.bin extension, as shown in this
example:
6881492.2.010.dump.bin
Maintenance version
You can use the db2level utility to check the current version of DB2. As shown in
Figure 10-2 on page 340, the utility returns information about the installed
maintenance updates (fix packs), the word length used by the instance (32-bit or
64-bit), the build date, and other code identifiers. We recommend that you
periodically check to determine if the newest available fix packs are installed.
339
The dot represents the current directory where the output file is stored. The rest
of the command options are not required and can be omitted. The -d and -c
clauses instruct the utility to connect to the invent database and to gather
information about database objects, such as table spaces, tables, or packages.
The Web site has the most recent copies of documentation, a knowledge base to
search for technical recommendations or DB2 defects, links for product updates,
the latest support news, and other useful DB2-related links.
340
To find related problems, prepare words that describe the issues, such as the
commands that were run, symptoms, or tokens from the diagnostic messages.
You can use these words as search terms in the DB2 Knowledge Base. The
Knowledge Base offers an option to search through DB2 documentation,
TechNotes, and DB2 defects (APARs). TechNotes are recommendations and
solutions for specific problems.
Authorized Program Analysis Reports (APARs) are defects in the DB2 code that
have been discovered by clients and that require a fix. APARs have unique
identifiers and are always specific to a particular version, but they can affect
multiple products in the DB2 family that run on multiple platforms. Fixes for
APARs are provided through the DB2 fix packs.
On the DB2 support site, you can search for closed, open, and HIPER APARs. A
closed status for an APAR indicates that a resolution for a problem has been
created and included in a specific fix pack. Open APARs represent DB2 defects
that are currently being addressed or are waiting to be included in the next
available fix pack. High-Impact or PERvasive (HIPER) APARs are critical
problems that you must review to assess the potential affect of staying at a
particular fix pack level.
The DB2 Technical Support site offers e-mail notification of critical or pervasive
DB2 client support issues, including HIPER APARs and fix pack alerts. To
subscribe to it, follow the DB2 Alert link on the Technical Support main page.
You can also send DB2 for Linux, UNIX, and Windows questions to:
[email protected].
341
342
MON_GET_SERVICE_SUBCLASS
MON_GET_SERVICE_SUBCLASS_DETAILS
MON_GET_WORKLOAD
MON_GET_WORKLOAD_DETAILS
MON_GET_CONNECTION
MON_GET_CONNECTION_DETAILS
MON_GET_UNIT_OF_WORK
MON_GET_UNIT_OF_WORK_DETAILS
MON_GET_BUFFERPOOL
MON_GET_TABLESPACE
MON_GET_CONTAINER
MON_GET_TABLE
MON_GET_INDEX
Example 10-12 shows an example of how you can use the MON_GET_TABLE
function to retrieve the rows read, inserted, updated, and deleted from all tables
in the ADMIN schema.
Example 10-12 Monitor table function
db2inst1@db2server:~> db2 "SELECT varchar(tabschema,20) as tabschema,
varchar(tabname,20) as tabname,
sum(rows_read) as total_rows_read,
sum(rows_inserted) as total_rows_inserted,
sum(rows_updated) as total_rows_updated,
sum(rows_deleted) as total_rows_deleted
FROM TABLE(MON_GET_TABLE('ADMIN','',-1)) AS t
GROUP BY tabschema, tabname
ORDER BY total_rows_read
DESC"
Snapshot monitoring
Snapshot monitoring describes the state of database activity at a particular point
in time when a snapshot is taken. Snapshot monitoring is useful in determining
the current state of the database and its applications. Taken at regular intervals,
the snapshots are useful for observing trends and foreseeing potential problems.
You can take snapshots from the command line, by using custom APIs, or
through SQL by using table functions. Example 10-13 on page 344 shows an
extract from a sample snapshot invoked from the command line.
343
9
767
7
1
0
7
9
9
[...]
Buffer
Buffer
Buffer
Buffer
pool
pool
pool
pool
=
=
=
=
Not
Not
Not
Not
Collected
Collected
Collected
Collected
[...]
In Example 10-13, the snapshot has collected database-level information for the
INVENT database. Several of the returned parameters display point-in-time
values, such as the number of currently connected applications:
Applications connected currently
= 767
Other parameters can contain historical values, such as the maximum number of
concurrent connections that have been observed on the database:
High water mark for connections
Cumulative or historical values are used to relate to the point in time during the
last initialization of counters. The counters can be reset to zero by the RESET
MONITOR command or by the appropriate DB2 event. In Example 10-13,
344
=
=
=
=
=
=
=
OFF
OFF
OFF
OFF
OFF
ON 09/11/2009 22:10:34.801069
The monitor switches can be turned on at the instance level or the application
level. To switch the monitors at the instance level, modify the appropriate
database manager parameter. After modifying the DFT_MON_BUFPOOL
parameter, as shown in Example 10-16, all users with SYSMAINT, SYSCTRL, or
SYSADM authorities are able to collect buffer pool statistics on any database in
the instance.
Example 10-16 Updating monitor switches at the instance level
db2 UPDATE DBM CFG USING DFT_MON_BUFPOOL ON
345
To switch the monitors at the application level, issue the UPDATE MONITOR
SWITCHES command using the command line. The changes only are applicable
to that particular prompt window. Example 10-17 shows how to update the
suitable monitor switch for collecting buffer pool information.
Example 10-17 Updating monitor switches at the application level
db2 UPDATE MONITOR SWITCHES USING BUFFERPOOL ON
Table 10-2 shows the complete list of monitor switches and related database
manager (DBM) parameters.
Table 10-2 List of monitor switches and related DBM parameters
Database manager
parameter
Monitor switch
Information provided
DFT_MON_BUFPOOL
BUFFERPOOL
DFT_MON_LOCK
LOCK
DFT_MON_SORT
SORT
DFT_MON_STMT
STATEMENT
DFT_MON_TABLE
TABLE
DFT_MON_UOW
DFT_MON_TIMESTAMP
TIMESTAMP
Sample snapshots
The database manager snapshot (Example 10-18) captures information specific
to the instance level. The information centers around the total amount of memory
that is allocated to the instance and the number of agents that are currently
active on the system.
Example 10-18 Database manager snapshot
db2 GET SNAPSHOT FOR DATABASE MANAGER
The lock snapshot (Example 10-19 on page 347) is useful in determining what
locks an application currently holds and the locks that other applications are
waiting on. The snapshot lists all applications on the system and the locks that
each of these applications holds. Each lock is given a unique identifier number,
and each application is given a unique identifier number.
346
The table snapshot (Example 10-20) contains information about the usage and
creation of all tables. This information is useful in determining how much work is
being run against a table and how much the table data changes. You can use
this information to decide how to lay out your data physically.
Example 10-20 Table snapshot
db2 GET SNAPSHOT FOR TABLES ON invent
The table space and buffer pool snapshots (Example 10-21) contain similar
information. The table space snapshot returns information regarding the layout of
the table space and the amount of space that is used. The buffer pool snapshot
contains information about the amount of space currently allocated for buffer
pools and the amount of space that is required when the database is next reset.
Both snapshots contain a summary of the way in which data is accessed from
the database. This access can be done from a buffer pool, directly from tables on
disk, or through a direct read or write for LOBs or LONG objects.
Example 10-21 Table space and buffer pool snapshots
db2 GET SNAPSHOT FOR TABLESPACES ON invent
db2 GET SNAPSHOT FOR BUFFERPOOLS ON invent
347
snapshot administrative views provide a simple means of accessing data for all
database partitions of the connected database.
Example 10-23 and Example 10-24 show how to get similar monitoring
information using the table functions and views as we did from Example 10-17 by
using the GET SNAPSHOT command. Example 10-23 demonstrates a query
that captures the snapshot of lock information for the currently connected
database. Example 10-24 is a query that captures a snapshot of lock information
about the SAMPLE database for the currently connected database partition.
Example 10-23 Sample snapshot table function
db2inst1@db2server:~> SELECT * FROM SYSIBMADM.SNAPLOCK
Example 10-24 Sample snapshot table function
db2inst1@db2server:~>SELECT * FROM TABLE(SNAP_GET_LOCK('invent',-1)) AS
SNAPLOCK
Table 10-3 lists the snapshot table functions, administrative views, and return
information that can be used to monitor your database system. All administrative
views belong to the SYSIBMADM schema.
Table 10-3 Common snapshot table functions and administrative views
Monitor
level
Administrative view
Information returned
DBM
SNAP_GET_DBM_V95
SNAPDBM
SNAP_GET_FCM
SNAPFCM
SNAP_GET_FCM_PART
SNAPFCM_PART
SNAP_GET_SWITCHES
SNAPSWITCHES
SNAP_GET_DBM_MEMOR
Y_POOL
SNAPDBM_MEMORY_
POOL
SNAP_GET_DB_V95
SNAPDB
SNAP_GET_DB_MEMORY_
POOL
SNAPDB_MEMORY_
POOL
SNAP_GET_HADR
SNAPHADR
DB
348
APP
SNAP_GET_APPL_V95
SNAPAPPL
SNAP_GET_APPL_INFO_
V95
SNAPAPPL_INFO
SNAP_GET_LOCKWAIT
SNAPLOCKWAIT
SNAP_GET_STMT
SNAPSTMT
SNAP_GET_AGENT
SNAPAGENT
SNAP_GET_SUBSECTION
SNAPSUBSECTION
SNAP_GET_AGENT_
MEMORY_POOL
SNAPAGENT_MEMORY
_POOL
SNAP_GET_TAB_V91
SNAPTAB
SNAP_GET_TAB_REORG
SNAPTAB_REORG
Lock
SNAP_GET_LOCK
SNAPLOCK
Table
space
SNAP_GET_TBSP_V91
SNAPTBSP
SNAP_GET_TBSP_PART_
V91
SNAPTBSP_PART
SNAP_GET_TBSP_
QUIESCER
SNAPTBSP_QUIESCER
SNAP_GET_CONTAINER_
V91
SNAPCONTAINER
SNAP_GET_TBSP_RANGE
SNAPTBSP_RANGE
SNAP_GET_BP_V95
SNAPBP
SNAP_GET_BP_PART
SNAPBP_PART
Dynamic
SQL
SNAP_GET_DYN_SQL_V95
SNAPDYN_SQL
DB
SNAP_GET_UTIL
SNAPUTIL
SNAP_GET_UTIL_PROGRE
SS
SNAPUTIL_PROGRESS
SNAP_GET_DETAILLOG_
V91
SNAPDETAILLOG
SNAP_GET_STORAGE_
PATHS
SNAPSTORAGE_PATHS
Table
Buffer
pool
349
SNAP_GET_DB_V95
SNAP_GET_DB_MEMORY_POOL
SNAP_GET_DETAILLOG_V91
SNAP_GET_HADR
SNAP_GET_STORAGE_PATHS
SNAP_GET_APPL_V95
SNAP_GET_APPL_INFO_V95
SNAP_GET_AGENT
SNAP_GET_AGENT_MEMORY_POOL
SNAP_GET_STMT
SNAP_GET_SUBSECTION
SNAP_GET_BP_V95
SNAP_GET_BP_PART
The database name parameter does not apply to the database manager-level
snapshot table functions; they have an optional parameter for database partition
number.
Event monitoring
Event monitors are used to monitor the performance of DB2 over a fixed period
of time. The information that can be captured by an event monitor is similar to the
snapshots, but in addition to snapshot-level information, event monitors also
examine transition events in the database, and they consider each event as an
object. Event monitors can capture information about DB2 events in the following
areas:
Statements: A statement event is recorded when an SQL statement ends. The
monitor records the statements start and stop time, CPU used, text of
dynamic SQL, the return code of the SQL statement, and other matrixes,
such as the fetch count.
350
351
Locking: The locking event monitor is new and can replace the deprecated
deadlock event monitor. A locking event is recorded upon detection of any of
the following event types, depending on the configuration: lock timeout,
deadlock, or lock wait beyond a specified duration. The information captured
from this event monitor focuses on the locks involved in the failure and the
applications that own them.
Unit of work: The unit of work is a new event monitoring type to DB2 9.7 and
replaces the deprecated transaction event monitor. A unit of work event is
recorded upon the completion of a unit of work. The unit of work event
records a variety of information, including attributes at the database level,
connection level, and unit of work level.
Event monitors are created with the CREATE EVENT MONITOR SQL
statement. Information about event monitors is stored in the system catalog
table, and it can be reused later.
Example 10-25 shows a sequence of statements that illustrate how to collect
Event Monitor information using commands.
Example 10-25 Working with event monitors
352
353
A configuration window appears where you can specify the general settings and
the values for Visual Explain to use for special registers when fetching explain
data.
354
Figure 10-4 shows an example of an access plan graph. To get the details,
right-click the desired graph element.
355
356
system algorithm and on the level of activity on the file system. And, the
performance of an SMS table space can be negatively affected. Therefore, SMS
table spaces are ideal for small databases that require low maintenance and
monitoring and that grow and shrink rapidly.
With DMS, the database manager can ensure that pages are physically
contiguous, because it bypasses operating system I/O and interfaces with the
disk directly. This approach can improve performance significantly. You can
create a DMS table space by using the MANAGED BY DATABASE clause in the
create table space definition.
The disadvantage is that a DMS table space requires more tuning and
administrative effort, because you must add more storage containers as the table
space fills with data. However, you can easily add new containers, drop, or
modify the size of existing containers. The database manager then automatically
rebalances existing data into all the containers belonging to the table space.
Therefore, DMS table spaces are ideal for performance-sensitive applications,
particularly applications that involve a large number of INSERT operations.
If a database is enabled for automatic storage (which is enabled by default),
there is a third option when creating a table space. You can specify automatic
management by using the MANAGED BY AUTOMATIC STORAGE clause in
the CREATE TABLESPACE definition. With this option, DB2 decides what type
of table space to create. There is no need to specify container details, because
DB2 assigns the containers and manages table space creation automatically.
For optimal performance, you must place large volume data and indexes within
DMS table spaces; if possible, split them to separate raw devices. Initially,
system temporary table spaces need to be of the SMS type. In an online
transaction processing (OLTP) environment, there is no need to create large
temporary objects to process SQL queries, so the SMS system temporary table
space is a good starting point. The easiest way to optimize your table spaces is
to use table spaces that are managed by automatic storage.
357
As shown in Figure 10-5, all data modifications are not only written to table space
containers, but they are also logged to ensure recoverability. Because every
INSERT, UPDATE, or DELETE statement is replicated in the transactional log,
the flushing speed of the logical log buffer can be crucial for the entire database
performance. To understand the importance of logical log placement, remember
that the time necessary to write data to disk depends on the physical data
distribution on disk. The more random reads or writes that are performed, the
more disk head movements are required, and therefore, the slower the writing
speed. Flushing the logical log buffer to disk is by its nature sequential, and other
operations must not interfere with it. Locating logical log files on separate devices
isolates them from other processes and ensures uninterrupted sequential writes.
To change logical log files to a new location, you must modify the
NEWLOGPATH database parameter, as shown in Example 10-27. The logs are
relocated to the new path on the next database activation (it takes time to create
the files).
Example 10-27 Relocation of logical logs
db2 UPDATE DB CFG FOR SAMPLE USING NEWLOGPATH /db2/logs
358
When creating a DMS table space with many containers, DB2 automatically
distributes the data across the containers in a round-robin fashion, similar to the
striping method that is available in disk arrays. To achieve the best possible
performance, place each table space container on a dedicated physical device.
For parallel asynchronous writes and reads from multiple devices, you must
adjust the number of database page cleaners (NUM_IO_CLEANERS) and I/O
servers (NUM_IOSERVERS). The best value for these two parameters depends
on the type of workload and available resources. You can start your configuration
with the following values:
NUM_IOSERVERS = Number of physical devices, not less than three and no
more than five times the number of CPUs
NUM_IO_CLEANERS = Number of CPUs
However, the most effective way to configure these parameters is to set them to
automatic and let DB2 manage them, as shown in Example 10-28.
Example 10-28 Updating I/O-related processes
db2 UPDATE DB CFG FOR sample USING NUM_IOSERVERS AUTOMATIC
db2 UPDATE DB CFG FOR sample USING NUM_IOCLEANERS AUTOMATIC
If there are a relatively small number of disks available, it can be difficult to keep
logical logs, data, indexes, system temporary table spaces (more important for
processing large queries in a data warehousing environment), backup files, or
the operating system paging file on separate physical devices. A compromise
solution is to have one large file system striped by a disk array (RAID device) and
create table spaces with only one container. The load balancing is shifted to the
hardware, and you do not have to worry about space utilization. If you want
parallel I/O operations on a single container, you must set the
DB2_PARALLEL_IO registry variable before starting the DB2 engine.
If this registry variable is set, and the prefetch size of the table is not
AUTOMATIC, the degree of parallelism of the table space is the prefetch size
divided by the extent size. If this registry variable is set, and the prefetch size of
the table space is AUTOMATIC, DB2 automatically calculates the prefetch size
of a table space. Table 10-4 on page 360 summarizes the available options and
how parallelism is calculated for each situation.
359
DB2_PARALLEL_IO setting
Parallelism is equal to
AUTOMATIC
Not set
Number of containers
AUTOMATIC
Table space ID
Number of containers x 6
AUTOMATIC
Number of containers x n
Not AUTOMATIC
Not set
Number of containers
Not AUTOMATIC
Table space ID
Not AUTOMATIC
360
At the start, you can create additional buffer pools for caching data and leave the
IBMDEFAULTBP for system catalogs.
Creating an extra buffer pool for system temporary data can also be valuable for
the system performance, especially in an OLTP environment where the
temporary objects are relatively small. Isolated temporary buffer pools are not
influenced by the current workload, so it takes less time to find free pages for
temporary structures, and it is likely that the modified pages will not be swapped
out to disk.
In a data warehousing environment, the operations on temporary table spaces
are considerably more intensive, so the buffer pools need to be larger, or
combined with other buffer pools if there is not enough memory in the system
(one pool for caching data and temporary operations).
Example 10-29 shows how to create buffer pools, assuming that an additional
table space named DATASPACE for storing data and indexes was already
created and that there is enough memory in the system. Use this example as a
starting buffer pool configuration for a 2 GB RAM system.
Example 10-29 Increasing buffer pools
connect to sample;
-- creating two buffer pools 256 MB and 64 MB
CREATE BUFFERPOOL data_bp IMMEDIATE SIZE 65536 pagesize 4k;
CREATE BUFFERPOOL temp_bp IMMEDIATE SIZE 16384 pagesize 4k;
-- changing size of the default buffer pool
ALTER BUFFERPOOL ibmdefaultbp IMMEDIATE SIZE 16384;
-- binding the tablespaces to buffer pools
ALTER TABLESPACE dataspace BUFFERPOOL data_bp;
ALTER TABLESPACE tempspace1 BUFFERPOOL temp_bp;
-- checking the results
SELECT
substr(bs.bpname,1,20) AS bpname
,bs.npages
,bs.pagesize
,substr(ts.tbspace,1,20) as TBSPACE
FROM syscat.bufferpools bs JOIN syscat.tablespaces ts ON
bs.bufferpoolid = ts. bufferpoolid;
The results:
BPNAME
-------------------IBMDEFAULTBP
IBMDEFAULTBP
NPAGES
----------16384
16384
PAGESIZE
----------4096
4096
TBSPACE
-------------------SYSCATSPACE
SYSTOOLSPACE
361
IBMDEFAULTBP
DATA_BP
TEMP_BP
16384
65536
16384
4096
4096
4096
USERSPACE1
DATASPACE
TEMPSPACE1
Although you can tune your buffer pools manually, using the Self-Tuning Memory
Manager is an easier and more effective way of tuning the buffer pools for
optimal performance. As we discussed in 9.6, Autonomics on page 306, the
Self-Tuning Memory Manager can tune database memory parameters and buffer
pools without any DBA intervention. The Self-Tuning Memory Manager works
with buffer pools of multiple page sizes and can easily trade memory between
the buffer pools as needed. You can turn on the Self-Tuning Memory Manager
for a specific buffer pool by issuing commands in Example 10-30.
Example 10-30 Self-tuning memory manager and tuning buffer pools
db2inst1@db2server:~ > db2 UPDATE DB CFG FOR DATABASE invent USING
SELF_TUNING_MEM ON
db2inst1@db2server:~ > db2 ALTER BUFFERPOOL bp32 SIZE AUTOMATIC
The first command in Example 10-30 turns the Self-Tuning Memory Manager on,
which is the default. The second command tells DB2 to automatically tune the
buffer pool BP32. You can tune individual buffer pools or all of the buffer pools
with the Self-Tuning Memory Manager.
The CHNGPGS_THRESH parameter specifies the percentage of changed
pages at which the asynchronous page cleaners will be started. Asynchronous
page cleaners write changed pages from the buffer pool to disk. The default
value for the parameter is 60%. When that threshold is reached, certain users
might experience a slower response time. Having larger buffer pools means
more modified pages in memory and more work to be performed by page
cleaners, as shown in Figure 10-6 on page 363.
362
For databases with a heavy update transaction workload, you can generally
ensure that there are enough clean pages in the buffer pool by setting the
parameter value to be equal-to or less-than the default value. A percentage
larger than the default can help the performance of your database if there are a
small number of extremely large tables. To change the default parameter, you
can use the following command:
db2 update db cfg for sample using CHNGPGS_THRESH
40
363
From a performance perspective, it is better to have a larger log file size because
of the cost of switching from one log to another log. When log archiving is
switched on, the log size also indicates the amount of data for archiving. In this
case, a larger log file size is not necessarily better, because a larger log file size
can increase the chance of failure or cause a delay in archiving or log shipping
scenarios. You need to balance the log size and the number of logs.
Example 10-31 allocates 400 MB of total log space.
Example 10-31 Resizing the transactional log
db2 UPDATE DB CFG FOR sample USING logfilsiz 5120
db2 UPDATE DB CFG FOR sample USING logprimary 20
Locking is the mechanism that the database manager uses to control concurrent
access to data in the database by multiple applications. Each database has its
own list of locks (a structure stored in memory that contains the locks held by all
applications concurrently connected to the database). The size of the lock list is
controlled by the LOCKLIST database parameter.
The default storage for LOCKLIST on Windows and UNIX is set to AUTOMATIC.
On 32-bit platforms, each lock requires 48 or 96 bytes of the lock list, depending
on whether other locks are held on the object. On 64-bit platforms, each lock
requires 64 or 128 bytes of the lock list, depending on whether other locks are
held on the object.
When this parameter is set to AUTOMATIC, it is enabled for self-tuning, which
allows the memory tuner to dynamically size the memory area controlled by this
parameter as the workload requirements change. Because the memory tuner
trades memory resources among separate memory consumers, there must be at
least two memory consumers enabled for self-tuning in order for self-tuning to be
active.
The value of LOCKLIST is tuned together with the MAXLOCKS parameter.
Therefore, disabling the self-tuning of the LOCKLIST parameter automatically
disables the self-tuning of the MAXLOCKS parameter. Enabling the self-tuning of
the LOCKLIST parameter automatically enables the self-tuning of the
MAXLOCKS parameter.
Automatic tuning of this configuration parameter only occurs when self-tuning
memory is enabled for the database (the SELF_TUNING_MEM database
configuration parameter is set to ON).
When the maximum number of lock requests has been reached, the database
manager replaces existing row-level locks with table locks (lock escalation). This
operation reduces the requirements for lock space, because transactions will
364
hold only one lock on the entire table instead of many locks on every row. Lock
escalation has a negative performance impact, because it reduces concurrency
on shared objects. Other transactions must wait until the transaction holding the
table lock commits or rolls back work. Setting LOCKLIST to AUTOMATIC avoids
this situation, because the lock list will increase synchronously to avoid lock
escalation or a lock list full situation.
To check the current usage of locks, use snapshots, as shown in Example 10-32.
Example 10-32 Invoking a snapshot for locks on the invent database
db2 get snapshot for locks on invent
The snapshot collects the requested information at the time that the command
was issued. Issuing the get snapshot command later can produce other results,
because, in the mean time, the applications might commit the transaction and
release the locks. To check lock escalation occurrences, look at the db2diag.log
file.
Log buffer
Log records are written to disk when one of the following situations occurs:
A transaction commits or a group of transactions commit, as defined by the
mincommit configuration parameter.
The log buffer is full.
Another internal database manager event occurs, which results in log records
being written to disk.
This log buffer size must also be less than or equal to the dbheap parameter.
Buffering the log records results in more efficient logging file I/O, because the log
records are written to disk less frequently and a greater quantity of log records
are written out at each time.
The default size for the log buffer is 256 4 KB pages. In most cases, the log
records are written to disk when one of the transactions issues a COMMIT or
when the log buffer is full. We recommend that you increase the size of this
buffer area if there is considerable read activity on a dedicated log disk or if there
is high disk utilization. Increasing the size of the log buffer can result in more
efficient I/O operations, especially when the buffer is flushed to disk. The log
records are written to disk less frequently, and more log records are written each
time.
When increasing the value of this parameter, also consider increasing the
DBHEAP parameter, because the log buffer area uses space that is controlled by
the DBHEAP parameter.
365
At a later time, you can use the get snapshot for applications command to
check the current usage of log space by transactions, as shown in
Example 10-33.
Example 10-33 Current usage of log space by applications
$db2 UPDATE MONITOR SWITCHES USING UOW ON
$db2 GET SNAPSHOT FOR APPLICATIONS ON sample
UOW log space used
UOW log space used
UOW log space used
(Bytes) =
(Bytes) =
(Bytes) =
478
21324
110865
Before running the application snapshot, switch on the Unit Of Work monitor. In
Example 10-33, at the time that the snapshot was issued, you can see that there
are only three applications running on the system. The first transaction uses 478
bytes of log space, the second transaction uses 21,324 bytes of log space, and
the last transaction uses 110,865 bytes of log space, which is roughly 28 pages
more than the default log buffer size. The snapshot gives only the current values
from the moment that the command was issued. To get more valuable
information about the usage of log space by transactions, run the snapshot
multiple times.
366
After many changes to table data, logically sequential data might reside on
non-sequential data pages, so that the database manager must perform
additional read operations to access data.
Additional read operations are also required if many rows have been deleted. In
this case, consider reorganizing the table to match the index and to reclaim
space. You can also reorganize the system catalog tables.
Because reorganizing a table usually takes more time than updating statistics,
you can execute the RUNSTATS command to refresh the current statistics for
your data and then rebind your applications. If refreshed statistics do not improve
performance, reorganization might help.
You can execute the RUNSTATS command against a table from the command
line. Example 10-34 shows how to execute the RUNSTATS command against our
sample inventory table.
Example 10-34 Executing RUNSTATS on the inventory table
db2inst1@db2server:~> db2 "RUNSTATS ON TABLE ADMIN.INVENTORY"
DB20000I The RUNSTATS command completed successfully.
It is also possible to update statistics using the Data Studio tool. Within the
Database Explorer View, connect to your database and drill down the database
object folders until you find the table for which you want to update the statistics.
In our example, we connect to the invent database, and then, we drop down the
invent database folder, the schema folder, the ADMIN schema folder, and the
Tables folder to a list of tables in the invent database in the ADMIN schema. To
pull up the table options, we right-click the INVENTORY table icon, as shown in
Figure 10-7 on page 368.
367
368
In certain scenarios, you might need more than current statistics on a table to
improve performance. The following factors can indicate if your database will
benefit from table reorganization:
A high volume of insert, update, and delete activity has occurred against
tables that are accessed by queries.
Significant changes have occurred in the performance of queries that use an
index with a high cluster ratio.
Executing the RUNSTATS command to refresh table statistics does not
improve performance.
Output from the REORGCHK command indicates a need for table
reorganization.
You can access the table reorganization option from the Data Studio by
right-clicking the table, as shown in Figure 10-7 on page 368. After selecting the
REORG Table option in the drop-down menu, the reorganization table wizard
opens in the main view. Figure 10-9 on page 370 shows the reorganization table
wizard. You can select the parameters for the REORG command, and select Run
to execute the command.
369
370
371
In any case, you can run the Configuration Advisor manually at any time against
a database to update the current configuration, regardless of the
DB2_ENABLE_AUTOCONFIG_DEFAULT setting. All recommendations are
based on the input that you provide and the system information that the
Configuration Advisor gathers. The generated recommendations can be applied
or simply displayed.
It is important to point out that the values that are suggested by the Configuration
Advisor are relevant for only one database per instance. If you want to use this
advisor on more than one database, each database must belong to a separate
instance.
The Configuration Advisor can be manually invoked with the AUTOCONFIGURE
command from the command line processor (CLP), either stand-alone or as part
of the CREATE DATABASE command. Additionally, it can also be run via a GUI
that is available in the Control Center, by calling the db2AutoConfig API, or
finally, by using the ADMIN_CMD stored procedure.
To invoke this wizard from the DB2 Control Center, expand the object tree until
you find the database that you want to tune. Select the icon for the database,
right-click, and select Configuration Advisor. Through several dialog windows,
the wizard collects the following information:
Based on the supplied answers, the wizard proposes configuration changes and
gives you the option to apply the recommendations or to save them as a task for
the Task Center for later execution, as shown in Figure 10-11 on page 373.
Figure 10-12 on page 374 shows the resulting recommendations.
372
373
You can also acquire the initial configuration recommendations through the
text-based AUTOCONFIGURE command (Example 10-36).
Example 10-36 Sample AUTOCONFIGURE command
db2 AUTOCONFIGURE USING mem_percent 40 tpm 300 num_local_apps 80 isolation CS
apply none
[...]
Current
and
Recommended
Values
for
Database Configuration
Description
Parameter
Current Value
Recommended Value
--------------------------------------------------------------------------------------Default application heap (4KB)
(APPLHEAPSZ) = 256
256
Catalog cache size (4KB)
(CATALOGCACHE_SZ) = 33
91
Changed pages threshold
(CHNGPGS_THRESH) = 80
80
Database heap (4KB)
(DBHEAP) = 1200
3552
Degree of parallelism
(DFT_DEGREE) = 1
1
Default tablespace extentsize (pages)
(DFT_EXTENT_SZ) = 32
32
[...]
374
Valid values
Default
value
Explanation
mem_percent
1100
25
Workload_type
simple, mixed, or
complex
mixed
num_stmts
11 000 000
10
Tpm
1200 000
60
admin_priority
performance,
recovery, or both
both
is_populated
yes, no
yes
num_local_apps
05 000
num_remote_apps
05 000
10
Isolation
RR
bp_resizeable
yes, no
yes
375
To execute the index advisor against a specific database, we first must specify
the workload that will be run against the database. From the command line, we
create a file that defines the workload. Example 10-37 shows the queries to run
against the SAMPLE database.
Example 10-37 The db2advis.in input file
--#SET FREQUENCY 100
SELECT COUNT(*) FROM EMPLOYEE;
SELECT * FROM EMPLOYEE WHERE LASTNAME='HAAS';
--#SET FREQUENCY 1
SELECT AVG(BONUS), AVG(SALARY) FROM EMPLOYEE
GROUP BY WORKDEPT ORDER BY WORKDEPT;
--#SET FREQUENCY 50
select FIRSTNME, lastname, deptname from department d, employee e where
d.deptno = e.workdept and e.lastName like 'W%'
We can then run the db2advis command and specify the db2advis.in file as
the workload input script. Example 10-38 shows the syntax and output to execute
the index advisor. For more options, run db2advis -h from the command line.
Example 10-38 Finding indexes for a particular query
db2inst1@db2server:~/ > db2advis -d sample -i db2advis.in -t 5
Using user id as default schema name. Use -n option to specify schema
execution started at timestamp 2009-09-18-02.38.35.785903
found [3] SQL statements from the input file
Recommending indexes...
total disk space needed for initial set [
0.000] MB
total disk space constrained to
[ 29.459] MB
Trying variations of the solution set.
0 indexes in current solution
[813.2663] timerons (without recommendations)
[813.2663] timerons (with current solution)
[0.00%] improvement
---- LIST OF RECOMMENDED INDEXES
-- ===========================
-- no indexes are recommended for this workload.
---- RECOMMENDED EXISTING INDEXES
-- ============================
-- RUNSTATS ON TABLE "DB2INST1"."EMPLOYEE" FOR SAMPLED DETAILED INDEX "DB2INST1"."XEMP2"
;
-- COMMIT WORK ;
---- UNUSED EXISTING INDEXES
376
-- ============================
-- DROP INDEX "DB2INST1"."XEMP2";
-- ===========================
--- ====ADVISOR DETAILED XML OUTPUT=============
-- ==(Benefits do not include clustering recommendations)==
---<?xml version="1.0"?>
--<design-advisor>
--<statement>
--<statementnum>0</statementnum>
--<statementtext>
-SELECT COUNT(*) FROM EMPLOYEE
--</statementtext>
--<objects>
--<identifier>
--<name>EMPLOYEE</name>
--<schema>DB2INST1</schema>
--</identifier>
--<identifier>
--<name>XEMP2</name>
--<schema>DB2INST1</schema>
--</identifier>
--</objects>
--<benefit>0.000000</benefit>
--<frequency>100</frequency>
--</statement>
--<statement>
--<statementnum>1</statementnum>
--<statementtext>
-SELECT * FROM EMPLOYEE WHERE LASTNAME='HAAS'
--</statementtext>
--<objects>
--<identifier>
--<name>EMPLOYEE</name>
--<schema>DB2INST1</schema>
--</identifier>
--</objects>
--<benefit>0.000000</benefit>
--<frequency>100</frequency>
--</statement>
--<statement>
--<statementnum>2</statementnum>
--<statementtext>
-SELECT AVG(BONUS), AVG(SALARY)
-- FROM EMPLOYEE
GROUP BY WORKDEPT ORDER BY
-- WORKDEPT
--</statementtext>
--<objects>
--<identifier>
--<name>EMPLOYEE</name>
--<schema>DB2INST1</schema>
--</identifier>
--<identifier>
377
--<name>XEMP2</name>
--<schema>DB2INST1</schema>
--</identifier>
--</objects>
--<benefit>0.000000</benefit>
--<frequency>1</frequency>
--</statement>
--</design-advisor>
-- ====ADVISOR DETAILED XML OUTPUT=============
-27 solutions were evaluated by the advisor
DB2 Workload Performance Advisor tool is finished.
378
379
380
11
Chapter 11.
XML
Compression
Partitioning
Multidimensional clustering (MDC)
Materialized query tables (MQT)
User-defined data types (UDT)
381
382
In order to query and update SQL and XML data, you can use SQL and XQuery
statements (SQL/XML: International Organization for Standardization (ISO)
standard ISO/International Electrotechnical Commission (IEC) 9075-14:2003).
Several operations are available to directly modify not only full documents, but
also parts or subtrees of XML documents without having to read, modify, and
reinsert them. Using the XQuery language, you can directly modify single values
and nodes within the XML document. XQuery is a fairly new, standardized query
language supporting path-based expressions. You can obtain more information
about XQuery at this Web site:
http://www.w3.org/TR/2007/REC-xquery-20070123/
With pureXML, applications are not only able to combine statements from both
languages to query SQL and XML data; you can express many queries in plain
XQuery, in SQL/XML, or XQuery with embedded SQL. In certain cases, one of
the options to express your query logic might be more intuitive than another
option. In general, you must choose the correct approach for querying XML data
on a case-by-case basis, taking the applications requirements and
characteristics into account. Example 11-1 on page 384 shows a simple XQuery
command.
383
STREET
-----------------------------5 Rosewood
25 EastCreek
25 EastCreek
1596 Baseline
1596 Baseline
223 NatureValley Road
CITY
-----------Toronto
Markham
Markham
Aurora
Toronto
Toronto
6 record(s) selected.
384
385
386
As of DB2 9.7, data compression had been extended to include all temporary
tables. Data compression for temporary tables reduces the amount of temporary
disk space that is required for large and complex queries, increasing query
performance.
Index objects and indexes on compressed temporary tables can also be
compressed to reduce storage costs. This compressions is especially useful for
large online transaction processing (OLTP) and data warehousing environments,
where it is common to have many large indexes. In both cases, index
compression can cause significant performance improvements in I/O-bound
environments and little or no performance decrements in CPU-bound systems.
If compression is enabled on a table with an XML column, the XML data that is
stored in the XDA object is also compressed. A separate compression dictionary
for the XML data is stored in the XDA object. XDA compression is not supported
for tables whose XML columns were created prior to this version; for such tables,
only the data object is compressed.
387
388
DB2 will automatically parallelize queries by splitting them up and sending them
directly to the nodes that hold the parts of the data that was requested.
These systems can also be made highly available when using shared storage. In
this case, two or more nodes can share the file systems holding the table spaces.
If an outage occurs, the surviving node can immediately access the failed nodes
table spaces and continue processing.
Remember you can use database partitioning efficiently in combination with table
partitioning and multidimensional clustering.
389
After creating a partitioned table, open INSERT, UPDATE, or LOAD into the
table, and DB2 automatically inserts rows into the appropriate table partition
according to the specified range. If the inserted data does not fit within the
ranges of any of the partitions, DB2 produces an error.
Traditionally, in order to archive older data, you moved data to the archived
locations, and you issued delete statements to remove the data from the current
table. This effort results in a full table scan to find all rows belonging to the
requested range. By using table partitioning, each table partition can be quickly
separated from the table using the DETACH PARTITION key words in the
ALTER TABLE statement. Example 11-5 describes the syntax for dropping a
particular table partition.
Example 11-5 Detaching a table partition
db2> ALTER TABLE sales DETACH PARTITION Q4_2009 INTO TABLE OldMonthSales
db2> COMMIT
db2> EXPORT OldMonthSales; DROP OldMonthSales
Physically, there is no impact to the system when using the ALTER table
DETACH PARTITION command. The command is extremely fast, because no
390
data movement takes place. As you can see in Figure 11-5, the table containing
the detached partition resides in the same table space as the original table
partition. The DETACH only changes catalog entries to let DB2 know that the
table partition is no longer a part of the main table. After the statement is
committed, the detached data is available from the new table name. From now
on, the table is a regular table, and you can perform actions on it.
The ATTACH command is similar to DETACH. For more details, visit the IBM
DB2 Information Center:
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
Remember that you can use table partitioning efficiently in combination with
database partitioning and multidimensional clustering.
391
Figure 11-7 on page 393 shows the data clustering according to the three
dimensions as defined in Example 11-6.
392
When organizing by dimensions, you can specify one or more table columns.
DB2 places all inserted rows with the same values for specific columns into a
physical location close to one another. This special physical location is called a
block. A block (extent) is a set of contiguous pages on disk, so access to these
records is sequential and accessed with minimal I/O operations. If an existing
block is filled, a new block is allocated. All blocks with the same combination of
dimension values are grouped into cells. With this internal organization, DB2 can
quickly find data along dimensions or find all rows for a specific combination of
dimension values.
393
Note that you can use MDC efficiently in combination with database partitioning
and table partitioning.
394
month, and year information and is encoded in a single date field. The date
dimensions are extracted from the date field of the transaction using built-in
functions. Other tables in this schema represent account information for
customers and customer information.
An MQT is created with the sum and count of sales for each level of the following
hierarchies:
Product
Location
Time, composed of year, month, day
Many queries can be satisfied from this stored aggregate data. The following
example shows how to create an MQT that computes the sum and the count of
sales along the product group and line dimensions; along the city, state, and
country dimension; and along the time dimension. It also includes several other
columns in its GROUP BY clause. Example 11-7 is an example of the CREATE
TABLE statement that will create an MQT table.
Example 11-7 Create MQT statement
CREATE TABLE dba.PG_SALESSUM
AS (
SELECT l.id AS prodline, pg.id AS pgroup,
loc.country, loc.state, loc.city,
l.name AS linename, pg.name AS pgname,
YEAR(pdate) AS year, MONTH(pdate) AS month,
t.status,
SUM(ti.amount) AS amount,
COUNT(*) AS count
FROM cube.transitem AS ti, cube.trans AS t,
cube.loc AS loc, cube.pgroup AS pg,
cube.prodline AS l
WHERE ti.transid = t.id
AND ti.pgid = pg.id
AND pg.lineid = l.id
AND t.locid = loc.id
AND YEAR(pdate) > 1990
GROUP BY l.id, pg.id, loc.country, loc.state, loc.city,
year(pdate), month(pdate), t.status, l.name, pg.name
)
DATA INITIALLY DEFERRED REFRESH DEFERRED;
REFRESH TABLE dba.PG_SALESSUM;
395
The cost of computing the answer using the MQT can be significantly less than
using a large base table, because a portion of the answer is already computed.
MQTs can reduce expensive joins, sorts, and the aggregation of base data.
The larger the base tables become, the greater the improvements in response
time can be, because the MQT grows more slowly than the base table. MQTs
can effectively eliminate overlapping work among queries by performing the
computation after the MQTs are built and refreshed, as well as reusing their
content for many queries.
Structured type
A structured type is a user-defined data type that has a well defined structure
consisting of existing built-in or user-defined data types. A structured type has
the attributes and methods defined. The attribute defines its data storage
properties, and methods define its behavior.
A structured type can be used as the type of a table, view, or column. When
used as a type for a table or view, that table or view is known as a typed table
or typed view. For typed tables and typed views, the names and data types of
the attributes of the structured type become the names and data types of the
columns of this typed table or typed view. Rows of the typed table or typed
view can be thought of as a representation of instances of the structured type.
When used as a data type for a column, the column contains values of that
396
structured type (or values of any of that types subtypes, as defined next).
Methods are used to retrieve or manipulate attributes of a structured column
object.
A structured type can be created using the CREATE TYPE statement. For
example, we can define a product and sku type, which can be used to create
typed tables, as shown in Example 11-8. Figure 11-8 shows its hierarchy.
Distinct type
ID
Order
use
Structure type
Typed table
Product_type
Product
Structure type
Typed table
SKU_type
SKU
We can also use this type as a type for a column, as shown in the last
statement of Example 11-8.
397
Reference type
A reference type is a companion type to a structured type. Similar to a distinct
type, a reference type is a scalar type that shares a common representation
with one of the built-in data types. This same representation is shared for all
types in the type hierarchy. The reference type representation is defined
when the root type of a type hierarchy is created. When using a reference
type, a structured type is specified as a parameter of the type. This parameter
is called the target type of the reference.
The target of a reference is always a row in a typed table or a typed view.
When a reference type is used, it can have a scope defined. The scope
identifies a table (called the target table) or view (called the target view) that
contains the target row of a reference value. The target table or view must
have the same type as the target type of the reference type. An instance of a
scoped reference type uniquely identifies a row in a typed table or typed view,
which is called the target row.
Array type
A user-defined array type is a data type that is defined as an array with
elements of another data type. Every ordinary array type has an index with
the data type of INTEGER and has a defined maximum cardinality. Every
associative array has an index with the data type of INTEGER or VARCHAR
and does not have a defined maximum cardinality.
Row type
A row type is a data type that is defined as an ordered sequence of named
fields, each with an associated data type, which effectively represents a row.
A row type can be used as the data type for variables and parameters in SQL
PL to provide simple manipulation of a row of data.
Cursor data type
A user-defined cursor type is a user-defined data type that is defined with the
keyword CURSOR and optionally with an associated row type. A user-defined
cursor type with an associated row type is a strongly typed cursor type;
otherwise, it is a weakly typed cursor type. A value of a user-defined cursor
type represents a reference to an underlying cursor.
398
Appendix A.
399
MYSQL example
DB2 function
DB2 example
Notes
AVG([DISTINCT]
expression)
mysql> SELECT a,
AVG(b)
FROM t1
GROUP BY a
Returns the
average set of
numbers
COUNT([DISTINCT]
expression,
expression,...)
mysql> SELECT a,
COUNT(b)
FROM t1
GROUP BY a
COUNT([DISTINCT|
ALL] expression).
Returns the
number of rows or
values in a set of
rows or values
MAX ([DISTINCT]
expression)
mysql> SELECT a,
MAX(b)
FROM t1
GROUP BY a
db2 "SELECT a,
MAX(b)
FROM t1
GROUP BY a"
Returns the
maximum value in
a set of values
MIN ([DISTINCT]
expression)
mysql> SELECT a,
MIN(b)
FROM t1
GROUP BY a
Returns the
minimum value in a
set of values
STDDEV (expression) /
STDDEV_POP
(expression)
mysql> SELECT
STDDEV (a),a
FROM t1
GROUP BY a
STDDEV ([DISTINCT |
ALL] expression)
Returns the
standard deviation
(/n) of a set of
numbers
SUM([DISTINCT]
expression)
mysql> SELECT a,
SUM(b)
FROM t1
GROUP BY a
SUM([DISTINCT | ALL]
expression)
VAR_POP(expression) /
VARIANCE(expression)
mysql> SELECT
VAR_POP(a)
FROM t1
GROUP BY a
VARIANCE ([DISTINCT |
ALL] expression)
Returns the
variance of a set of
numbers
BIT_AND (expression)
This function is an
extension to SQL standards
mysql> SELECT
BIT_AND(a), a
FROM t1
GROUP BY a
No equivalent function.
Implement using
user-defined function
(UDF).
BIT_OR (expression)
This function is an
extension to SQL standards
mysql> SELECT
BIT_OR(a), a
FROM t1
GROUP BY a
No equivalent function.
Implement using UDF.
BIT_XOR (expression)
This function is an
extension to SQL standards
mysql> SELECT
BIT_XOR(a), a
FROM t1
GROUP BY a
No equivalent function.
Implement using UDF.
400
GROUP_CONCAT(expr
ession)
This function is an
extension to SQL
standards
mysql> SELECT a,
GROUP_CONCAT(b)
FROM t1 group by a
No equivalent function.
Implement using UDF.
STD
This function is an
extension to SQL standards
mysql> SELECT
STD(a),a
FROM t1
GROUP BY a
STDDEV ([DISTINCT |
ALL] expression)
db2 "SELECT
stddev(a), a
FROM t1
GROUP BY a"
Returns the
standard deviation
(/n) of a set of
numbers
Not Available
CORRELATION
(expression, expression)
Returns the
coefficient of
correlation of a set
of number pairs
Not Available
COVARIANCE
(expression, expression)
db2 SELECT
COVARIANCE(a, b)
FROM t1
WHERE c = 52
Returns the
(population)
covariance of a set
of number pairs
Not Available
GROUPING(expression)
Returns a
concatenated
variable for all
values
GROUP BY on alias
mysql> SELECT a as x
FROM a
GROUP BY x;
GROUP BY on position
mysql> SELECT a
FROM t1
GROUP BY 1
HAVING on alias
mysql> SELECT a as x
FROM t1
GROUP BY a
HAVING x > 0
Returns a value
that indicates
whether a row
returned in a
GROUP BY answer
set is a row
generated by a
grouping set that
excludes the
column
represented by
expression
401
MYSQL example
DB2 function
DB2 example
Notes
ASCII(string)
ASCII(string)
Returns ASCII
code value
BIN(integer)
No equivalent
function.
Alternative use
UDF
BIT_LENGTH(
string)
LENGTH(string,
CODEUNITS16|
CODEUNITS32|
OCTETS)
db2 "VALUES
length('Hello')*8 "
mysql> SELECT
CHAR_LENGTH('Orange');
+-----------------------+
| CHAR_LENGTH('Orange') |
+-----------------------+
|
6|
+-----------------------+
1 row in set (0.00 sec)
CHARACTER_L
ENGTH(string,
CODEUNITS16|
CODEUNITS32|
OCTETS),/
CHAR_LENGT
H(string,
CODEUNITS16|
CODEUNITS32|
OCTETS)
CHR(string)
1
----------97
1 record(s) selected.
Returns the binary
value of the string
1
----------40
1 record(s) selected.
CHAR_LENGT
H(string) /
CHARACTER_
LENGTH(string
)
CHAR(int,
[USING
character set])
402
1
----------6
1 record(s) selected.
1
a
1 record(s) selected.
Result depends on
the
encoding scheme
used for
character data:
single byte,
double byte,
UTF-8
Returns the
number of
bytes for
expression. For
double-byte
character set
(DBCS), the
number of
DBCS characters
is
returned
Returns the
character that has
the ASCII code
value specified by
the argument
CONCAT_WS(
separator,
string,
string,)
Use || to
implement
CONCAT(list).
mysql> SELECT
CONCAT(firstname, ' ', lastname) as
FULLNAME from owners where id =
501;
+----------------+
| FULLNAME
|
+----------------+
| Angela Carlson |
+----------------+
1 row in set (0.00 sec)
Use
CONCAT(string,
string) or || to
implement
CONCAT(list).
ELT(int,str1,str
2,str3,...)
No equivalent
function.
Alternative use
CASE
expression or
UDF.
EXPORT_SET(
bit, onString,
offString,
length)
mysql> SELECT
EXPORT_SET(12,'ON','OFF','|',4);
+---------------------------------+
| EXPORT_SET(12,'ON','OFF','|',4) |
+---------------------------------+
| OFF|OFF|ON|ON
|
+---------------------------------+
1 row in set (0.00 sec)
No equivalent
function.
Alternative use
UDF
Returns a string
representation for
each of the bit
values of a binary
value
FIELD(string,st
ring1,string2,str
ing3,...))
No equivalent
function.
Alternative use
CASE
expression or
UDF
Returns the
position of the
matching string
FIND_IN_SET(
expr)
mysql> SELECT
FIND_IN_SET('Doe','John,Doe,Stas
ie,Smith') as POSITION;
+----------+
| POSITION |
+----------+
|
2|
+----------+
1 row in set (0.01 sec))
No equivalent
function.
Implement using
UDF.
Returns the
position in the set
of the matching
string
Returns the
concatenation of
string arguments
with separator
FULLNAME
------------------------------------------------------------Angela-Carlson-acarlson
1 record(s) selected.
CONCAT(strin
g, string,)
Returns the
concatenation of
string arguments
FULLNAME
----------------------------------------Angela Carlson
1 record(s) selected.
403
FORMAT(doub
le, integer)
FORMAT:
select
format(1234.5555, 2)
returns 1,234.56
No equivalent
function.
Implement using
UDF.
Returns the
rounded string
HEX(string)
HEX(string)
db2 "VALUES
HEX(00000255)"
Returns a
hexadecimal
representation of a
value as a
character string
1
-------FF000000
1 record(s) selected.
INSERT(string,
position, length,
substring)
INSERT(string,
position, length,
substring)
db2 "VALUES
INSERT('original', 2, 2, 'NEW')"
1
----------oNEWginal
1 record(s) selected.
Inserts a string
into an existing
string
INSTR
(substring,
string)
/LOCATE(subs
tring, string,
[position])
/POSITION(su
bstring, string)
LOCATE(substri
ng, string, [start],
[CODEUNITS16
|
CODEUNITS32|
OCTETS])
Returns the
starting position of
the first
occurrence of one
string within
another string
LCASE(string) /
LOWER(string)
LCASE(string)
Returns a string in
which all
characters have
been converted to
lowercase
characters
LEFT(string1,
strin2)
LEFT(string1,
integer)
db2 "SELECT
LEFT('Inventory', 6) FROM
SYSIBM.SYSDUMMY1"
1
--------Invent
1 record(s) selected.
Returns a string
consisting of the
leftmost String up
to the integer
position
LENGTH(string)
/
OCTET_LENGT
H(string)
LENGTH(string
)/
OCTECT_LEN
TH(string)
LOAD_FILE(dir
String)
404
LPAD(string,
length,
substring) /
RDAP( string,
length,
substring)
mysql> SELECT
LPAD('TEST',6,'!!!');
+----------------------+
| LPAD('TEST',6,'!!!') |
+----------------------+
| !!TEST
|
+----------------------+
1 row in set (0.00 sec)
No equivalent
function.
Implement
using UDF.
LTRIM(string)
LTRIM(string)
Removes blanks
from the beginning
of
string-expression
MAKE_SET(bit
s, string,
string,....)
No equivalent
function.
Alternative use
UDF
Returns a set of
strings based on
the bit
OCT(expressio
n)
No equivalent
function.
Alternative use
UDF
QUOTE(string)
SELECT with ||
REPEAT(string,
integer)
1
---------------------'Angela'
1 record(s) selected.
REPEAT(string
, integer)
db2 "VALUES
REPEAT('REPEAT THIS ', 5)"
Returns the
repeated string N
times
1
------REPEAT THIS REPEAT THIS
REPEAT THIS REPEAT THIS
REPEAT THIS
1 record(s) selected.
405
REPLACE(strin
g1, string2,
string3)
REVERSE(stri
ng1, S)
RIGHT(string,
length)
RTRIM(string)
Returns as string
with all
occurrences of
string2 in string1
with string3
Returns the
reverse order of
the string.
Implement UDF.
For the complete
code of the
example, refer to
IBM DB2 SQL
Reference,
Volume 1, V8,
SC10-4249.
RIGHT(string,
length)
db2 "VALUES
RIGHT('Inventory', 4 ) "
1
--------tory
1 record(s) selected.
Returns a string
consisting of the
rightmost String
starting from the
integer position
RTRIM(string)
Removes blanks
from the end of
string
SOUNDEX(stri
ng)
mysql> SELECT
SOUNDEX('apple');
+------------------+
| SOUNDEX('apple') |
+------------------+
| A140
|
+------------------+
1 row in set (0.00 sec)
SOUNDEX(strin
g)
db2 "VALUES
SOUNDEX('apple')"
1
---A140
1 record(s) selected.
Returns a
4-character code
representing the
sound of the words
in the argument.
MySQL: String1
SOUNDS LIKE
string2 can be
ported to
SOUNDEX(String
1) =
SOUNDEX(string
2) in DB2
SPACE(expres
sion)
SPACE(express
ion)
Returns a
character string
consisting of
blanks with length
specified by the
second argument
406
REPLACE(strin
g1, string2,
string3)
1
------PEAR
1 record(s) selected.
STRCMP(strin
g, string)
CASE
SUBSTRING_I
NDEX()
mysql> SELECT
SUBSTRING_INDEX('This is a
test...', 't', 2);
+---------------------------------------------+
| SUBSTRING_INDEX('This is a
test...', 't', 2) |
+---------------------------------------------+
| This is a test |
+---------------------------------------------+
1 row in set (0.00 sec)
No equivalent
function.
Implement
using UDF.
SUBSTRING(st
ring, position,
length) / MID
(string,
position,
length) /
SUBSTR(string
, position,
[length])
SUBSTR(string,
position, length)
db2 "VALUES('abcdef', 2, 3)
1
--bcd
TRIM([Both |
Leading |
trailing
[substring]
FROM] string)
TRIM([Both |
Leading | trailing
[substring]
FROM] string)
UCASE(string)
/ UPPER
(String)
UCASE(string) /
UPPER (String)
UNHEX()
mysql> SELECT
UNHEX('546F646179');
+---------------------+
| UNHEX('546F646179') |
+---------------------+
| Today
|
+---------------------+
1 row in set (0.00 sec)
No equivalent
function.
Alternative use
UDF
Returns -1 if the
first string is
smaller, 0 if the
strings are the
same length, 1 if
the second string
is smaller
Returns a
substring of a
string
1 record(s) selected.
Removes blanks
or occurrences of
another specified
character from the
end or the
beginning of a
string expression
1 record(s) selected.
db2 "VALUES UPPER('jobs')"
1
---JOBS
1 record(s) selected.
Returns a string in
which all
characters have
been converted to
uppercase
characters
Converts
hexadecimal digits
to a character
string
407
MySQL example
DB2 command
DB2 example
ABS(expression)
mysql> SELECT
ABS(-51234);
ABS(expression)
ACOS(expression)
ACOS(expression)
ASIN(expression)
ASIN(expression)
ATAN(expression,
expression), ATAN2(
expression, expression)
mysql> SELECT
ATAN2(1,-1);
ATAN(expression),
ATAN2(expression,
expression),
CEIL(expression),
CEILING(expression)
mysql> SELECT
CEILING(3.35);
CEIL(expression),
CEILING(expression)
COS(expression)
COS(expression)
COT(expression)
COT(expression)
DEGREES(expression)
mysql> SELECT
DEGREES(3);
DEGREES(expression)
EXP(expression)
EXP(expression)
FLOOR(expression)
mysql> SELECT
FLOOR(3.35);
FLOOR(expression)
LN(expression)
mysql> SELECT
LN(100);
LN(expression)
LOG10(expression)
mysql> SELECT
LOG10(100);
LOG10(expression)
LOG(expression,
[expression])
mysql> SELECT
LOG(100);
LOG(expression)
MOD(expression,
expression)
mysql> SELECT
MOD(125, 50);
MOD(expression,
expression)
POW(expression,
expression),
POWER(expression,
expression)
mysql> SELECT
POWER(5, 2);
POWER(expression,
expression)
RADIANS(expression)
mysql> SELECT
RADIANS(180);
RADIANS(expression)
db2 "VALUES
RADIANS(180)"
RAND([expression])
RAND([expression])
ROUND(expression,
expression)
mysql> SELECT
ROUND(873.726, 1);
ROUND(expression,
expression)
db2 "VALUES
ROUND(873.726, 1)"
SIGN(expression)
mysql> SELECT
SIGN(6453);
SIGN(expression)
408
MySQL command
MySQL example
DB2 command
DB2 example
SIN(expression)
SIN(expression)
SQRT(expression)
mysql> SELECT
SQRT(25);
SQRT(expression)
TAN(expression)
TAN(expression)
TRUNCATE(expression,
expression)
mysql> SELECT
TRUNCATE(873.726,2);
TRUNCATE(expression,
expression)
db2 "VALUES
TRUNCATE(873.726,2)"
MySQL example
DB2 command
DB2 example
ADDDATE(),
DATE_ADD(),DATE_SUB(),
SUBDATE()
mysql> SELECT
DATE_ADD('2009-8-31',
INTERVAL 15 DAY);
DATE + expression
CURDATE(),
CURRENT_DATE(),
CURRENT_DATE
mysql> SELECT
CURDATE();
CURRENT DATE
CURRENT_TIME(),
CURRENT_TIME,
CURTIME()
mysql> SELECT
CURRENT_TIME();
CURRENT TIME
DAYOFMONTH
(expression),
DAY(expression)
mysql> SELECT
DAYOFMONTH('2009-0
8-31 05:06:00');
Day(expression)
mysql> SELECT
EXTRACT(YEAR_MON
TH from '2009-08-31
05:06:00');
HOUR(expression)
mysql> SELECT
HOUR('2009-08-31
05:06:00');
MIDNIGHT_SECONDS
(expression)/60/60
db2 "VALUES
MIDNIGHT_SECONDS('2009-0831 05:06:00')/60/60"
SEC_TO_TIME(arg INTEGER)
RETURNS TIME CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC RETURN
TIME('00:00:00') + (arg / 3600)
HOURS + MOD(arg / 60, 60)
MINUTES + MOD(arg, 3600)
SECONDS "
SEC_TO_TIME()
STR_TO_DATE(expression,
format)
mysql> SELECT
STR_TO_DATE('15,03,2
009','%d,%m,%Y');
db2 "VALUES
DATE(TO_DATE('15,3,2009','DD,
MM,YYYY'))"
WEEK(expression)
WEEKOFYEAR
(expression, mode)
mysql> SELECT
week('2009-08-31
05:06:00');
WEEK(expression)
409
MySQL command
MySQL example
DB2 command
DB2 example
WEEKDAY(expression)
mysql> SELECT
weekday('2009-08-31
05:06:00');
DAYOFWEEK(expression)
DB2
Comments
MOD
CASE
SELECT CASE
WHEN 1<> 1
THEN x
ELSE y
END
CASE
BETWEEN in SELECT
CASE
No equivalent
BIT_COUNT
ENCRYPT
ENCRYPT
FIELD
CASE
GREATEST
FnGratst
IF
CASE
410
IN on numbers in SELECT
CASE
IN on strings in SELECT
CASE
LOCATE as INSTR
LOCATE
INTERVAL
CASE
LAST_INSERT_ID
IDENTITY_VAL_LOCAL
LEAST
FnLeastN
LIKE in SELECT
LOG(m,n)
LOG(m)/LOG(n)
NOT in SELECT
CASE
CASE
CASE
PASSWORD
ENCRYPT
For encryption
POW
POWER
REGEXP in SELECT
No equivalent
VERSION
db2level
411
412
Appendix B.
413
414
1
----------2
-------------------------------------------------------------------------values bitand(1038,78);
--------------------------------------------------1
----------14
415
2
----------------------12.34
-12.34
120,034.56
123,400,123,456,789.00
3
----------------------12.
-12.
120,034.
123,400,123,456,789.
4 record(s) selected.
(C1
(I1
(C1
(I1
VarChar(4000),
Integer,
VarChar(4000),
Integer,
N
N
N
N
integer,
integer,
integer,
integer,
C2
C2
C2
C2
VarChar(4000))
Varchar(4000))
Varchar(4000))
Varchar(4000))
416
substr(C1 ||
repeat(C2,((sign(N-length(C1))+1)/2)*(N-length(C1)+length(C2))/(length(C2)+1-sign(length(C2)))),1,N)
;
UDF RPAD with the third parameter omitted is shown in Example B-7.
Example: B-7 RPAD omitting the third parameter
CREATE FUNCTION RPAD (C1 VarChar(4000), N integer)
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC RPADVarCharParm2
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
RPAD(C1,N,' ')
;
Running the RPAD function gives you the results that are shown in Example B-8.
Example: B-8 Results of RPAD omitting the third parameter
SELECT char(rpad('ABCDE',15) || 'X',50) FROM SYSIBM.SYSDUMMY1;
--------------------------------------------------1
-------------------------------------------------ABCDE
X
417
1 record(s) selected.
Function RPAD allows a set of different input arguments. Example B-9 shows two
more RPAD UDFs.
Example: B-9 RPAD with first parameter as integer, 2, and 3 parameters
CREATE FUNCTION RPAD (I1 Integer, N integer, C2 Varchar(4000))
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC RPADIntParm3
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
RPAD(rtrim(char(I1)),N,C2)
;
CREATE FUNCTION RPAD (I1 Integer, N integer)
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC RPADIntParm2
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
RPAD(rtrim(char(I1)),N,' ')
;
418
1
-------------------------------------------------927
X
1 record(s) selected.
The counterpart for RPAD are the LPAD functions, which are shown in
Example B-11.
Example: B-11 LPAD: CREATE FUNCTION and sample usage
CREATE FUNCTION LPAD (C1 VarChar(4000), N integer, C2 VarChar(4000))
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC LPADBase
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
CASE
WHEN N > length(C1) THEN
substr(repeat(C2,(N-length(C1)+length(C2))/(length(C2)+1-sign(length(C2)))),1,N-length(C1)) || C1
ELSE substr(C1,1,N)
END
;
419
1 record(s) selected.
Because RPAD allows LPAD a different number and data type for input
arguments, Example B-13 shows LPAD without the third parameter.
Example: B-13 LPAD: Omitting the third parameter
CREATE FUNCTION LPAD (C1 VarChar(4000), N integer)
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC LPADParm2
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LPAD(C1,N,' ')
;
The results of Example B-13 must look like those results in Example B-14.
Example: B-14 Result of LPAD: Omitting the third parameter
SELECT char(lpad('ABCDE',15),20) FROM SYSIBM.SYSDUMMY1;
--------------------------------------------------1
-------------------ABCDE
1 record(s) selected.
Two more LPAD UDFs with different characteristics are shown in Example B-15.
Example: B-15 LPAD: The first parameter is integer, 2, and 3 parameters
CREATE FUNCTION LPAD (I1 Integer, N integer, C2 Varchar(4000))
420
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC LPADIntParm3
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LPAD(rtrim(char(I1)),N,C2)
;
-------------------------------------------------------------------------CREATE FUNCTION LPAD (I1 Integer, N integer)
RETURNS VARCHAR(4000)
LANGUAGE SQL
SPECIFIC LPADIntParm2
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LPAD(rtrim(char(I1)),N,' ')
;
421
422
423
THEN CASE
WHEN P3 >= P5 THEN P3
ELSE
P5
END
ELSE CASE
WHEN P4 >= P5 THEN P4
ELSE
P5
END
END
END
ELSE CASE
WHEN P2 >= P3
THEN CASE
WHEN P2 >= P4
THEN CASE
WHEN P2 >= P5 THEN P2
ELSE
P5
END
ELSE CASE
WHEN P4 >= P5 THEN P4
ELSE
P5
END
END
ELSE CASE
WHEN P3 >= P4
THEN CASE
WHEN P3 >= P5 THEN P3
ELSE
P5
END
ELSE CASE
WHEN P4 >= P5 THEN P4
ELSE
P5
END
END
END
END
;
----------------------------------------------------- GREATEST function with six parameters
--------------------------------------------------------------------------CREATE FUNCTION GREATEST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4 VarChar(254)
, P5 VarChar(254), P6 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC GREATESTOracle6
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
GREATEST(GREATEST(P1,P2,P3),GREATEST(P4,P5,P6))
;
----------------------------------------------------- GREATEST function with seven parameters
--------------------------------------------------------------------------CREATE FUNCTION GREATEST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4 VarChar(254)
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC GREATESTOracle7
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
424
GREATEST(GREATEST(P1,P2,P3,P4),GREATEST(P5,P6,P7))
;
----------------------------------------------------- GREATEST function with eight parameters
--------------------------------------------------------------------------CREATE FUNCTION GREATEST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254),
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254),
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC GREATESTOracle8
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
GREATEST(GREATEST(P1,P2,P3,P4),GREATEST(P5,P6,P7,P8))
;
----------------------------------------------------- GREATEST function with nine parameters
--------------------------------------------------------------------------CREATE FUNCTION GREATEST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254),
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254),
, P9 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC GREATESTOracle9
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
GREATEST(GREATEST(P1,P2,P3,P4,P5),GREATEST(P6,P7,P8,P9))
;
----------------------------------------------------- GREATEST function with ten parameters
--------------------------------------------------------------------------CREATE FUNCTION GREATEST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254),
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254),
, P9 VarChar(254),P10 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC GREATESTOracle10
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
GREATEST(GREATEST(P1,P2,P3,P4,P5),GREATEST(P6,P7,P8,P9,P10))
;
---------------------------------------------------
P4 VarChar(254)
P8 VarChar(254))
P4 VarChar(254)
P8 VarChar(254)
P4 VarChar(254)
P8 VarChar(254)
425
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh'),20) FROM sysibm.sysdummy1;
--------------------------------------------------1
-------------------defgh
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh','endof...'),20) FROM sysibm.sysdummy1;
--------------------------------------------------1
-------------------endof...
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh','endof...','add on'),20) FROM sysibm.sysdummy1;
--------------------------------------------------1
-------------------endof...
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh','endof...','add on','extra'),20) FROM
sysibm.sysdummy1;
--------------------------------------------------1
-------------------extra
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh','endof...','add on','extra','a bit of'),20) FROM
sysibm.sysdummy1;
--------------------------------------------------1
-------------------extra
1 record(s) selected.
-------------------------------------------------------------------------SELECT char(greatest('abcdefg','defgh','abcfgh','endof...','add on','extra','a bit of','more'),20)
FROM sysibm.sysdummy1;
--------------------------------------------------1
-------------------more
1 record(s) selected.
--------------------------------------------------------------------------
426
427
428
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle5
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
CASE
WHEN P1 <= P2
THEN CASE
WHEN P1 <= P3
THEN CASE
WHEN P1 <= P4
THEN CASE
WHEN P1 <= P5 THEN P1
ELSE
P5
END
ELSE CASE
WHEN P4 <= P5 THEN P4
ELSE
P5
END
END
ELSE CASE
WHEN P3 <= P4
THEN CASE
WHEN P3 <= P5 THEN P3
ELSE
P5
END
ELSE CASE
WHEN P4 <= P5 THEN P4
ELSE
P5
END
END
END
ELSE CASE
WHEN P2 <= P3
THEN CASE
WHEN P2 <= P4
THEN CASE
WHEN P2 <= P5 THEN P2
ELSE
P5
END
ELSE CASE
WHEN P4 <= P5 THEN P4
ELSE
P5
END
END
ELSE CASE
WHEN P3 <= P4
THEN CASE
WHEN P3 <= P5 THEN P3
ELSE
P5
END
ELSE CASE
WHEN P4 <= P5 THEN P4
ELSE
P5
END
END
END
END
;
----------------------------------------------------- LEAST function with six parameters
--------------------------------------------------------------------------CREATE FUNCTION LEAST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4 VarChar(254)
429
, P5 VarChar(254), P6 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle6
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LEAST(LEAST(P1,P2,P3),LEAST(P4,P5,P6))
;
----------------------------------------------------- LEAST function with seven parameters
--------------------------------------------------------------------------CREATE FUNCTION LEAST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle7
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LEAST(LEAST(P1,P2,P3,P4),LEAST(P5,P6,P7))
;
----------------------------------------------------- LEAST function with eight parameters
--------------------------------------------------------------------------CREATE FUNCTION LEAST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254), P8
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle8
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LEAST(LEAST(P1,P2,P3,P4),LEAST(P5,P6,P7,P8))
;
----------------------------------------------------- LEAST function with nine parameters
--------------------------------------------------------------------------CREATE FUNCTION LEAST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254), P8
, P9 VarChar(254))
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle9
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LEAST(LEAST(P1,P2,P3,P4,P5),LEAST(P6,P7,P8,P9))
;
----------------------------------------------------- LEAST function with ten parameters
--------------------------------------------------------------------------CREATE FUNCTION LEAST (P1 VarChar(254), P2 VarChar(254), P3 VarChar(254), P4
, P5 VarChar(254), P6 VarChar(254), P7 VarChar(254), P8
, P9 VarChar(254),P10 VarChar(254))
430
VarChar(254)
VarChar(254)
VarChar(254))
VarChar(254)
VarChar(254)
VarChar(254)
VarChar(254)
RETURNS VarChar(254)
LANGUAGE SQL
SPECIFIC LEASTOracle10
DETERMINISTIC
CONTAINS SQL
NO EXTERNAL ACTION
RETURN
LEAST(LEAST(P1,P2,P3,P4,P5),LEAST(P6,P7,P8,P9,P10))
;
431
432
set temp=substr(temp,locate(delimit,temp)+1);
set num=num-1;
end while;
if(n>0) then
return substr(In,1,pos);
else
return substr(In,pos+1);
end if;
end
433
434
Related publications
The publications listed in this section are considered particularly suitable for a
more detailed discussion of the topics that are covered in this book.
Other publications
These publications are also relevant as further information sources:
IEEE Standard for Software Test Documentation (829-1998),
ISBN 0-7381-1444-8
Understanding DB2, Learning Visually with Examples, Second Edition,
ISBN-13:978-0-13-158018-3
Installing IBM Data Server Clients, GC27-2454-00
Installing DB2 Servers, GC27-2455-00
Getting Started with DB2 Installation and Administration on Linux and
Windows, GI11-9411-00
Database Administration Concepts and Configuration Reference,
SC27-2442-00
Database Monitoring Guide and Reference, SC27-2458-00
Database Security Guide, SC27-2443-00
Partitioning and Clustering Guide, SC27-2453-00
Troubleshooting and Tuning Database Performance, SC27-2461-00
435
Online resources
These Web sites are also relevant as further information sources:
DB2
Database Management
http://www.ibm.com/software/data/management/
DB2
http://www.ibm.com/software/data/db2/
436
DB2 Express-C
http://www.ibm.com/software/data/db2/express/
http://www.ibm.com/developerworks/forums/forum.jspa?forumID=805
IBM DB2 Database for Linux, UNIX, and Windows Information Center
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
IBM developerWorks
http://www.ibm.com/developerworks/
IBM PartnerWorld
http://www.ibm.com/partnerworld
Related publications
437
Leverage MySQL skills to learn DB2 Express: DB2 versus MySQL backup
and recovery
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0606tham/
Leverage MySQL skills to learn DB2 Express, Part 3: DB2 versus MySQL
graphical user interface
http://www.ibm.com/developerworks/db2/library/techarticle/dm-0608tham/
Leverage MySQL skills to learn DB2 Express, Part 4: DB2 versus MySQL
data movement
http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0610tham/
MySQL
MySQL home page
http://www.mysql.com/
PHP MyAdmin
http://www.phpmyadmin.net/home_page/index.php
Others
VMware
http://www.vmware.com/
PHP
http://www.php.net/
438
APACHE
http://www.apache.org/
Perl
http://www.perl.org/
Ruby
http://www.ruby-lang.org/en/
UnixODBC
http://www.unixodbc.org/
Related publications
439
440
Index
Symbols
.FRM file 124
.MRG file 124
.MYD file 124
.MYI file 124
Numerics
2-tier 25
32-bit 4
64-bit 4
A
access control 178
access package 29
access path 17
access plan 28, 354
access right 167
accessctrl authority 184, 187
accesslist table 268
account detail 79
ActiveX Data Object 32
address space 10
administrative interface 37
administrative views 348
ADO 32
ADO.NET 32
aggregate function 221
all privileges privilege 188
alter privilege 188
alterin privilege 188
AMD workstation 4
ANSI mode 210
ANSI92 standard 217
applaccess table 268
applet 30
application architecture 56, 62
application assessment 62
application client 26
application data 68
application developer 2
application development 92
application environment 56
application flow 76
application function 56
application interface 37, 56
application porting 56
application profile 56
application server 26
application user account 177
archive storage engine 47
ASNCLP command 297
assignment 213
async i/o 14
audit statement 183
authentication 181
autocommit 274
automated backup 6
automatic storage 127
autonomic computing daemon 10
B
background process 10
backup compression 5
bash shell 147
batch application 25
big integer 118
binary data 175
binary large object 175
bind command 28
bind commands 277
bindadd privilege 187
binding 27
blackhole storage engine 47
BLOB 175
block device 14
buffer pool 15, 164
buffers 39
built-in data 116
built-in function 160
business intelligence 7, 57
C
C client library 38
C/C++ 62
cache 39
441
442
D
dascrt 103
dasupdt 101
data compression 2
data definition language 67
data dictionary 41
data extraction 144
data file 41
data manipulation language 71
data movement utility 279
data page 14
data porting 56
data recovery 279
data recovery module 57
data server 2, 91
data type mapping 115
data types 175
data warehouse 58
dataaccess authority 184, 187
database 11
database activity 91
database administration 38, 103
database administrator 2, 59
database architecture 62
database authority 184
database backup 72
database configuration 279
database configuration parameters
database_memory 307
locklist 307
logarchmeth1 288
logarchmeth2 288
maxlocks 307
pckcachesz 307
sheapthres_shr 307
sortheap 307
trackmod 288
database design 60
database discovery 25
database driver 223, 232
database element 145
database feature 2
database interface 62
database managed space 13
database management system 11, 57
database management utility 39
database manager 29
database manager configuration parameter 127
database modeling tool 147
database node 125
database object 13, 18, 123
database partition 12, 125
database partition group 12
database recovery 286
database replication 279, 296
database server 38
database structure 145
database system monitor 182
database user account 177
date and time data type
date 118
datetime 119
time 119
timestamp 119
year 119
DB2 commands
b2admin 20
dasauto 20
dascrt 20
dasdrop 20
dasmigr 20
db2acd 10
db2ca 104, 283
db2cc 20
db2dart 20, 334
db2icrt 19, 103
db2idrop 19
db2ilist 19, 281
db2imigr 19
db2import 301
db2isetup 20
db2iupd 101
db2iupdt 19
db2level 20
db2load 304
db2look 20, 325
db2ls 334
db2pd 20, 335
db2rc 297
db2set 281
db2setup 20, 101
db2sqljcustomize 278
db2start 19
db2stop 19
DB2 dump file 335
DB2 privilege level
database l 187
row or column 188
table space 188
DB2 system controller 10
DB2 watch dog 10
db2comm 24
db2diag tool 334
db2diag.log 335
db2fmp 10
db2greg tool 334
db2isetup command 102
db2jcc4.jar 23, 30
db2level command 334
db2move utility 302
db2setup.err 102
db2setup.log 102
DB2Sqlca 263
db2support 335
db2sysc 10
db2systm file 282
db2wdog 10
DBADM authority 183, 187
dbheap 365
DBI 33
DBI interface 222
DBMS 57
DDL 67, 122
deadlocks 274
decompress table 16
default password 77
delete privilege 188
delimited ASCII format file 298
design advisor 6
Index
443
desktop system 8
destination server 86
device name 14
diagnostic level 282
direct I/O 14
directory name 14
discover_db parameter 25
discover_inst parameter 25
disk space 90
distinctrow keyword 207
distributed platform 2
DML 71
double precision 118
driver code 3
dropin privilege 188
dynamic query 187
dynamic SQL 187
dynamic sql statement 28
dynamic table 46
dynamic warehouse 7
E
e-business 57
EDU 10
education opportunity 60
EJB 240
embedded analytical feature 7
embedded SQL statement 27
embedded system 3
employee inventory 76
encrypted password 168
engine dispatchable unit 10
engine infrastructure 10
Enterprise JavaBean 240
execute privilege 184
explain authority 183, 187
export mode 302
F
failover 57
federated storage engine 47, 130
file name 14
file system 90, 123
fixed term license 5
floating-point number 118
foreign key 48, 162
foreign key constraint 47
foriegn key 142
444
G
global level profile registry 280
global levels 280
global variable Privilege 184
go to label 264
grant command 179
grant table buffer 39
granting privilege 70
graphical interface 38
graphical tool 90
H
hashed index 133
health and fault monitor 6
heap size 127
heap storage engine 130
heap table 46
High availability 279
high availability 4
host information 25
host language variable 27
host name 39
I
I/O bound workload 16
ibm_db2 226
implicit casting 213
implicit privilege 186
implicit_schema privilege 187
import command 168
import mode 302
IMS 2
incremental backup 288
independent software vendor 5
index 136
index blocks 39
Index privilege 184
index privilege 188
indexe 13
InnoDB engine 162
InnoDB table 124
insert privilege 188
installation method 92
J
Java 62
Java code 31
Java enabled browser 30
JavaServer Page 240
join order 206
JSP 240
K
key buffer 39
L
label based access control 2, 180
language flag 102
large object 13
LBAC 180
LBAC rule exemption privilege 188
licensing model 36
lightweight deployment solution 23
lightweight security audit mechanism 58
Linux distribution 88
list command 141
ln command 107
load authority 184
load mode 302
M
main process 10
mainframe 2
maintenance window 164
make command 107
manageability 59
management tool 59
materialized query 133
memory 91
memory cache 39
memory model 4
memory storage engine 46
memory table 133
merge storage 46
merge storage engine 130
merge table 124
method privileges 188
migration assessment 56
migration project 56
migration task 56
mincommit 365
mobile device 8
module 184
module privilege 184
mounting option 123
MQT 134
multi-byte character set 33
multiple database 140
multi-user version 5
mv command 109
MYD extension 43
MYI extension 43
Index
445
446
mysqlbinlog 51
mysqlcheck 50
mysqldump 40, 51, 145, 168
mysqldump options
--help 169
--no-create-info 169171
--no-data 169
--password 169
--tab 170
--tab= 169
--user 169
mysqlhotcopy 40, 51, 172
mysqlhotcopy script 168
mysqlimport 51
mysqlshow 51
N
named pipe 39
national language support 71
natural join 208
nickname privilege 184
node 25, 125
non-recoverable database 287
non-transaction-safe storage engine 45
null value 217
numeric data type 216
bigint 118
bit 117
bool 117
boolean 117
decimal 118
double 118
fixed 118
float 118
int 118
integer 118
numeric 118
real 118
smallint 117
tinyint 117
numeric values 215
O
object privileges 18
object-oriented extension 107
ODBC driver manager 29
OLAP 7, 57
OLE DB provider 32
OLTP 57
on-line analytical processing 57
online memory tuning 164
on-line transaction processing 57
optimizer 39, 206
P
package 65
package cache 164
package cache size 307
package privilege 184, 188
parallel database system 11
parse tree 39
Parser 38
partition group 126
partitioned database environment 125
password 77
payload file deployment 92
PDO 226
pdo_ibm 33
performance 9
Perl 62
permission 186
PHP 62
PHP data object 226
PHP source 106
phpinfo() 107
physical resource 39
physical storage device 14
physical structure 12
plug-in 23
policy 311
port address 24
port number 103
Porting preparation 56
precision 117
precompile option 272
precompiled package 107
pre-compiling 27
PREP 277
pre-parsed tree 11
primary key 162
primary keys 65, 142
privilege 13, 167, 186
privilege level
column 179
database 179
global level 179
routine 179
table 179
procedural interface 107
process identifier 338
profile 180
profile registry 282
profile registry variables 280
pureXML, autonomics 2
Q
query cache 39
query interface 37
query optimization class 206
query performance 134
query syntax 39
query users 37
quiesce_connect privilege 187
R
range-clustered table 135
raw device 14
raw disk partitions 47
RDO 32
read stability 273
rebuildconf command 110
recover history file 287
recoverable database 288
recovery log 125
recovery log file 287
recovery logs file 286
recovery method 286
Redbooks Web site 439
Contact us xv
reference type 398
REFERENCES privilege 188
Referential integrity 48
referential integrity 162
register variable
db2_compatibility_vector 219
registered user 77
registry variable 24, 219, 280
relational modeling 2
remote connection 23, 36
remote data object 32
repeatable read 273
replication service 4
reserved words 145
resource tuning database technology 59
Index
447
S
schema 43, 128
Schema level 187
Schema privilege 184
secadm authority 183, 187
security 11, 13
security label privilege 188
security model 181
security system 177
select privilege 188
self tuning memory manager 6
self-tuning memory manager 59, 164165
sequence privilege 184, 188
server code 10
server privilege 184
service request 77
service ticket 76, 81
service type 82
servlets 240
setup wizard 90
severity level 84
shared memory networking protocol 39
shared-nothing architecture 48
single byte integer 117
single precision 118
single-tier 25
slave database 40
slave system 40
small integer 117
sort heap 164
448
subtable 135
summary table 134
supertable 135
symbolic link 123
synchronous event 14
synonym 117, 207
sysadm authority 181
syscat catalog view 18
sysctrl authority 181
sysmaint authority 182
sysmon authority 182
sysstat catalog view 18
system catalog 11
system catalog table 13, 128
system directory 11
system managed space 13
system monitor 182
system planning 60
system requirement 90
T
table column 11
table descriptor 39
table hierarchy 135
table partitioning 2
table space change history file 287
table space privilege 184
table spaces 12
table-level encryption 8
tar file 105
target system 56, 63
TCP/IP communication 100
temporary table 128
temporary table space 128
text 145
threaded engine 2
threaded model 9
throttled utility 311
throttling system 311
timestamp 215
trace file 101
transaction isolation 271
transaction processing 57
transaction safe 129
transaction-safe storage engines 45
TRG extension 43
trigger 65, 137
TRN extension 43
U
uncommitted read 273
underscore 178
Unified ODBC 234
unique key 142, 162
unit of work 276
unload script 173
update privilege 188
updateable view 175
use privilege 188
user account 77, 98
user account information 39, 177
user account management 167, 177
User data 68
user data 167
user defined function 10
user defined table 128
user privilege 40
user table space 128
user-defined data type 116
user-defined partitioning 124
V
validation program 88
value compression 386
vector I/O 14
version recovery 291
view 13
W
warehouse architect 59
Web server 26
Web service 22
Web-browser 26
whenever statement 263
wildcard 178
wlmadm authority 183, 187
work sheet format 298
workload 164
workload management 2
workload manager 279
Index
449
X
XML support 71
XSR object privilege 184
450
(1.0 spine)
0.875<->1.498
460 <-> 788 pages
Back cover
INTERNATIONAL
TECHNICAL
SUPPORT
ORGANIZATION
BUILDING TECHNICAL
INFORMATION BASED ON
PRACTICAL EXPERIENCE
IBM Redbooks are developed by
the IBM International Technical
Support Organization. Experts
from IBM, Customers and
Partners from around the world
create timely technical
information based on realistic
scenarios. Specific
recommendations are provided
to help you implement IT
solutions more effectively in
your environment.
ISBN 0738433659