Open In App

PL/SQL Disable Triggers

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article

Similar Reads