Sub topic 1
What Is a Database Trigger?
A database trigger is a stored PL/SQL program that Oracle runs automatically when a specified database event occurs. The event might be an INSERT, UPDATE, or DELETE on a table. It might also be a data definition language (DDL) event, such as dropping a table.
A trigger belongs to the database schema, not to one particular application. Any session that causes the matching event can cause the trigger to run. This makes a trigger useful when a rule must apply consistently to changes from applications, scripts, batch jobs, and direct SQL sessions.
Prerequisites
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE statements; basic PL/SQL blocks, variables, conditions, and exceptions; tables, views, and sequences; and the meaning of COMMIT and ROLLBACK.
The Trigger Working Model
A trigger has three important parts:
- The event that starts it, such as inserting into
employees. - The timing, such as before or after the event.
- The trigger body, which contains PL/SQL statements.
For example, a BEFORE INSERT trigger runs after Oracle receives an insert statement but before Oracle stores the new row. It can check values or assign a generated value. An AFTER UPDATE trigger runs after the row has been changed. It is often suitable for recording an audit entry.
A trigger can run once for the whole statement or once for every affected row. A statement that updates 500 rows can therefore run a statement-level trigger once or a row-level trigger 500 times.
When to Use a Trigger
Use a trigger when the rule is a database-wide reaction to a data or schema event. Common examples include recording who changed a row, filling a creation timestamp, rejecting an invalid transition, or protecting objects from accidental DDL operations.
A constraint is usually better when the rule can be expressed declaratively. For example, use a CHECK constraint for a salary that must be nonnegative, or a foreign key for a required relationship. Constraints are easier for Oracle to optimize and inspect.
Application code is often better for workflows, user messages, external service calls, and rules that depend on a particular user interface. Triggers can hide behavior from application developers, so they should remain focused and documented.
A trigger is not a replacement for every constraint. If a rule must always hold, prefer a constraint when one can express it. Use a trigger when the required action involves procedural logic or must react automatically to an event.
Choosing a Trigger Deliberately
Before creating one, identify the event, timing, level, and required action. Ask whether a constraint or application service would express the rule more clearly. Then check whether the trigger should affect every row or only the statement as a whole.
Exercise
A rule must reject every attempt to insert a negative salary, regardless of which application sends the statement. Is a database trigger automatically the best first choice?
Show answer
No. A CHECK constraint is usually the better first choice because it directly expresses the invariant. Use a trigger only when the rule needs procedural behavior that a constraint cannot provide.