Examples of Business Rules in SQL

Previous Next

The following examples are typical declarative business rules, and show how they are seamlessly translated into the rule-based technology.

Example Rule 1

A Customer cannot withdraw money if their account is overdrawn or the account has been frozen.

The precise conditions are: the sum of the TRANSACTIONS is less than 0 or the ACCOUNT_STATUS is Frozen.

Implementation:

 

SELECT '' RESTRICT

FROM     ACCOUNT A

WHERE    0 > (    SELECT SUM(T.TXN_AMOUNT)

                 FROM TRANSACTIONS T

                 WHERE A.ACCOUNT_ID = T.ACCOUNT_ID)

OR     A.ACCOUNT_STATUS = 'FROZEN'

This business rule is a restrictive type of constraint. It affects two database objects. This rule would ensure that the situation (described above) would never be possible.

This business rule is checked regardless how these transactions enter the system (e.g. direct entry, background process, or batch process).

Also, this business rule is checked regardless of how the rule is broken (e.g. adding a new transaction, changing the amount of any transaction, or setting the account to frozen).

Example Rule 2

Customers are charged an ATM transaction fee of $1.00. If the customer has generated more than 10 ATM transactions since the last statement, ATM transaction fees would be waived.

Implementation:

 

INSERT INTO TRANSACTION_FEES

(ACCOUNT_ID,TXN_AMOUNT,TXN_DATE,TXN_FEE_ID)

  SELECT ACCOUNT_ID,1,SYSDATE,1

  FROM   TRANSACTIONS T

  WHERE  OLD(TXN_ID) IS NULL

  AND    10 > ( SELECT COUNT(*)

                FROM   TRANSACTION_FEES TF, ACCOUNT A

                WHERE  TF.TXN_DATE   > A.LAST_STATEMENT_DATE

                AND    TF.ACCOUNT_ID = A.ACCOUNT_ID

                AND    TF.ACCOUNT_ID = T.ACCOUNT_ID)

This Rule is behavioral, corrective. If a transaction is generated, then the Rule will check to see if 10 transaction fees have already been created since the last statement date. If less than 10 have been created, the Rule will create a new transaction fee to go along with the transaction the user has just entered.