0% found this document useful (0 votes)
5 views

Lab 10 - Triggers

Trigger

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 10 - Triggers

Trigger

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Triggers

Database Triggers
• Database triggers are program units that execute in
response to the database events of inserting,
updating, or deleting a record.

• Triggers may be used to supplement declarative


referential integrity, to enforce complex business
rules, to audit changes to data, or to signal to other
programs that changes were made to a table.

• The code within a trigger body is made up of


PL/SQL. The execution of triggers is transparent to
the users.
Difference between triggers and other program units

• Triggers cannot accept input parameters

• Program units other than triggers have to be


explicitly executed by typing the program unit
name at the command prompt or in a command
in a calling procedure.

• Trigger executes only when its triggering event


occurs. When a trigger executes, it is said to have
fired.
Types of Triggers
Row-Level Triggers
• Row-level triggers execute once for each row
in a transaction.

• They are created using the for each row clause


in the create trigger command.
Statement-Level Triggers
• Statement-level triggers execute once for each
transaction, either before or after the SQL
triggering statement executes.
Before and After Triggers
• Since the events that execute triggers are
database transactions, triggers can be
executed immediately before or after inserts,
updates, and deletes.
INSTEAD OF Triggers
• You can use INSTEAD OF triggers to tell
ORACLE what to do instead of performing the
actions that executed the trigger.

• For example, you could use an INSTEAD OF


trigger to redirect table inserts into a different
table.
Valid Trigger Types
1. BEFORE INSERT row
2. BEFORE INSERT statement
3. AFTER INSERT row
4. AFTER INSERT statement

5. BEFORE UPDATE row


6. BEFORE UPDATE statement
7. AFTER UPDATE row
8. AFTER UPDATE statement

9. BEFORE DELETE row


10. BEFORE DELETE statement
11. AFTER DELETE row
12. AFTER DELETE statement

13. INSTEAD OF row


14. INSTEAD OF statement
Trigger Syntax
Create [or replace] trigger [user.] trigger
{before | after | instead of }
{delete| insert| update [of column [, column [,
column] … ] }
[or {delete| insert| update [of column [, column] …}
} ] … on [user.] {TABLE | VIEW}
[ [ referencing {old [as] old | new [as] new} … ]
for each {row | statement} [when (condition) ]]
PL/SQL BLOCK;
Example 1 : Creating a Trigger that Logs Table Changes

CREATE TABLE EMPLOYEES_LOG (


log_date DATE ,
action VARCHAR2(50),
oldid number (6),
Newid number(6)
);
Example 1: Creating a Trigger that Logs Table Changes (con.)

CREATE OR REPLACE TRIGGER EMPLOYEES_CHANGE_TRIGGER


AFTER INSERT OR UPDATE OR DELETE ON EMPLOYEES referencing old
as old new as new for each row
DECLARE
log_action EMPLOYEES_LOG.action%TYPE;
BEGIN
IF INSERTING THEN
log_action := 'Insert';
ELSIF UPDATING THEN
log_action := 'Update';
ELSIF DELETING THEN
log_action := 'Delete';
ELSE
DBMS_OUTPUT.PUT_LINE('This code is not reachable.');
END IF;

INSERT INTO EMPLOYEES_LOG VALUES (SYSDATE,


log_action, :OLD.EMPLOYEE_ID, :NEW.EMPLOYEE_ID);
END;
OLD and NEW Pseudorecords
• For an INSERT trigger:
– OLD contains no values, and NEW contains the new
values.

• For an UPDATE trigger:


– OLD contains the old values, and NEW contains the
new values.

• For a DELETE trigger:


– OLD contains the old values, and NEW contains no
values.
Example 2: Creating Triggers that Log LOGON and LOGOFF Events

CREATE TABLE hr_users_log (


user_name VARCHAR2(30),
activity VARCHAR2(20),
event_date DATE
);
Example 2: Creating Triggers that Log LOGON and LOGOFF Events (con.)

CREATE OR REPLACE TRIGGER logon_trigger


AFTER LOGON ON HR.SCHEMA
BEGIN
INSERT INTO hr_users_log (user_name,
activity, event_date)
VALUES (USER, 'LOGON', SYSDATE);
END;
Example 2: Creating Triggers that Log LOGON and LOGOFF Events (con.)

CREATE OR REPLACE TRIGGER logoff_trigger


BEFORE LOGOFF ON HR.SCHEMA
BEGIN
INSERT INTO hr_users_log (user_name,
activity, event_date)
VALUES (USER, 'LOGOFF', SYSDATE);
END;
Disabling or Enabling Triggers
• Disabling or Enabling a Single Trigger
– ALTER TRIGGER trigger_name DISABLE;
– ALTER TRIGGER trigger_name ENABLE;

• Disabling or Enabling All Triggers on a Single Table


– ALTER TABLE table_name DISABLE ALL
TRIGGERS;
– ALTER TABLE table_name ENABLE ALL TRIGGERS;
Dropping Triggers

– DROP TRIGGER trigger_name

You might also like