9 Trigger-1
9 Trigger-1
TYPES OF TRIGGERS
The various types of triggers are as follows,
•Before: It fires the trigger before executing the trigger statement.
•After: It fires the trigger after executing the trigger statement.
•For each row: It specifies that the trigger fires once per row.
• For each statement: This is the default trigger that is invoked. It specifies that the
trigger fires once per statement.
Sytax:
Q1: Create a trigger that insert current user into a username column of an existing table
c) Procedure for doing the experiment:
Step
Details of the step
no.
1 Create a table itstudent4 with name and username as arguments
2 Create a trigger for each row that insert the current user as user name into a table
3 Execute the trigger by inserting value into the table
d)Program:
SQL> create table itstudent4(name varchar2(15),username varchar2(15));
Table created.
SQL> create or replace trigger itstudent4 before insert on itstudent4 for each row
2 declare
3 name varchar2(20);
4 begin
5 select user into name from dual;
6 :new.username:=name;
7 end;
8 /
Trigger created.
e)Output:
SQL> insert into itstudent4 values('&name','&username');
Enter value for name: akbar Enter value for username:
ranjani old 1: insert into itstudent4
values('&name','&username') new 1: insert into
itstudent4 values('akbar','ranjani') 1 row created.
SQL> /
Enter value for name: suji
Enter value for username:
priya
old 1: insert into itstudent4 values('&name','&username')
new 1: insert into itstudent4 values('suji','priya')
1 row created.
SQL> select * from itstudent4;
NAME USERNAME
--------------- ---------------
akbar SCOTT
suji SCOTT
Q2: Create a Simple Trigger that does not allow Insert Update and Delete Operations on
the Table
d)Program:
Table used:
SQL> select * from itempls;
ENAME EID SALARY
---------- --------- ---------
xxx 11 10000 yyy
12 10500 zzz 13
15500 Trigger:
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5
6 /
Trigger created.
e)Output:
SQL> insert into itempls values('aaa',14,34000);
insert into itempls values('aaa',14,34000)
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.ITTRIGG", line 2
ORA-04088: error during execution of trigger 'STUDENT.ITTRIGG'
Q3: Create a Trigger that raises an User Defined Error Message and does not allow
updating and Insertion
d)Program:
Table used:
SQL> select * from itempls;
ENAME EID SALARY
---------- --------- ---------
xxx 11 10000 yyy
12 10500 zzz 13
15500 Trigger:
SQL> create trigger ittriggs before insert or update of salary on itempls for each row
2 declare
3 triggsal itempls.salary%type;
4 begin
5 select salary into triggsal from itempls where eid=12;
6 if(:new.salary>triggsal or :new.salary<triggsal) then
7 raise_application_error(-20100,'Salary has not been changed'); 8 end if;
9 end;
10 /
Trigger created.
e)Output:
SQL> insert into itempls values
('bbb',16,45000); insert into itempls values
('bbb',16,45000) *
ERROR at line 1:
ORA-04098: trigger 'STUDENT.ITTRIGGS' is invalid and failed re-validation
SQL> update itempls set eid=18 where ename='zzz';
update itempls set eid=18 where ename='zzz'
*
ERROR at line 1:
ORA-04298: trigger 'STUDENT.ITTRIGGS' is invalid and failed re-validation
f)Result:
Thus the creation of triggers for various events such as insertion, updation, etc., was performed
and executed successfully.