Db2 Cert6113 PDF
Db2 Cert6113 PDF
Db2 Cert6113 PDF
This tutorial is designed to introduce you to the skills to implement business rules in a DB2
database environment. This tutorial will also help you prepare for Section 3 of the DB2 10.1
DBA for Linux, UNIX, and Windows certification exam 611.
View more content in this series
Objectives
After completing this tutorial, you should be able to:
Prerequisites
To take the DB2 10.1 for Linux, UNIX, and Windows DBA certification exam (exam 611), you must
have already passed the DB2 10.1 Fundamentals exam (exam 610) or the DB2 9 Fundamentals
exam (exam 730).
This tutorial is one tool to help you prepare for exam 611. You should also review the resources at
the end of this tutorial for more information about DB2 utilities (see Related topics). Although not
all materials discussed in the Fundamentals tutorial series are required to understand the concepts
described here, you should at least have a basic knowledge of:
DB2 products
DB2 tools
DB2 instances
Databases
Database objects
System requirements
You do not need DB2 to complete this tutorial. However, you will get more out of it if you download
the free trial version of IBM DB2 10.1 to work along with this tutorial.
This section focuses on business rules as a statement that defines or constrains some aspect of
the business, the implementation of which requires deep knowledge about the business process.
For example:
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 2 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
Such rules must be checked in two distinct system events: when creating a new product/customer
or when updating the address of an existing customer.
DB2 10.1 provides the following types of constraints to facilitate the implementation of business
rules:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Informational
In a DB2 database, a null value represents an unknown state. By default, null values are allowed
for all built in data types. However, there might be some business rules that insist that a value
always be provided. For example, "It is mandatory for every customer to have at least one
address." Such a rule can be implemented by creating a NOT NULL constraint on the address
column as shown in the following example.
The NOT NULL defined on the CUSTOMER_ADDRESS1 column ensures that it can never be
assigned a null value. However, providing values for CUSTOMER_ADDRESS2 is optional as this
is a nullable column.
With such a table definition, the following statements will execute successfully:
INSERT INTO CUSTOMER VALUES (001,'CHRISTINE HAAS','25 EASTCREEK',NULL); INSERT INTO CUSTOMER VALUES
(002,'HELENA THOMPSON', '5 ROSEWOOD STREET',' MARKHAM');
However, the following statement would fail and return an SQL0407N error, indicating a violation of
the NOT NULL constraint that has been defined on the CUSTOMER_ADDRESS1 column:
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 3 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
UNIQUE constraint
A UNIQUE constraint is a rule that prevents duplicate values from being stored in one or more
columns within a table. The columns on which a UNIQUE constraint is needed must be defined as
NOT NULL.
UNIQUEconstraint can be defined in the CREATE TABLE or ALTER TABLE statement. The following
examples show the creation of a UNIQUE constraint on the CUSTOMER_PHONE column of a table
named CUSTOMER.
With such a table definition, the following statement will execute successfully:
INSERT INTO CUSTOMER VALUES (001,'CHRISTINE HAAS', '493-308-2412',' 25 EASTCREEK',NULL);
However, execution of the following statement would result in an SQL0803N error due to the
violation of the unique constraint defined on the column CUSTOMER_PHONE.
INSERT INTO CUSTOMER VALUES (002,'HELENA THOMPSON','493-308-2412', '5 ROSEWOOD STREET',' MARKHAM');
Creating a unique constraint automatically creates a unique index on the column. In the event
a unique index is created by an ALTER TABLE statement, any existing index on the column will
automatically be designated as unique index.
Though uniqueness can be enforced by both a UNIQUE constraint and a unique index, a unique
index allows one (and only one) null value on the column(s) specified and generally cannot be
used as a parent key to enforce referential integrity. However, a UNIQUE constraint does not allow
null values and can be used as a parent key.
PRIMARY KEYs can be defined when a table is created or can be added later by altering a table's
definition.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 4 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
With such a table definition, the following statement will execute successfully (the first time it is
executed):
INSERT INTO CUSTOMER VALUES (001,'CHRISTINE HAAS', '493-308-2412',' 25 EASTCREEK',NULL);
However, if the same statement is executed more than once, it results in an SQL0803N error due to
a violation of the PRIMARY KEY constraint defined on the CUSTOMER_ID column.
Here, the BRANCH table acts as the parent table, and the CUSTOMER table acts the child table.
The referential constraint defined on the CUSTOMER table allows users/applications to only
assign a branch to a customer when the branch exists in the BRANCH table.
Similarly, any branch assigned to a customer should not be deleted from the list of branches. This
is enforced by specifying the DELETE rule RESTRICT, which ensures a row in the BRANCH table
does not get deleted if referenced by a row in the CUSTOMER table. In addition to RESTRICT, DB2
supports following DELETE rules:
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 5 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
And, a statement that creates a customer and associates the customer with existing branch might
look like this:
INSERT INTO CUSTOMER VALUES (001,'CHRISTINE HAAS','493-308-2412','BR01','25 EASTCREEK', NULL);
This insert operation will be successful because the value specified for the BRANCH column
exists in the BRANCH table. However, the following statement results in a SQL0530N error since a
corresponding entry is not present in the BRANCH table.
INSERT INTO CUSTOMER VALUES (002,'HELENA THOMPSON','413-308-2412', 'BR02', '5 ROSEWOOD STREET',' MARKHAM');
CHECK constraint
CHECK constraint help implement business rules by enforcing data integrity at the table level. For
example, a business rule might dictate that the gender of a customer must be either male or
female.
Such a business rule can be implemented using a CHECK constraint on the CUSTOMER_GENDER
column of the CUSTOMER table as follows:
Any attempt to assign a value other than M or F results in an SQL0545N error. Thus, the gender
of a customer must always be either male or female. For instance, the first two of the following
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 6 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
statements will be completed successfully, whereas the third fails due to violation of the CHECK
constraint.
Informational constraint
In situations where an application enforces a constraint (such as the gender of a customer), it
is needless to enforce the same constraint at the database level. The high amounts of system
activity, especially when loading large quantities of records that have referential integrity
constraints defined, becomes overhead. Informational constraints avoid this overhead by telling
the database manager what rules the data conforms to without the need to be enforced.
These examples use the NOT ENFORCED clause to instruct the database manager not to enforce the
constraint when INSERT and UPDATE operations are performed. The ENABLE QUERY OPTIMIZATION
clause tells the database manager to use the constraint when optimizing SQL operations.
The behavior of INSERT/UPDATE operations would need a deep understanding when NOT ENFORCED
is used.
In this example, the value for CUSTOMER_GENDER is specified as T, which violates the check
constraint defined for this column. Yet, the database manager allows the insert to happen since the
NOT ENFORCED clause was used when the constraint was created.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 7 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
Consequently, the data that resides in the table will look something like this:
When the same table is queried for the record that violates the check constraint as shown below,
unexpected results will be returned.
This is because of the ENABLE QUERY OPTIMIZATION clause, which instructs the database manager
to use the constraints during SQL optimization. If this is not the desired result, the constraint needs
to be recreated by specifying the DISABLE QUERY OPTIMIZATION clause to define the constraint. For
example:
ALTER TABLE CUSTOMER ALTER CHECK CUST_GEN_CHK DISABLE QUERY OPTIMIZATION;
Now the database manager will no longer use the constraint when retrieving data from the table.
Consequently, the results produced by querying the table will look something like this:
Listing 16. Database manager no longer using constraint when retrieving table
data
SELECT CUSTOMER_ID, CUSTOMER_NAME,CUSTOMER_GENDER FROM CUSTOMER; CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER
------------- --------------- ------------------- 1 CHRISTINE HAAS M 2 HELENA THOMPSON F 3 KATHY SMITH T
And the query that did not return any records when query optimization was enabled, returns the
row that violates the constraint now:
Listing 17. Query that did not return any records when query optimization was
enabled
SELECT CUSTOMER_ID, CUSTOMER_NAME,CUSTOMER_GENDER FROM CUSTOMER WHERE CUSTOMER_GENDER ='T'; CUSTOMER_ID
CUSTOMER_NAME CUSTOMER_GENDER ------------- --------------- ------------------- 3 KATHY SMITH T
Informational constraints can help improve performance and can be used if the data originates
from a trusted source that guarantees data integrity. Typically, data warehouses that have data
originating from different OLTP systems can leverage the informational constraints that are
supported in DB2 10.1 for Linux, UNIX, and Windows.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 8 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
can also impact performance. However, such restrictions can be imposed by using views that have
been defined with the WITH CHECK OPTION specified. When used, this option enforces restrictions
on DML operations performed against a view, based on the predicates that were specified during
the view's creation. Thus, if the following SQL statements are executed:
The resulting viewCUSTOMER_BR01allows the creation of customer records only for the
branch 'BR01' and any attempt to assign other branches will result in an error.
Therefore, the following INSERT statement succeeds because it conforms to the restrictions
imposed by the view:
INSERT INTO CUSTOMER_BR01 VALUES (001,'CHRISTINE HAAS', 'M','493-308-2412','BR01',' 25 EASTCREEK');
And if the CUSTOMER_BR01 view is queried immediately after this INSERT statement is executed,
the results produced should look like this:
On the other hand, the following statement fails and an SQL0161N error is returned since the data
values provided do not conform to the restrictions imposed by the view's definition.
INSERT INTO CUSTOMER_BR01 VALUES (002,'HELENA THOMPSON','F','413-467-8732','BR02','5 ROSE STREET');
The restriction is imposed through the view; there is no restriction on the underlying base table.
Furthermore, the restriction is applied only to the subset of data as required by the business rule.
It is important to note that the WITH CHECK OPTION clause cannot be specified for:
A read-only view
A view that references the NODENUMBER or PARTITION function, a nondeterministic function, or a
function with an external action
A typed view
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 9 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
business rule that requires writing audit details to an audit table whenever data is changed in a
transaction table.
Triggers are used to defining set of actions to be performed in response to events as specified
by one or more business rules. Thus, application logic and business rules can be moved into a
database by using triggers. This causes such rules to be automatically enforced by the database
management system, even when newer applications come on line at a later point in time.
Suppose you want to create a trigger to enforce a business rule that states, "For any new
customer record that gets created, details of the employee that created the customer record, along
with the time of record creation, needs to be logged." The first component needed for the trigger is
the subject table. For this example, let's suppose it is a table named CUSTOMER that was created
as follows.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 10 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
Based on our business rule, we're also going to need a target table. For this example, let's
suppose it is a table named AUDIT_LOG created as follows.
The second component needed for the trigger is an event, and in this case, the event is the
execution of an INSERT statement. The third component needed is the triggering time, which in this
example is AFTER the INSERT operation has been processed. And the fourth component needed
is the action to be performed, which in this case is to INSERT a record into the AUDIT_LOG table.
(The INSERT operation happens automatically in response to the triggering event and is transparent
to the application.)
Once this trigger is created, if the following statement is used to insert new records into the
CUSTOMER table (to create new users).
Corresponding records are inserted into the AUDIT_LOG table by the trigger. These records can
be seen by executing the following query.
Similarly, we can create a trigger that will insert a record into the AUDIT_LOG table trigger any
time a record in the CUSTOMER table is modified by executing the following SQL statement.
Listing 25. Insert record into AUDIT_LOG table trigger when record in
CUSTOMER table is modified
CREATE TRIGGER UPDT_CUST_TRIG AFTER UPDATE ON CUSTOMER REFERENCING OLD AS O FOR EACH ROW BEGIN INSERT INTO
AUDIT_LOG VALUES (CURRENT USER,O.CUSTOMER_ID,CURRENT TIMESTAMP); END;
And, once this trigger is created, if the following statement is used to change an existing record
into the CUSTOMER table:
UPDATE CUSTOMER SET CUSTOMER_GENDER = 'F' WHERE CUSTOMER_ID = 001;
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 11 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
A corresponding record will be inserted into the AUDIT_LOG table by the trigger. This record can
be seen by executing the following query.
The third record found in the result data set produced was inserted by the UPDATE trigger. Finally,
we can create a trigger that will insert a record into the AUDIT_LOG table any time a record in the
CUSTOMER table is deleted by executing the following SQL statement.
Listing 27. Create trigger to insert record into AUDIT_LOG table when record
in CUSTOMER table is deleted
CREATE TRIGGER DEL_CUST_TRIG AFTER DELETE ON CUSTOMER REFERENCING OLD AS O FOR EACH ROW BEGIN INSERT INTO
AUDIT_LOG VALUES (CURRENT USER,O.CUSTOMER_ID,CURRENT TIMESTAMP); END;
Now, if the following statement is used to delete a record from the CUSTOMER table.
A corresponding record will be inserted into the AUDIT_LOG table. Once again, this record can be
seen by executing the following query.
This time, the fourth record found in the result data set produced was inserted by the DELETE
trigger.
When a LOAD operation is initiated against a table that has constraints defined, the LOAD utility
places the table in "Set Integrity Pending" state. This state indicates the table has constraints that
were not verified after the last LOAD.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 12 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
The SET INTEGRITY transaction control statement can be used to do several things:
Bring a table out of "Integrity Pending" state by performing the integrity processing required.
A table can be incrementally processed by checking only the appended portion for constraints
violations. (A table can be checked for constraint violations by executing the SET INTEGRITY
statement with the IMMEDIATE CHECKED clause specified.)
Bring a table out of "Integrity Pending" state without performing integrity processing.
Place a table in "Integrity Pending" state.
Place a table into full access state.
For example, suppose a relational integrity constraint was created between two tables (BRANCH
and CUSTOMER) as follows:
Now, assume the following statement was used to add two records to the BRANCH table.
INSERT INTO BRANCH VALUES ('BR01', 'NATUREVALLEY', 'EVA PULASKI', 'TORONTO'), ('BR02', 'GREAT LAKES', 'JAMES
JEFFERSON', 'MARKHAM');
If the CUSTOMER table were populated using a LOAD operation like LOAD FROM customer.del OF
DEL INSERT INTO CUSTOMER;, an attempt to query the CUSTOMER table immediately after the LOAD
completes will result in an SQL0668N error due to the fact the table is in "Check Pending" status. For
example:
And by querying the system catalog, you can obtain the current status of the CUSTOMER table.
For example:
Here, the STATUS column indicates the CUSTOMER table is in "Check Pending" state, and the
ACCESS_MODE column shows the access restriction state relative to the "Check Pending" state.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 13 of 15
611 prep, Part 3: Business rules implementation
developerWorks ibm.com/developerWorks/
The value N, which is seen in the first byte in the value for CONST_CHECKED column indicates
that the foreign key constraint has not been verified on the data inserted with the LOAD utility.
If the SET INTEGRITY statement is executed with the UNCHECKED option specified, constraint
checking is bypassed and the table will remain in "Check Pending" status. For example:
SET INTEGRITY FOR CUSTOMER CHECK IMMEDIATE UNCHECKED;
On the other hand, if the SET INTEGRITY statement is executed like SET INTEGRITY FOR CUSTOMER
CHECK IMMEDIATE CHECKED;, constraint checking is performed and the table is taken out of "Check
Pending" status. And by querying the system catalog again, you can verify that the status of the
CUSTOMER table has been returned to "Normal." For example:
Listing 33. Verify that status of CUSTOMER table was returned to "Normal"
SELECT TABNAME,STATUS,ACCESS_MODE,CONST_CHECKED FROM SYSCAT.TABLES WHERE TABNAME = 'CUSTOMER'; TABNAME STATUS
ACCESS_MODE CONST_CHECKED ---------------------------------------------------------------- CUSTOMER N F
YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
As the STATUS column shows, the table is no longer in "Check Pending" status. In addition, the
table has been moved to full access state and the validation of foreign keys has been completed,
which is evident from the first byte changed to Y in the value for the CONST_CHECKED column.
At this point, all operations are permissible on the CUSTOMER table, as it has been moved out
of "Check Pending" status. Consequently, an attempt to query the CUSTOMER table should be
successful.
Listing 34. Verify that status of CUSTOMER table was returned to "Normal"
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER ------------ ----------------- ---------------- 1 CHRISTINE HAAS M
2 HELENA THOMPSON F 3 KATHY SMITH F 4 DAVID BROWN M 5 RAMLAL MEHTA F 6 WING LEE M 7 KIYOSHI YAMAMOTO M 8 ROY
ALONZO M 9 KIM NATZ F 10 THEODORE SPENSER M
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 14 of 15
611 prep, Part 3: Business rules implementation
ibm.com/developerWorks/ developerWorks
Related topics
Read the Preparation Guide for DB2 10.1 DBA for Linux, UNIX, and Windows Exam 611 to
learn in-depth information about each of the concepts presented in this tutorial. This guide is
compilation of topics from the DB2 10.1 documentation.
Use the DB2 documentation in IBM Knowledge Center to find more details about each of the
concepts presented in this tutorial.
Use an RSS feed to request notification for the upcoming tutorials in this series. (Find out
more about RSS feeds of developerWorks content.)
Now you can use DB2 for free. Download DB2 Express-C, a no-charge version of DB2
Express Edition for the community that offers the same core data features as DB2 Express
Edition and provides a solid base to build and deploy applications.
DB2 10.1 DBA for Linux, UNIX, and Windows certification exam Page 15 of 15
611 prep, Part 3: Business rules implementation