Documente Academic
Documente Profesional
Documente Cultură
When certain events take place in an IBM DB2 Universal DatabaseTM database, you can fire triggers that can perform other actions. In this article, you'll explore the world of triggers and see how they can be used to enforce business rules in your database. You'll also learn how the DB2 UDB Version 8.1 Control Center can help you build a simple trigger that can be applied to a simple business scenario.
Setting up shop
As mentioned above, our bank will consist of only one table. In it, we'll house information about a customer's checking account and savings account balances. Each customer is identified by their social security number. Here is a description of the table: Table 1. Description of ACCTTABLE
Column Type Varchar(11) Varchar(30) Varchar(30) Decimal (Precision: 7, Scale: 2) Decimal (Precision: 7, Scale: 2)
Nullable? NO NO NO NO NO
* denotes primary key Go ahead and use the DB2 Command Line Processor to create a database for the table above. Call the database bnkdb.
db2 => connect to bnkdb user db2admin using db2admin Now, create the accttable table:
db2 => create table accttable(ssn varchar(30) not null primary key, lastname varchar(30) not null, firstname varchar(30) not null, savingbalance decimal(7,2) not null, checkingbalance decimal(7,2) not null)
Triggers can be fired before or after an INSERT, DELETE, or UPDATE of a table. In our example, you'll be creating a trigger that fires before the UPDATE of the ACCTTABLE. In trigger terminology, the INSERT, DELETE, or UPDATE that causes a trigger to be fired is known as the triggering event. Whether a trigger fires before or after the triggering event is known as the activation time of the trigger. Back to top
On the Create Trigger screen, you'll be able to specify the schema your trigger resides in. Go ahead and choose the DB2ADMIN schema. Remember, triggers are associated with tables, so we need to choose the schema of the associated table. Again go ahead and choose the DB2ADMIN schema:
In the same screen, you will need to specify a trigger name. Specify a trigger name of OVERDRAFT. Also, specify the table name that is associated with the trigger. Pick the table you created called ACCTTABLE.
In the section called Operation that causes the trigger to be executed choose the Update of columns function and specify the column of CHECKINGBALANCE:
Figure 5.
Note that you might not be able to specify a correlation name for an option depending on the combination of activation time and the particular operation that causes the trigger to fire. For example, let's say you chose a Time to trigger action of Before for an trigger that is fired by a DELETE statement. In such a scenario, we won't be able to specify a "Correlation name for the new rows" Why? Because after a delete, a new row will not exist. Because you are formulating a before trigger invoked by an UPDATE, you cannot populate the Temporary table for the old rowsand Temporary table for the new rows options. You'll notice that in this case (a before trigger invoked by an update), you can only specify that the trigger fire on each row, not each statement.
The statement that causes a triggering event might affect multiple rows in the database. The "For each Row" option means that the trigger will be activated once for each row that is modified. On the other hand, the "For each statement" (not allowed for "before" triggers) would mean that the actions defined by a trigger would only be performed once for the invoking SQL statement. You can click the Show SQL button to see what the underlying SQL statement looks like so far:
Let's dissect this logic piece by piece. In a trigger body, you can declare variables that you want to use in the body of the trigger. We do this with the line: decimal(7,2)declare overage decimal (7,2); where we declared a variable named overage of the type decimal(7,2). Next we set the value of our overage variable to the value of (NEWROW.CHECKINGBALANCE*-1); We'll use this calculation to figure out how much extra (the overage) we are trying to pull out from checking account.NEWROW.CHECKINGBALANCE is specified because we want to analyze the checking balance as it would be if the update were carried out.
You can use a SIGNAL statement to raise an error condition if the business rule defined in your trigger is violated. In our case, we do not allow someone to have a negative checking account balance. If someone tries to update the checking account balance column to a negative amount, we try and see if we have enough in the savings account to make up for the negative amount. If not, we signal the SQL state of '70001' with the message of "'Overdraft Protection Unsuccessful". It is important to realize the impact of the inclusion of our SIGNAL statement. The SIGNAL statement rolls back the changes that were attempted by the triggering statement (that is, our update statement). SIGNAL statements will also roll back changes caused within the trigger. Furthermore, let us say that we are using something like a JavaTM application that interacts with our database and tries to perform an update operation that fires our trigger and violates our business rule. The Java application will receive the SQLSTATE we specified and the SQLCODE of -438. We use the SIGNAL SQLSTATE feature in this line:
if overage>OLDROW.SAVINGBALANCE then SIGNAL SQLSTATE '70001' ('Overdraft Protection Unsuccessful');
which states that if our overage is greater than the amount in our savings balance, then we need to go ahead and raise a red flag.
Transferring money
Transferring money occurs if the savings account balance is large enough to make up for the overage. If this condition is met, we perform two modifications to the new row: 1. Modify the savingbalance column of the "new row" and subtract the overage to facilitate the overdraft transfer. 2. Set the new row's checking balance to zero. We do this with our code of:
else set newrow.savingbalance = oldrow.savingbalance-overage, newrow.checkingbalance = 0; end if;
3.
When you click Close, you should see that the OVERDRAFT trigger has been created:
Back to top
Following the business logic we created, this update should fire our trigger which will take 500.00 from the savingbalance column to account for the overdraft of the checking account. Consequently, our checkingbalance will be 0.00 and our savingbalance will be 1000.00 (the original balance of 1500 - 500 of overdraft) for the account with SSN 111-11-1111. The query shown below verifies this:
Back to top
Wrap-up
You've created a DB2 trigger in the context of a mock business scenario. Triggers are a powerful DB2 database feature that can be used to polarize business logic to the relational database. Such a polarization is quite powerful considering the fact that you might have multiple applications trying to interact with the same database. Many times, in a large enterprise, you might even be unaware of what applications are being built that will interact with your database. Rather than just hope that these applications follow business rules that are considered the commandments of your organization (that is, they are deemed always true) , you can use triggers as one of the tools in your toolbox to make sure such business rules are enforced by all applications that interact with your database now and in the future. Back to top