0% found this document useful (0 votes)
19 views2 pages

U 2 Oracle

A Trigger in Oracle is a stored program that automatically executes in response to specific database events such as INSERT, UPDATE, or DELETE. It consists of a triggering event, condition, and action, and can be categorized into statement-level or row-level triggers, as well as BEFORE and AFTER triggers. Triggers are used for automatic execution, ensuring data integrity, auditing changes, and preventing invalid actions.

Uploaded by

sanketb6543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

U 2 Oracle

A Trigger in Oracle is a stored program that automatically executes in response to specific database events such as INSERT, UPDATE, or DELETE. It consists of a triggering event, condition, and action, and can be categorized into statement-level or row-level triggers, as well as BEFORE and AFTER triggers. Triggers are used for automatic execution, ensuring data integrity, auditing changes, and preventing invalid actions.

Uploaded by

sanketb6543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-----------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
-----------------Trigger in Oracle
---------------------------------------------------------
What is a Trigger?
A Trigger is a special kind of stored program in Oracle that automatically runs
when a specific event occurs in the database. Unlike stored procedures, triggers
execute on their own when a condition is met.
--------------------------------------------------------------
Why Use Triggers?
✅ Automatic Execution – Runs automatically when a database event happens (like
INSERT, UPDATE, DELETE).
✅ Data Integrity – Ensures that correct and consistent data is stored in the
database.
✅ Audit and Security – Keeps track of changes made to important tables.
✅ Prevention of Invalid Actions – Restricts unwanted modifications to data.
---------------------------------------------------------------------
Components of a Trigger
A trigger has three main parts:
1️
.Triggering Event – Defines when the trigger should fire (e.g., INSERT, UPDATE,
DELETE).
2️
.Triggering Condition – Defines the specific condition for execution.
3️
.Trigger Action – The code that runs when the event occurs.
------------------------------------------------------------------------
[Types of Triggers in Oracle]
->(1. Statement-Level Trigger)
-A statement-level trigger runs only once for a SQL statement, no matter how many
rows are affected.
-It does not check individual rows.

🔹 Example Scenario:
Imagine running an UPDATE command that modifies 100 rows in a table. A statement-
level trigger fires only once, not 100 times.

🔹 Example:
CREATE OR REPLACE TRIGGER statement_trigger_example
AFTER UPDATE ON employees
BEGIN
DBMS_OUTPUT.PUT_LINE('Employees table updated!');
END;

->(2. Row-Level Trigger)


-A row-level trigger fires once for each row affected by the SQL statement.
-If 5 rows are updated, the trigger runs 5 times (once per row).

🔹 Example Scenario:
If an UPDATE statement changes 3 rows, a row-level trigger runs 3 times (once per
row).

🔹 Example:
CREATE OR REPLACE TRIGGER row_trigger_example
AFTER UPDATE ON employees
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Row updated: ' || :OLD.id);
END;

->(3. BEFORE Trigger)


-A BEFORE trigger runs before the actual SQL command is executed.
-It is useful for data validation (e.g., preventing negative salaries).

🔹 Example Scenario:
Before inserting a new employee, check if their salary is greater than zero.

🔹 Example:
CREATE OR REPLACE TRIGGER before_insert_salary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF :NEW.salary < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be negative!');
END IF;
END;

->(4. AFTER Trigger)


-An AFTER trigger runs after the SQL statement is executed.
-It is useful for logging, auditing, or tracking changes.

🔹 Example Scenario:
After deleting an employee, store their details in a backup table.

🔹 Example:
CREATE OR REPLACE TRIGGER after_delete_employee
AFTER DELETE ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_backup (id, name, salary)
VALUES (:OLD.id, :OLD.name, :OLD.salary);
END;
/

You might also like