Oracle
DB Trigger
In Oracle, a database trigger is a stored PL/SQL block that is automatically executed (or "triggered") in response to certain events on a particular table or view. Triggers can be used for various purposes, such as enforcing business rules, validating input data, or automatically updating related tables. They are powerful tools for ensuring data integrity and automating tasks within the database.
Here’s a brief overview of database triggers:
Types of Triggers
- BEFORE Trigger: Executes before an insert, update, or delete operation on a table.
- AFTER Trigger: Executes after an insert, update, or delete operation on a table.
- INSTEAD OF Trigger: Executes instead of an insert, update, or delete operation, commonly used with views.
Creating a Trigger
- To create a trigger, you use the
CREATE TRIGGERstatement. Here’s a basic example that demonstrates how to create a trigger:
CREATE OR REPLACE TRIGGER before_book_insert BEFORE INSERT ON books FOR EACH ROW BEGIN IF :NEW.title IS NULL THEN RAISE_APPLICATION_ERROR(-20001, 'Title cannot be NULL'); END IF; END;- This trigger, named
before_book_insert, ensures that a row cannot be inserted into thebookstable without a title.
- To create a trigger, you use the
Trigger Timing
- Triggers can be set to fire BEFORE or AFTER an operation. You specify this timing using keywords in the
CREATE TRIGGERstatement.
- Triggers can be set to fire BEFORE or AFTER an operation. You specify this timing using keywords in the
Trigger Event
- Specify the event (INSERT, UPDATE, DELETE) that will activate the trigger. This is defined in the
CREATE TRIGGERstatement.
- Specify the event (INSERT, UPDATE, DELETE) that will activate the trigger. This is defined in the
FOR EACH ROW vs. FOR EACH STATEMENT
- FOR EACH ROW: The trigger fires once for each row affected by the SQL statement.
- FOR EACH STATEMENT: The trigger fires once for the entire SQL statement, regardless of the number of rows affected.
Disabling and Dropping Triggers
- To disable a trigger:
ALTER TRIGGER before_book_insert DISABLE; - To drop a trigger:
DROP TRIGGER before_book_insert;
- To disable a trigger:
Example: Inserting Data into
booksTable- The following SQL statement will activate the trigger, demonstrating how it prevents insertion of a book without a title:
INSERT INTO books (book_id, title, author_id) VALUES (1, NULL, 101); -- This will raise an error due to the trigger
This introduction to database triggers provides a foundation for understanding how they work in Oracle and how you can use them to enforce rules and automate tasks in your database environment.
INSERT Trigger:
An INSERT trigger is executed automatically when a new record is inserted into a table.
Example:
Suppose we have a table namedemployeeswith columnsid,name, andsalary. We want to log each new employee's information into another table calledemployee_logswhenever a new record is inserted into theemployeestable.
CREATE OR REPLACE TRIGGER log_new_employee
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_logs (action, employee_id, employee_name, salary)
VALUES ('INSERT',:NEW.id,:NEW.name,:NEW.salary);
END;
UPDATE Trigger:
An UPDATE trigger is executed automatically when a record in the table is updated.
Example:
Let's say we have a table namedaudit_trailwhere we want to keep track of all changes made to theemployeestable. We'll create an UPDATE trigger to log the old and new values of the updated record.
CREATE OR REPLACE TRIGGER log_employee_update
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_trail (action, employee_id, old_salary, new_salary)
VALUES ('UPDATE',:OLD.id,:OLD.salary,:NEW.salary);
END;
DELETE Trigger:
A DELETE trigger is executed automatically when a record is deleted from the table.
Example:
Suppose we have a table nameddeleted_employeeswhere we want to move all deleted employee records for archival purposes. We'll create a DELETE trigger to automatically insert deleted records into thedeleted_employeestable.
CREATE OR REPLACE TRIGGER move_deleted_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO deleted_employees (employee_id, employee_name, deleted_at)
VALUES (:OLD.id,:OLD.name, SYSDATE);
END;

Comments Not Found