0% found this document useful (0 votes)
98 views5 pages

9 Trigger-1

The document describes creating triggers in Oracle. It provides examples of: 1. Creating a trigger that inserts the current username into a table on row insertion. 2. Creating a trigger that prevents any insert, update, or delete operations on a table. 3. Creating a trigger that raises a user-defined error if the salary is changed during an update. 4. A query to drop a created trigger is also provided.

Uploaded by

Pawas Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views5 pages

9 Trigger-1

The document describes creating triggers in Oracle. It provides examples of: 1. Creating a trigger that inserts the current username into a table on row insertion. 2. Creating a trigger that prevents any insert, update, or delete operations on a table. 3. Creating a trigger that raises a user-defined error if the salary is changed during an update. 4. A query to drop a created trigger is also provided.

Uploaded by

Pawas Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise Number: 9

Title of the Exercise : TRIGGER


Date of the Exercise :

OBJECTIVE (AIM) OF THE EXPERIMENT


To create triggers for various events such as insertion, updation, etc.,
FACILITIES REQUIRED AND PROCEDURE
a) Facilities required to do the experiment:
Sl.No. Facilities Quantity
required
1 System 1
2 Operating System Windows
3 Front end VB/VC ++/JAVA
4 Back end Oracle11g,my SQL, DB2
b) PL/SQL Syntax:
TRIGGER
A Trigger is a stored procedure that defines an action that the database automatically
take when some database-related event such as Insert, Update or Delete occur.

TRIGGER VS. PROCEDURE VS CURSOR

TRIGGER PROCEDURES CURSORS


These are named These are named These are named PL/SQL
PL/SQL blocks. PL/SQL blocks. blocks.
These are invoked User as per need invokes These can be created both
automatically. these. explicitly and implicitly.
These can’t take These can take These can take parameters.
parameters. parameters.
These are stored in These are stored in These are not stored in
database. database. database.

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.

VARIABLES USED IN TRIGGERS


•:new
•:old
These two variables retain the new and old values of the column updated in the
database. The values in these variables can be used in the database triggers for data
manipulation
Row Level Trigger vs. Statement Level Trigger:
Row Level Trigger Statement Level Trigger
These are fired for each row affected These are fired once for the statement
by the DML statement. instead of the no of rows modified by it.
These are used for generating/checking These are used for generated the
the values begin inserted or updated. summary information.

Before trigger vs. after trigger

Before Triggers After Triggers


Before triggers are fired before the After triggers are fired after the
DML statement is actually executed. DML statement has finished
execution.

Sytax:

Create or replace trigger <trg_name> Before /After Insert/Update/Delete


[of column_name,
column_name….] on <table_name>
[for each row] [when condition]
begin ---statement
end;

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'

SQL> delete from itempls where ename='xxx';


delete from itempls where ename='xxx'
*
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'

SQL> update itempls set eid=15 where ename='yyy';


update itempls set eid=15 where ename='yyy'
*
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

Q4: develop a query to Drop the Created Trigger


Ans:
SQL> drop trigger ittrigg;
Trigger dropped.

f)Result:
Thus the creation of triggers for various events such as insertion, updation, etc., was performed
and executed successfully.

You might also like