Triggers Examples
Triggers Examples
Example 1
Create a transparent system for maintenance of customer records by using trigger. If any
UPDATE OR DELETE operations performed on the table CUST then id, date of operation, type of
operation and username fields are stored in CUST_AUDIT table.
CUST CUST_AUDIT
Field name Data type and width Field name Data type and width
Id Number(5) Id Number(5)
Salary Number(5)
Example 2
Write a database trigger, which will not allow any transaction after office hours
(after 7:00 pm) and on Sunday.
END;
Example 3:
To start with, we will be using the CUSTOMERS table we had created and used in the
previous chapters:
When the above code is executed at SQL prompt, it produces the following result:
Trigger created.
Here following two points are important and should be noted carefully:
• OLD and NEW references are not available for table level triggers, rather you can use
them for record level triggers.
• If you want to query the table in the same trigger, then you should use the AFTER
keyword, because triggers can query the table or change it again only after the initial changes
are applied and the table is back in a consistent state.
• Above trigger has been written in such a way that it will fire before any DELETE or
INSERT or UPDATE operation on the table, but you can write your trigger on a single or
multiple operations, for example BEFORE DELETE, which will fire whenever a record will be
deleted using DELETE operation on the table.
Triggering a Trigger
Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT
statement which will create a new record in the table:
Old salary:
New salary: 7500
Salary difference:
Because this is a new record so old salary is not available and above result is coming
as null. Now, let us perform one more DML operation on the CUSTOMERS table. Here is one
UPDATE statement which will update an existing record in the table:
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
When a record is updated in CUSTOMERS table, above create trigger
display_salary_changes will be fired and it will display following result:
Table created.
Sequence created.
Trigger created.
DECLARE
lpk varchar2(10);
BEGIN
BEGIN
select pk_value into lpk from pkey_lookup;
EXCEPTION
when no_data_found then
lpk := 'C1';
END;
:new.custno := lpk;
lpk := 'C' || to_char(to_number(substr(lpk,2) + 1));
IF lpk = 'C2' THEN
INSERT INTO pkey_lookup VALUES (lpk);
ELSE
UPDATE pkey_lookup SET pk_value = lpk;
END IF;
END;
EXAMPLE 6:
Write a database trigger that not allowing the change in emp table after
Business hours ( From 10:00 AM to 5.00 PM) from Monday to Saturday There is no
restriction on viewing the data .
-----------------------------------------
create or replace trigger timer_03b020
before insert or update or delete
on emp20
begin
if deleting then
raise_application_error(-20000.'Delete restriction.');
end if;
if inserting then
raise_application_error(-20001,'Insert restricted.');
end if;
end if;
if updating then
if to_char(sysdate,'dy')<> 'mon' or
to_char(sysdate,'dy')<> 'tue' or
to_char(sysdate,'dy')<> 'wed' or
to_char(sysdate,'dy')<> 'thu' or
to_char(sysdate,'dy')<> 'fri' or
to_char(sysdate,'dy')<> 'sat' or
to_number(to_char(sysdate,'hh24')) < 10 or
to_number(to_char(sysdate,'hh24')) > 17 then
raise_application_error(-20002,'Updation restricted');
end if;
end if;
end;
/
• TO FIND ERRORS IN PL/SQL :