Triggers
Triggers
Triggers
Triggers are special types of stored procedures in PL/SQL that are automatically executed in
response to certain events or changes in a database. These events could include data
manipulation language (DML) statements like INSERT, UPDATE, and DELETE or data definition
language (DDL) statements like CREATE, ALTER, and DROP.
Trigger Syntax
The syntax for creating a trigger in PL/SQL is as follows:
Trigger Event
The trigger event is the event that causes the trigger to execute. In PL/SQL, triggers can be
defined on INSERT, UPDATE, and DELETE events. These events are defined using the
BEFORE or AFTER keyword in the trigger definition.
Trigger Table
The trigger table is the table that the trigger is defined on. This is specified using the ON
keyword in the trigger definition.
A BEFORE INSERT trigger is executed before a new row is inserted into the table. Here is an
example of a BEFORE INSERT trigger that validates the salary of a new employee:
A BEFORE UPDATE trigger is executed before an existing row is updated. Here is an example
of a BEFORE UPDATE trigger that prevents updates to the hire_date column:
BEFORE DELETE trigger is executed before an existing row is deleted. Here is an example of a
BEFORE DELETE trigger that prevents the deletion of employees with a higher salary:
"Before insert" trigger for the employees table that sets a default value for the salary
column:
CREATE OR REPLACE TRIGGER trg_set_default_salary
BEFORE INSERT
ON oehr_employees
FOR EACH ROW
DECLARE
BEGIN
IF :new.salary IS NULL THEN
:new.salary := 5000;
END IF;
END;
"Before insert" trigger for the jobs table that sets a default value for the max_salary
column:
CREATE OR REPLACE TRIGGER trg_set_default_max_salary
BEFORE INSERT
ON oehr_jobs
FOR EACH ROW
DECLARE
BEGIN
IF :new.max_salary IS NULL THEN
:new.max_salary := 20000;
END IF;
END;
UPDATE oehr_jobs
SET min_salary = 2500
WHERE job_id = 'AD_ASST';
"After update" trigger for the departments table that logs changes made to the
location_id column:
UPDATE oehr_departments
SET location_id = 1700
WHERE department_id = 20;
BEFORE INSERT
Example 1: employees table
Suppose you want to create a before insert trigger for the employees table that sets the hire
date to the current date if no hire date is provided. Here's how you can do it:
AFTER INSERT
Example 1: employees table
Suppose you want to create an after insert trigger for the employees table that adds a record to
the jobs table whenever a new job is added to the employees table. Here's how you can do it:
CREATE OR REPLACE TRIGGER add_job_to_jobs_table
CREATE OR REPLACE TRIGGER add_job_to_jobs_table
AFTER INSERT ON oehr_employees
FOR EACH ROW
BEGIN
INSERT INTO oehr_jobs (job_id, min_salary, max_salary)
VALUES (:NEW.job_id, :NEW.salary, :NEW.salary);
END;
INSERT INTO employees (employee_id, first_name, last_name, email, phone_number,
hire_date, job_id, salary, commission_pct, manager_id, department_id)
VALUES (1001, 'John', 'Doe', 'jdoe@example.com', '555-1234', SYSDATE, 'ANALYST', 5000,
NULL, 100, 50);
BEFORE DELETE
BEFORE UPDATE
Example 1: employees table
Suppose you want to create a before update trigger for the employees table that prevents any
employee from being given a salary increase greater than 10% of their current salary. Here's
how you can do it:
CREATE OR REPLACE TRIGGER prevent_salary_increase
BEFORE UPDATE OF salary ON oehr_employees
FOR EACH ROW
BEGIN
IF :NEW.salary > (:OLD.salary * 1.1) THEN
RAISE_APPLICATION_ERROR(-20001, 'A salary increase of more than 10% is not
allowed.');
END IF;
END;
BEFORE INSERT
Example 1: employees table
Suppose you want to create a before insert trigger for the employees table that sets the hire
date to the current date if no hire date is provided. Here's how you can do it:
AFTER DELETE
Example 1: customers table
Suppose you want to create an after delete trigger for the customers table that removes any
orders associated with the deleted customer from the orders table. Here's how you can do it:
AFTER UPDATE
Example 1: employees table
Suppose you want to create an after update trigger for the employees table that updates the
salary of any employee who has been promoted. Here's how you can do it:
CREATE OR REPLACE TRIGGER update_salary_on_promotion
AFTER UPDATE ON employees
FOR EACH ROW
WHEN (NEW.job_id <> OLD.job_id AND NEW.salary > OLD.salary)
BEGIN
UPDATE employees SET salary = :NEW.salary WHERE employee_id = :OLD.employee_id;
END;
/
AFTER INSERT
Example 1: employees table
Suppose you want to create an after insert trigger for the employees table that adds a record to
the jobs table whenever a new job is added to the employees table. Here's how you can do it:
CREATE OR REPLACE TRIGGER add_job_to_jobs_table
CREATE OR REPLACE TRIGGER add_job_to_jobs_table
AFTER INSERT ON oehr_employees
FOR EACH ROW
BEGIN
INSERT INTO oehr_jobs (job_id, min_salary, max_salary)
VALUES (:NEW.job_id, :NEW.salary, :NEW.salary);
END;
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: