This module examined how Oracle invokes PL/SQL automatically when a configured database event occurs. You progressed from identifying a trigger's event and timing to using row values, validating changes, recording transactional history, and combining INSERT, UPDATE, and DELETE behavior. The central skill is explaining exactly what a trigger does when an application changes data, including what happens when that change fails or is rolled back.
These lessons use Oracle AI Database 26ai, but automatic firing, row pseudorecords, and ordinary transaction participation are established PL/SQL concepts. Understanding their interaction matters more than memorizing a CREATE TRIGGER template. A definition can compile successfully while implementing the wrong business rule, using an unsuitable timing point, or recording history with unintended transaction semantics.
Use this conclusion to connect the examples and assess your understanding. The original lessons contain the complete setup scripts, diagrams, and tests. This review adds read-only diagnostic queries rather than another trigger to install. Its expected results refer to the stated lesson baselines; they are not results captured from an Oracle execution during preparation of this article.
| Lesson | Main question | Evidence to look for |
|---|---|---|
| 1. Introduction to triggers | Does this action belong with a database event? | A stated purpose and an appropriate choice between constraints, procedures, and triggers. |
| 2. What is a trigger? | Which event, timing point, and firing level apply? | A definition whose activation conditions match the requirement. |
| 3. Triggers and procedures | Who invokes the operation? | A clear distinction between an explicit call and automatic trigger execution. |
| 4. OLD and NEW values | Which row values can the body read or assign? | Correct pseudorecord references, assignment timing, and NULL comparisons. |
| 5. Individual DML events | How does the rule behave for its selected event? | Successful and rejected operations with verified stored values. |
| 6. Combining DML events | Which logic is shared and which is event-specific? | All three event paths, including DELETE history and rollback. |
Read the sequence as a repeatable method. Start with the data requirement, select the mechanism, identify the available values, and then write the smallest appropriate body. Define the test results before running the DML. This keeps the database operation, its automatic consequences, and the final transaction decision connected in your explanation.
A procedure exposes an operation that a caller invokes by name. A trigger is associated with an event, so Oracle invokes it when a qualifying operation reaches the configured timing point. Creating either object stores a definition; it does not perform the object's future business operation or reconstruct earlier changes.
The two mechanisms can cooperate. A procedure may issue an UPDATE that fires a table trigger, and a trigger may call a helper procedure. Moving logic into a helper does not remove the restrictions imposed by the trigger's execution context. In particular, moving a COMMIT into an ordinary called procedure does not make transaction control permissible inside the trigger's operation.
A trigger also does not expose formal IN, OUT, and IN OUT parameters for applications to supply. Oracle provides its row context while processing the triggering DML. When a helper needs information, pass suitable individual field values. A procedure interface and a table trigger solve different activation problems, even when both are written in PL/SQL.
Choose an explicit procedure when the application needs a named operation with a documented interface. Choose a trigger when justified behavior should accompany a database event across permitted access paths. Privileges still determine which paths are available; defining a procedure does not automatically prevent users from updating its underlying table directly.
For the simple table triggers used here, INSERT, UPDATE, and DELETE identify events. BEFORE and AFTER select timing. FOR EACH ROW selects row scope; omitting it selects statement scope. These choices are independent enough that the same event can support different designs, such as validating a proposed row or collecting statement-level information.
For ordinary execution of one successful statement affecting several rows, statement-level BEFORE work precedes row processing. Each row's BEFORE work precedes its modification and its AFTER work follows that modification. Statement-level AFTER work follows the row processing. Do not turn this model into an assumption about which particular row Oracle processes first.
A statement affecting no rows still has statement-level timing points, but it supplies no affected row for a row trigger. One transaction can contain several qualifying statements, so statement scope never means once per transaction. Similarly, AFTER does not mean after COMMIT. Oracle documents the timing model in its DML trigger reference.
When several triggers share a timing point, do not infer their order from their names or creation dates. If correctness requires a sequence, use an appropriate supported ordering design or keep tightly related operations together. An example with one trigger at each timing point does not establish an ordering rule for every trigger installed in a production schema.
| Event | OLD fields | NEW fields | Typical use |
|---|---|---|---|
| INSERT | NULL | Proposed inserted values | Validate or initialize the incoming row. |
| UPDATE | Values before this update | Proposed updated values | Compare a transition or validate the resulting row. |
| DELETE | Values being removed | NULL | Inspect or record the deleted row. |
These pseudorecords describe one affected row, not the complete set of rows changed by a statement. OLD is relative to the current operation. If a transaction updates the same row twice, the second update's OLD values reflect the result of the first update, even though neither change has necessarily been committed.
OLD fields are read-only. A BEFORE INSERT or UPDATE row trigger can assign individual NEW fields; an AFTER row trigger cannot assign them. Statement-level code has no individual row's OLD or NEW values. Inside a trigger body, use the colon notation, such as :NEW.sale_price. In a trigger WHEN condition, omit the colon. See Oracle's pseudorecord documentation.
The PRODUCT example in Lesson 4 makes this concrete. With the stated starting row for product 14, an UPDATE proposes cost 4.50 and a NULL sale price. The BEFORE trigger supplies 4.95 using the example's ten-percent markup and assigns the update date when the cost changes. A non-NULL proposed price is retained instead. Those outcomes follow the chosen rule; the trigger does not automatically reprice every cost change.
An ordinary comparison involving NULL does not establish that two values are equal or unequal. An IF branch executes only when its condition is TRUE. Therefore, a condition comparing NEW and OLD with an inequality alone can miss a transition from a known value to NULL or from NULL to a known value.
Lesson 4 explicitly handles those transitions when detecting a cost change. Lessons 5 and 6 choose another policy for price validation: compare amounts only when both required operands are known. Neither policy is universally correct. The requirement must say whether unknown amounts are allowed and how a transition involving an unknown value should behave.
In Lesson 6, the no-price-reduction comparison deliberately excludes NULL operands. It therefore does not prohibit every possible historical price decrease: a known price could become NULL and later become a lower known price. If that path is unacceptable, change the stated policy and enforce the necessary data requirements. Do not claim stronger protection than the code actually provides.
INSERTING, UPDATING, and DELETING identify the corresponding triggering events. They let one body share a rule between INSERT and UPDATE while handling DELETE differently. A column-specific predicate such as UPDATING('SALE_PRICE') identifies a targeted column; it does not prove that the value changed. Self-assignment can still qualify.
Lesson 6's PRODUCT_DEMO_BIUD trigger handles all three events on an isolated practice table. INSERT and UPDATE share the price-versus-cost validation. UPDATE also compares the old and new price, while DELETE copies OLD fields to a separate history table. The DELETE branch does not attempt to validate a nonexistent replacement row through NEW.
With the lesson's baseline, product 901 reaches price 16 at cost 10. Reducing its price to 14 raises ORA-20002; raising its cost to 17 raises ORA-20001. Inserting product 903 with cost 10 and price 9 also fails. Deleting product 902 creates transactional history, which disappears when that deletion is rolled back.
A simple trigger covering several events still has one timing point. A compound trigger has explicitly defined timing sections and can share state across them. Combining events and combining timing sections are separate design decisions. Revisit the diagrams in Lessons 3 and 6: one follows statement and row processing, while the other identifies the parts of a multi-event definition.
For an ordinary nonautonomous DML trigger, database changes made by its body participate in the triggering transaction. This relationship is essential for transactional history: reversing the business change also reverses its history insert. An AFTER trigger can finish while the transaction is still pending and subject to rollback.
A failure in the module's simple DML triggers rolls back the triggering statement and its associated trigger effects. That is different from automatically rolling back every earlier statement in the transaction. A rejected multirow UPDATE must leave all of that statement's targeted rows unchanged, including rows processed before the failure. Earlier successful work can remain pending.
Savepoints make this distinction observable. Put the savepoint before the work you intend to reverse, query the changed values, and then roll back to it. ROLLBACK TO does not end the transaction. A full ROLLBACK ends it. Complete object creation before these tests because DDL and client autocommit can invalidate the intended savepoint sequence.
An autonomous transaction commits or rolls back independently of the main transaction. A trigger declared with PRAGMA AUTONOMOUS_TRANSACTION can therefore persist a separate log entry even when the original business operation later rolls back. That entry can describe an attempt; it does not prove that the main transaction committed.
Independence does not mean that autonomous work cannot affect its caller. An autonomous routine can fail, and accessing a resource locked by the suspended main transaction can cause a deadlock. It must finish its own active transaction before returning normally. These requirements make autonomy a deliberate design choice rather than a routine way to bypass ordinary trigger restrictions.
The module's transactional examples do not need autonomous logging. Keep their history in the main transaction so their rollback tests have a clear meaning. If a separate attempt log is required, design its data, failure behavior, and access controls explicitly. Oracle's autonomous transaction documentation explains the independent transaction model.
A missing effect can have several causes: the wrong schema, an absent or disabled trigger, a mismatched event, no affected rows, a false condition, or a later rollback. Diagnose them separately. The following read-only queries inspect the Lesson 6 trigger in the current user's schema; they do not fire or modify it.
SELECT trigger_name, trigger_type, triggering_event, status
FROM user_triggers
WHERE trigger_name = 'PRODUCT_DEMO_BIUD';
SELECT object_name, status
FROM user_objects
WHERE object_name = 'PRODUCT_DEMO_BIUD'
AND object_type = 'TRIGGER';
USER_TRIGGERS reports enablement, while USER_OBJECTS reports object validity. ENABLED and VALID answer different questions. An empty result may mean that the object was not created in this schema, not that it passed every check. Use the uppercase name here because the example creates an unquoted identifier. Oracle describes the trigger metadata in USER_TRIGGERS.
SELECT line, position, attribute, text
FROM user_errors
WHERE name = 'PRODUCT_DEMO_BIUD'
AND type = 'TRIGGER'
ORDER BY sequence;
Resolve ERROR diagnostics and review WARNING diagnostics according to their messages. A warning is not automatically a compilation failure, and an empty diagnostics result does not establish that an object exists. After checking metadata, test actual behavior and inspect stored values. The errors dictionary reference documents ATTRIBUTE and the related USER_ERRORS view.
Use declarative constraints for suitable integrity rules. Primary keys, foreign keys, unique constraints, NOT NULL, and CHECK each express a specific kind of requirement. A same-row price-versus-cost rule usually belongs in a CHECK constraint. Its NULL behavior still matters: an UNKNOWN condition can satisfy CHECK, so required amounts need NOT NULL separately.
Triggers are useful for justified event-associated actions, but automatic invocation is not proof of a complete design. A timestamp field is not a full change history. A history table is not automatically a tamper-resistant security audit. A procedure wrapper does not replace privileges, and moving work into a trigger does not establish that concurrent operations are handled correctly.
When consolidating triggers, inspect the existing definitions first. Creating a new name does not remove differently named triggers already attached to the table. Avoid duplicate behavior, unnecessary self-DML, and hidden dependency chains. Use pseudorecord fields for the current row instead of querying its mutating table, and keep shared helper routines within the applicable trigger restrictions.
You should be able to explain each test in terms of its starting data, requested operation, expected trigger path, and transaction outcome. Include equality, NULL transitions, self-assignment, zero-row statements, and multirow failures. Do not treat compilation success or an application's success message as a substitute for reading the resulting data.
For example, suppose an UPDATE reports success but no salary-history row appears. First establish whether it matched an employee. Then check whether salary actually changed, whether the history trigger was enabled, and whether the transaction was subsequently rolled back. Those observations distinguish a missing invocation from an intentionally skipped history insert or a correctly reversed change. Writing down that sequence makes troubleshooting more reliable than repeatedly recreating the trigger and rerunning the same statement without inspecting its effects.
Keep practice baselines separate from installation scripts. The product 14 example depends on the course row and its stated starting values, whereas the Lesson 6 examples create independent tables with their own identifiers. Running one lesson does not automatically establish the other's starting state. Before comparing a query result with the text, confirm the schema, table, row identifier, and current values. Restore test changes deliberately so a previous attempt does not silently determine the next result.
Return to the Create Three Triggers exercise to practice choosing event and timing combinations. Then use the Trigger Combination exercise to organize related behavior in one definition. Apply the current lessons' transaction and NULL-handling principles when reviewing any exercise answer.
The next module, Using Oracle Parameters, develops explicit interfaces for PL/SQL subprograms, script inputs, and parameterized cursors. Connect that material with the procedure-versus-trigger distinction: callers supply procedure or function arguments, while Oracle supplies a trigger's event and row context. A trigger can pass individual values to a helper routine without acquiring a caller-visible parameter list of its own.