Forum

What are Triggers i...
 
Share:
Notifications
Clear all

What are Triggers in PL/SQL, and how do they work?


Posts: 71
Guest
Topic starter
(@Vinay Kumar)
Trusted Member
Joined: 4 years ago
 

 

In PL/SQL, a trigger is a stored program that automatically executes in response to specified events on a table or view. Triggers enforce business rules, validate data, and help maintain data integrity within a database. They run automatically when specified events such as INSERT, UPDATE, or DELETE occur on the associated table.

1. Types of Triggers

Triggers in PL/SQL are categorized based on timing and events:

  1. Timing-Based Triggers:

    • BEFORE Triggers: Fire before the triggering event is executed. Commonly used for validating data before making changes.
    • AFTER Triggers: Fire after the triggering event is executed. Used for logging or updating related tables.
  2. Event-Based Triggers:

    • DML Triggers: Execute in response to INSERT, UPDATE, or DELETE operations.
    • DDL Triggers: Fire on Data Definition Language (DDL) events like CREATE, ALTER, or DROP.
    • Instead-of Triggers: Used with views to handle DML operations on complex views that cannot be modified directly.

2. Basic Syntax of a Trigger

A typical trigger structure includes a name, timing, event type, and trigger body.

plsql
 
CREATE OR REPLACE TRIGGER trigger_name
BEFORE | AFTER event_type
ON table_name
FOR EACH ROW
BEGIN
-- Trigger body (code to execute)
END;

3. Example of a Trigger

Suppose we have an employees table, and we want to create a trigger that logs changes to employee salaries:

plsql
 
CREATE OR REPLACE TRIGGER log_salary_changes
AFTER UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
INSERT INTO salary_log (employee_id, old_salary, new_salary, change_date)
VALUES (:OLD.employee_id, :OLD.salary, :NEW.salary, SYSDATE);
END;

Explanation:

  • AFTER UPDATE: The trigger activates after any UPDATE on the salary column.
  • FOR EACH ROW: Ensures the trigger runs for each row affected.
  •  

    and

     

    : These are special bind variables. :OLD.salary holds the previous value, while :NEW.salary holds the updated value.

  • Action: Inserts a log entry in salary_log with the old and new salary values.

4. Benefits of Using Triggers

  • Automatic Execution: Triggers activate without manual intervention, ensuring consistency and data validation in real-time.
  • Data Integrity: Enforce complex business rules that cannot be easily handled by constraints.
  • Audit and Logging: Track changes to sensitive data for security and compliance.

5. Limitations and Best Practices

While triggers are powerful, they can lead to complex behavior if not managed carefully.

  • Performance Impact: Triggers execute automatically and can slow down DML operations if they contain complex logic or interact with other tables.
  • Debugging Complexity: Triggers run automatically, which can complicate debugging. Make sure the logic is straightforward and documented.
  • Avoid Cascading Triggers: Triggers should not initiate other triggers unnecessarily, as it can lead to unpredictable outcomes and performance issues.

6. Using Conditional Logic in Triggers

Triggers can include conditional logic to execute different code depending on specific criteria. For example:

plsql
 
CREATE OR REPLACE TRIGGER check_salary_increase
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
WHEN (NEW.salary > OLD.salary * 1.20)
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'Salary increase exceeds 20% limit');
END;

This trigger raises an error if the new salary is more than 20% higher than the old salary, enforcing a business rule.


Conclusion

PL/SQL triggers provide a robust way to enforce rules, automate audits, and maintain data consistency within Oracle databases. However, overuse or complex triggers can impact performance, so they should be carefully implemented with clear, specific logic. For advanced use cases, consider exploring the Oracle Trigger Documentation for more detailed information.

Leave a reply

Author Name

Author Email

Title *

Preview 0 Revisions Saved
Share: