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

Module 5 Session 3

Triggers in PL/SQL are stored programs that automatically execute in response to specified events, with classifications based on timing, level, and event type. The document provides examples of creating triggers, enabling/disabling them, and mentions the use of a dictionary for triggers. Additionally, it briefly touches on database security measures to protect against unauthorized access and threats.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 5 Session 3

Triggers in PL/SQL are stored programs that automatically execute in response to specified events, with classifications based on timing, level, and event type. The document provides examples of creating triggers, enabling/disabling them, and mentions the use of a dictionary for triggers. Additionally, it briefly touches on database security measures to protect against unauthorized access and threats.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

What are Triggers in PL/SQL?

• Triggers are stored programs that are fired


automatically when some events occur. The
code to be fired can be defined as per the
requirement.
• Oracle has also provided the facility to
mention the event upon which the trigger
needs to be fire and the timing of the
execution.
Types of Triggers in Oracle

• Triggers can be classified based on the following parameters.


• Classification based on the timing
– BEFORE Trigger: It fires before the specified event has occurred.
– AFTER Trigger: It fires after the specified event has occurred.
• Classification based on the level
– STATEMENT level Trigger: It fires one time for the specified event
statement.
– ROW level Trigger: It fires for each record that got affected in the
specified event. (only for DML)
• Classification based on the Event
– DML Trigger: It fires when the DML event is specified
(INSERT/UPDATE/DELETE)
– DDL Trigger: It fires when the DDL event is specified (CREATE/ALTER)
– DATABASE Trigger: It fires when the database event is specified
(LOGON/LOGOFF/STARTUP/SHUTDOWN)
• So each trigger is the combination of above parameters.
How to Create Trigger

• Below is the syntax for creating a trigger.


EXAMPLE ON TRIGGER

Create or replace trigger trig_error


before insert or update or delete
on emp
begin
if to_char(sysdate,'DY') IN ('MON','TUE') then
raise_application_error(-20111,'No changes can be made on
tuesday');
dbms_output.put_line(‘Cant insert/delete on Tuesday’);
end if;
end;
Dbms_output.put_line(‘no changes can be done on MON and
TUE!!’);
EXAMPLE-2
Create or replace trigger depttrig
AFTER INSERT or ON dept
FOR EACH ROW
BEGIN
INSERT INTO dept_log
VALUES(:new.deptno,:new.dname,:new.loc);
END;
EXAMPLE-3
Create or replace trigger dept_trig
BEFORE update or delete on dept
FOR EACH ROW
BEGIN
INSERT INTO dept_log
VALUES(:old.deptno,:old.dname,:old.loc);
END;
CREATE OR REPLACE TRIGGER emp_count
AFTER DELETE ON EMP
DECLARE
n INTEGER;
BEGIN
SELECT COUNT(*) INTO n FROM emp;
DBMS_OUTPUT.PUT_LINE(' There are now ' || n
|| ' employees.');
END;
convert ename to upper

CREATE OR REPLACE TRIGGER before_emp


BEFORE UPDATE OR INSERT ON emp
FOR EACH ROW
begin
/* convert character values to upper case */
:new.ename := upper( :new.ename );
:new.job := upper( :new.job);
end;
• Dictionary for triggers is user_triggers
Select trigger_name,trigger_body,table_name,description
from user_triggers

Enabling/Diasabling Triggers
To enable a disabled trigger, use the ALTER TRIGGER statement with the
ENABLE clause.

For example, to enable the disabled trigger:

ALTER TRIGGER trig_error ENABLE;

To enable all triggers defined for a specific table, use the ALTER TABLE
statement with the ENABLE clause and the ALL TRIGGERS option.

ALTER TABLE emp ENABLE ALL TRIGGERS;


• Disabling Triggers
• You might temporarily disable a trigger if:

• You must perform a large data load, and you want it to


proceed quickly without firing triggers.

• ALTER TRIGGER trig_error DISABLE;

• To disable all triggers defined for a specific table, use the


ALTER TABLE statement with the DISABLE clause and the ALL
TRIGGERS option. For example, to disable all triggers defined
for the Inventory table, enter the following statement:

• ALTER TABLE emp DISABLE ALL TRIGGERS;


• Database security refers to the collective
measures used to protect and secure a
database or database management
software from illegitimate use and malicious
threats and attacks.
• It is a broad term that includes a multitude
of processes, tools and methodologies that
ensure security within
database environment.

You might also like