Triggers in PL/SQL are designed to automatically perform actions in response to specific events on a table, such as INSERT, UPDATE, or DELETE.
However, there are times when you may need to temporarily disable these triggers, To perform updates or to speed up large data imports. In this article, We will learn about the PL/SQL DISABLE triggers by understanding various examples.
PL/SQL DISABLE Triggers
PL/SQL DISABLE Triggers is a command used to turn off triggers in your database. When a trigger is disabled, it won’t run automatically when you make changes to the data. This is useful if you want to stop the trigger’s actions temporarily, such as during large updates or maintenance. You can later turn the trigger back on using the ENABLE command.
Syntax of the DISABLE triggers:
Disable Trigger
ALTER TRIGGER trigger_name DISABLE;
Enable Trigger
ALTER TRIGGER trigger_name ENABLE;
- ALTER TRIGGER: The command used to modify the trigger's status.
- trigger_name: The name of the trigger you want to disable.
Using the DISABLE Triggers in PL/SQL
Disabling triggers in PL/SQL stops them from executing, allowing you to perform operations or maintenance without their automatic actions interfering.
Step 1: Create Tables and a Trigger
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50),
salary NUMBER
);
CREATE TABLE employee_updates_log (
log_id NUMBER PRIMARY KEY,
employee_id NUMBER,
old_salary NUMBER,
new_salary NUMBER,
log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE SEQUENCE log_seq START WITH 1 INCREMENT BY 1;
CREATE OR REPLACE TRIGGER log_employee_update
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_updates_log (log_id, employee_id, old_salary, new_salary)
VALUES (log_seq.NEXTVAL, :OLD.employee_id, :OLD.salary, :NEW.salary);
END;
Step 2: Next, we'll insert some initial data into the employees table.
INSERT INTO employees (employee_id, name, salary) VALUES (101, 'Alice', 5000);
INSERT INTO employees (employee_id, name, salary) VALUES (102, 'Bob', 6000);
INSERT INTO employees (employee_id, name, salary) VALUES (103, 'Charlie', 7000);
Example 1: Update an employee's salary while the trigger is enabled.
UPDATE employees SET salary = 5500 WHERE employee_id = 101;
Output:
Log ID | Employee ID | Old Salary | New Salary | Log Date |
---|
1 | 101 | 5000 | 5500 | (Current Timestamp) |
Explanation: The trigger logs the change in salary, capturing both the old and new values in the employee_updates_log table.
Example 2: Disable the trigger and perform a bulk salary update.
ALTER TRIGGER log_employee_update DISABLE;
UPDATE employees SET salary = salary * 1.10;
Output: No new entries are added to the employee_updates_log table since the trigger is disabled.
Explanation: With the trigger disabled, the bulk update proceeds without logging any changes, which is often necessary for performance reasons during large-scale operations.
Example 3: Now, re-enable the trigger and update another employee's salary.
ALTER TRIGGER log_employee_update ENABLE;
UPDATE employees SET salary = 6500 WHERE employee_id = 102;
Output:
Log ID | Employee ID | Old Salary | New Salary | Log Date |
---|
2 | 102 | 6600 | 6500 | (Current Timestamp) |
Explanation: Once re-enabled, the trigger resumes logging updates, ensuring that any further changes to employee data are captured.
Conclusion
Disabling triggers in PL/SQL is a technique for managing large-scale data operations and maintenance tasks. By temporarily disabling triggers, you can prevent performance issues and unplanned consequences during bulk updates. The examples provided show how to use this technique effectively, ensuring that your database operations run smoothly without sacrificing the integrity of your data.
Similar Reads
PL/SQL Enable Triggers
In PL/SQL, triggers are special stored procedures that automatically execute in response to certain events on a particular table or view. These triggers can be used to enforce complex business rules, maintain integrity constraints, or track changes in the database. However, triggers can be enabled o
6 min read
PL/SQL Drop Triggers
In database management, triggers play an important role in maintaining data integrity and enforcing business rules automatically. However, there may be situations where we need to remove an existing trigger from a database. This process is accomplished using the DROP TRIGGER statement in PL/SQL.In t
4 min read
PL/SQL Triggers
PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features.PL/SQL supports SQL queries. It also supports the declaration of the variables, control statements, Functions, Records, Cursor, Procedure, and Triggers.PL/SQL contains a declaration section,
6 min read
PL/SQL Instead of Triggers
PL/SQL INSTEAD OF Triggers are a special type of trigger used primarily with views to allow operations like INSERT, UPDATE, and DELETE. These triggers provide a way to perform complex operations that might not be possible directly on a view, enabling you to maintain data integrity and enforce busine
4 min read
PL/SQL Row-Level Triggers
In PL/SQL, triggers are special stored procedures that automatically execute in response to specific events on a table or view. Among these, row-level triggers are particularly useful for enforcing business rules, maintaining data integrity, and automating tasks. This article explores row-level trig
5 min read
SQL Triggers
SQL triggers are essential in database management systems (DBMS). They enable SQL statements to run when specific database events occur such as when someone adds, changes, or removes data. Triggers are commonly used to maintain data integrity, track changes, and apply business rules automatically, w
8 min read
MySQL DROP Trigger
In MySQL, triggers automatically perform actions when events like INSERT, UPDATE, or DELETE occur on a table However, there are situations where a trigger may not be necessary and its logic may need to be updated. In such cases, MySQL provides the DROP TRIGGER statement to delete an existing trigger
4 min read
PL/SQL Statement level Triggers
Statement-level triggers in Oracle databases execute actions for each transaction, responding to various database events like DML and DDL statements, system events, and user interactions. They act as programmed responses to specific table events, enhancing database management and automation. Stored
4 min read
MySQL Create Trigger
Database triggers are specialized procedures that automatically respond to certain events on a table or view. These events include actions such as INSERT, UPDATE, or DELETE. Triggers can be used to enforce complex business rules, maintain audit trails, or synchronize data across tables. In MySQL, tr
4 min read
MySQL AFTER DELETE Trigger
In MySQL, triggers are a concept where SQL developers can define automatic actions to occur in response to specific changes in a database table. An AFTER DELETE trigger, for instance, is invoked automatically after a row is deleted from a table, allowing developers to perform additional operations s
4 min read