Now that you have seen the syntax of triggers, you will view another example of how to code a trigger and then complete an exercise where you create three triggers of your own. First, however, look at the table below to see a summary of the combinations of action, timing, and scope that can be used when creating table triggers.There are potentially 12 different triggers that can be set up for any database table.
The most commonly used triggers are the first three listed in the table below.
Action | Timing | Scope | Syntax example |
Update | Before | Once per row | CREATE TRIGGER PRE_UPDATE BEFORE UPDATE ON CUSTOMER FOR EACH ROW... |
Insert | Before | Once per row | CREATE TRIGGER PRE_INSERT BEFORE INSERT ON CUSTOMER FOR EACH ROW... |
Delete | Before | Once per row | CREATE TRIGGER PRE_DELETE BEFORE DELETE ON CUSTOMER FOR EACH ROW... |
Update | After | Once per row | CREATE TRIGGER POST_UPDATE BEFORE UPDATE ON CUSTOMER FOR EACH ROW... |
Insert | After | Once per row | CREATE TRIGGER POST_INSERT BEFORE INSERT ON CUSTOMER FOR EACH ROW... |
Delete | After | Once per row | CREATE TRIGGER POST_DELETE BEFORE DELETE ON CUSTOMER FOR EACH ROW... |
Update | Before | Once per transaction | CREATE TRIGGER PRE_UPDATE BEFORE UPDATE ON CUSTOMER... |
Insert | Before | Once per transaction | CREATE TRIGGER PRE_INSERT BEFORE INSERT ON CUSTOMER... |
Delete | Before | Once per transaction | CREATE TRIGGER PRE_DELETE BEFORE DELETE ON CUSTOMER... |
Update | After | Once per transaction | CREATE TRIGGER POST_UPD_TRANS BEFORE UPDATE ON CUSTOMER... |
Insert | After | Once per transaction | CREATE TRIGGER POST_INS_TRANS BEFORE INSERT ON CUSTOMER... |
Delete | After | Once per transaction | CREATE TRIGGER POST_DEL_TRANS BEFORE DELETE ON CUSTOMER... |
This trigger's purpose is to compare the current sale price of a product to the current cost of the product.
If the sale price is lower than the cost, the trigger fails which in turn causes the update to fail. This prevents a user from setting a money-losing price for any product.
The steps for creating a `BEFORE UPDATE` row-level trigger in Oracle 11g R2. Here's an explanation and a slight refinement to ensure everything is accurate:
Step-by-Step Verification and Explanation:
-
Trigger Definition Start:
-
Trigger Timing and Table:
-
BEFORE UPDATE ON PRODUCT
- Correct. Specifies that the trigger should fire before the
UPDATE
operation on the PRODUCT
table.
-
Row-Level Trigger:
-
FOR EACH ROW
- Correct. This ensures the trigger fires for each row being updated, rather than firing once for the entire
UPDATE
statement.
-
Declaring a User-Defined Exception:
-
PRICE_TOO_LOW EXCEPTION;
- Correct. A user-defined exception is declared to handle cases where the price is lower than the cost.
-
Condition Testing:
-
Exception Section:
-
Completing the Trigger:
-
END;
/
- Correct. The
END;
closes the trigger body, and the /
executes the trigger creation in SQL*Plus or similar tools.
Complete Trigger Code: Below is the complete trigger code based on the steps provided:
CREATE OR REPLACE TRIGGER PRE_UPD_PRODUCT
BEFORE UPDATE ON PRODUCT
FOR EACH ROW
DECLARE
PRICE_TOO_LOW EXCEPTION;
BEGIN
IF :NEW.SALE_PRICE < :NEW.STORE_COST THEN
RAISE PRICE_TOO_LOW;
END IF;
EXCEPTION
WHEN PRICE_TOO_LOW THEN
RAISE_APPLICATION_ERROR(-2000, 'Raise price');
END;
/
Additional Notes:
- Trigger Name: Ensure the name
PRE_UPD_PRODUCT
is meaningful and adheres to your naming conventions.
- Validation: After creating the trigger, you can test it by attempting to update the
PRODUCT
table with a SALE_PRICE
less than the STORE_COST
.
- Error Code Range: Oracle recommends using custom error codes in the range
-20000
to -20999
. The code -2000
you provided is slightly outside this range, but it should still work.
The steps outlined, combined with the complete code, are valid and should result in a correctly functioning trigger.