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

Triggers

Uploaded by

Rawat Vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Triggers

Uploaded by

Rawat Vivek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Triggers

Triggers are logic that automatically gets executed when certain event happens in database.
By default whenever we talk about trigger we think about DML Triggers that fires when we
do Insert/Update/Delete on a Table.
Apart from DML Triggers on Tables Oracle also supports DDL Triggers, “Instead ff Trigger”
on Views and System Triggers.

System triggers are fired at specific system events such as logon, logoff, database startup,
DDL execution, and servererror triggers

In Summary these are the Types of Triggers present in Oracle:

 DML Triggers
 Instead of Trigger for View
 DDL triggers
 Database startup triggers
 Logon triggers
 Logoff triggers
 Servererror triggers

Refer to below articles for examples on triggers

dba-oracle.com/art_dbazine_sys_trigs.htm
databasejournal.com/features/oracle/article.php/3299231/The-Trigger-Happy-DBA—System-
Triggers.htm

Oracle mutating trigger error – occurs when a trigger references the table that owns the
trigger, resulting in the “ORA-04091: table name is
mutating, trigger/function may not see it.” message.

Query to get details about the triggers


Select * from DBA_TRIGGERS
Below examples capture details of the user who does any DDL changes in database.

DDL triggers script for DDL auditing:

CREATE TABLE AUDIT_DDL (


d date,
OSUSER varchar2(255),
CURRENT_USER varchar2(255),
HOST varchar2(255),
TERMINAL varchar2(255),
owner varchar2(30),
type varchar2(30),
name varchar2(30),
sysevent varchar2(30));

create or replace trigger audit_ddl_trg after ddl on schema


begin
if (ora_sysevent=’TRUNCATE’)
then
null; — I do not care about truncate
else
insert into audit_ddl(d, osuser,current_user,host,terminal,owner,type,name,sysevent)
values(
sysdate,
sys_context(‘USERENV’,'OS_USER’) ,
sys_context(‘USERENV’,'CURRENT_USER’) ,
sys_context(‘USERENV’,'HOST’) ,
sys_context(‘USERENV’,'TERMINAL’) ,
ora_dict_obj_owner,
ora_dict_obj_type,
ora_dict_obj_name,
ora_sysevent
);
end if;
end;
/

Instead of Trigger for Views are created to solve the problem of Inserting data to a View
having multiple tables. With Instead of Trigger we can Insert into Base tables that are part of
the view.

Creating an INSTEAD OF Trigger Example

We are using ORDERS and CUSTOMERS tables in this example. You can create a view on
EMP and DEPT table and similar trigger for Practice.

We have created order_info view to display information about customers and their orders:

CREATE VIEW order_info AS


SELECT c.customer_id, c.cust_last_name, c.cust_first_name,
o.order_id, o.order_date, o.order_status
FROM customers c, orders o
WHERE c.customer_id = o.customer_id;

Normally we can not insert/update this View and trying to do so will throw error.
To make this view updatable, create an INSTEAD OF trigger on the view to process INSERT
statements directed to the view.

CREATE OR REPLACE TRIGGER order_info_insert


INSTEAD OF INSERT ON order_info
DECLARE
duplicate_info EXCEPTION;
PRAGMA EXCEPTION_INIT (duplicate_info, -00001);
BEGIN
INSERT INTO customers
(customer_id, cust_last_name, cust_first_name)
VALUES (
:new.customer_id,
:new.cust_last_name,
:new.cust_first_name);
INSERT INTO orders (order_id, order_date, customer_id)
VALUES (
:new.order_id,
:new.order_date,
:new.customer_id);
EXCEPTION
WHEN duplicate_info THEN
RAISE_APPLICATION_ERROR (
num=> -20107,
msg=> ‘Duplicate customer or order ID’);
END order_info_insert;
/

You can now insert into both base tables through the view (as long as all NOT NULL columns
receive values):

INSERT INTO order_info VALUES


(999, ‘Smith’, ‘John’, 2500, ’13-MAR-2001′, 0);

You might also like