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

3 Triggers

Uploaded by

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

3 Triggers

Uploaded by

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

Explain triggers in context to PL/SQL or what are triggers in PL/SQL?

PL/SQL: Triggers and Their Types

What is a Trigger?

A trigger is like an automatic alarm. It runs by itself whenever something happens in the
database, such as when a table is updated, a new row is inserted, or a row is deleted.

Think of it as a program that automatically executes when a specific event occurs in the
database.

1. Triggers are automatic programs that run when specific events happen in the database.
2. BEFORE triggers are used to modify or validate data before changes are made.
3. AFTER triggers are used to log or perform actions after the event occurs.

Types of Triggers

Triggers are categorized based on timing and event.

Type When It Runs Event


BEFORE Before an operation (INSERT, UPDATE, Happens before the event
Trigger DELETE) occurs
AFTER Trigger After an operation (INSERT, UPDATE, Happens after the event
DELETE) occurs

Differences Between BEFORE and AFTER Triggers

Feature BEFORE Trigger AFTER Trigger


Execution Runs before the operation Runs after the operation
Timing (INSERT/UPDATE/DELETE) completes
Use Case To validate or modify data before saving it To log changes or perform
actions after the event
Effect on Can modify the data being inserted or Cannot change the data because
Data updated it already happened

Unit: 4 - PL/SQL control statements and stored procedures Dr. Sulbha Gath Page 1
Example 1: BEFORE Trigger

Task: Automatically capitalize the name of a student before adding it to the table.

Code:

CREATE OR REPLACE TRIGGER before_insert_student


BEFORE INSERT ON students
FOR EACH ROW
BEGIN
:NEW.name := UPPER(:NEW.name);
END;

Explanation:
1. BEFORE INSERT ON students: The trigger runs before a new row is inserted into the
students table.
2. :NEW.name: Refers to the value being inserted into the name column.
3. UPPER(:NEW.name): Changes the name to uppercase.
What Happens?
 If you insert John into the table, it automatically becomes JOHN.

Example 2: AFTER Trigger

Task: Log a message after a new student is added.

Code:

CREATE OR REPLACE TRIGGER after_insert_student


AFTER INSERT ON students
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('A new student has been added: ' ||
:NEW.name);
END;

Explanation:
1. AFTER INSERT ON students: The trigger runs after a new row is added to the students
table.
2. :NEW.name: Refers to the name of the student just added.
3. DBMS_OUTPUT.PUT_LINE: Prints a message about the new student.
What Happens?
 If you add a student John, it prints:
A new student has been added: John

Unit: 4 - PL/SQL control statements and stored procedures Dr. Sulbha Gath Page 2

You might also like