You are on page 1of 10

Experiment No: - 9

LAB MANUAL
PART A
(PART A: TO BE REFFERED BY STUDENTS)

Experiment Name: - Trigger

Aim: - Performing practical by using trigger concept.

Resource required: - Oracle 9i - iSQLplus

Theory: -

● TRIGGER:
- Triggers are similar to stored procedures.
- A trigger stored in the database can include SQL and PL/SQL or Java statements to run as a
unit and can invoke stored procedures.
- Triggers are implicitly fired by Oracle when a triggering event occurs, no matter which user is
connected or which application is being used.

Syntax:
Create Trigger <trigger name>
(AFTER / BEFORE) <triggering events> ON <table name>
[ FOR EACH ROW ]
[ WHEN< condition> ]
<trigger actions >;
<triggering events> :: = < trigger event> { OR <trigger event>}
<trigger event> :: = Insert/ Delete/ Update [ OF <column name >
{, <column name> } ]
<trigger action > :: = < PL/SQL block>
Types of Triggers
When you define a trigger, you can specify the number of times the trigger action is to be run:
● Once for every row affected by the triggering statement, such as a trigger fired by an
UPDATE statement that updates many rows
● Once for the triggering statement, no matter how many rows it affects

Row Triggers:
A row trigger is fired each time the table is affected by the triggering statement. For
example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for
each row affected by the UPDATE statement. If a triggering statement affects no rows, a row
trigger is not run.
Statement Triggers:
A statement trigger is fired once on behalf of the triggering statement, regardless of the
number of rows in the table that the triggering statement affects, even if no rows are affected. For
example, if a DELETE statement deletes several rows from a table, a statement-level DELETE
trigger is fired only once.

BEFORE and AFTER Triggers:


When defining a trigger, you can specify the trigger timing--whether the trigger action is
to be run before or after the triggering statement. BEFORE and AFTER apply to both statement and
row triggers

Insert Triggers:
A BEFORE OR AFTER INSERT Trigger means that Oracle will fire this trigger before or after
the INSERT operation is executed.

Syntax:
CREATE or REPLACE TRIGGER trigger_name
BEFORE OR AFTER INSERT
ON <table name>
[FOR EACH ROW]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
Update Triggers:
A BEFORE OR AFTER UPDATE Trigger means that Oracle will fire this trigger before the
UPDATE operation is executed.

Syntax:
CREATE or REPLACE TRIGGER trigger_name
BEFORE OR AFTER UPDATE
ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;

Delete Triggers:
A BEFORE OR AFTER DELETE Trigger means that Oracle will fire this trigger before or after
the DELETE operation is executed

Syntax:
CREATE or REPLACE TRIGGER trigger_name
BEFORE OR AFTER DELETE
ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;

Drop Triggers:
Syntax:
DROP TRIGGER trigger_name;
SAMPLE EXAMPLES:
PART B
(PART B: TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The soft copy
must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at the end of the
practical in case the there is no Black board access available)

Roll. No. 39 Name: Devesh Rajbhar

Class : SE-COMPS Batch: A2


Date of Experiment: 29/04/2021 Date of Submission: 13/05/2021

Grade:

B. 1.Execute the following

Create a table
EMP (eno, ename, hrs, pno) and
PROJECT (pname, pno, thrs)
wherethrs is the total hours and is the derived attribute. Its value is the sum of hrs of all
employees working on that project. eno and pno are primary keys, head_no is foreign key to emp
relation. Insert 4 tuples and write triggers to do the following:

A. Creating a trigger to insert a new employee tuple and display the new total_hours (thrs )
from project table.

B. Creating a trigger to change the project of an employee and display the new
total_hours(thrs) from project table.

C. Creating a trigger to deleting the project of an employee.


B.2 Input and Output:
(Command and its output)

QUERY:
create table PROJECT
(
pno number(5) primary key,
pname varchar(25), thrs
number(5)
);
create table EMP
(
eno number(5) primary key,
ename varchar(25) , hrs
number(5), pno number(5),
constraintfk_ foreign key(pno) references PROJECT(pno)
);
insert into project values(1,'J&J',225);
insert into project values(2,'TESLA',320);
insert into project values(3,'RIPPLE',180);
insert into project values(4,'BTC',400);

insert into EMP values(11,'DOMINIC',25,3);


insert into EMP values(22,'ELON',40,4);
insert into EMP values(33,'BRYAN',30,2);
insert into EMP values(44,'ROMAN',35,3);

A.]
QUERY:
create or replace trigger update_thrs
after insert on EMP for each row
when (new.pno is not null) update
PROJECT setthrs=thrs+:new.hrs
wherepno=:new.pno
/
insert into Emp values(55,'BRAD',30,2);
OUTPUT:
PNO PNAME THRS

1 J&J 225
2 TESLA 350
3 RIPPLE 180
4 BTC 400

B.]
QUERY:
create or replace trigger update_proj after update on EMP for each row update PROJECT
setthrs = thrs -: old.hrs
wherepno =: old.pno;
update PROJECT
setthrs = thrs +: new.hrs
wherepno =: new.pno
/ update EMP set pno=2 where

eno=22; OUTPUT:
Before Trigger:
EMP Table
ENO ENAME HRS PNO

55 BRAD 30 2
11 DOMINIC 25 3
22 ELON 40 4
33 BRYAN 30 2
44 ROMAN 35 3
Project Table
PNO PNAME THRS

1 J&J 225
2 TESLA 350
3 RIPPLE 180
4 BTC 400

After Trigger:
EMP TABLE
ENO ENAME HRS PNO

55 BRAD 30 2
11 DOMINIC 25 3
22 ELON 40 2
33 BRYAN 30 2
44 ROMAN 35 3

Project Table
PNO PNAME THRS

1 J&J 225
2 TESLA 390
3 RIPPLE 180
4 BTC 360

C.]
QUERY:
create or replace trigger del_project
after update of pno on EMP for
each row update PROJECT
setthrs = thrs -: old.hrs
wherepno =: old.pno
/ updateEmployee_trigger set pno=NULL where

eno=22; OUTPUT:

EMP TABLE
ENO ENAME HRS PNO

55 BRAD 30 2
11 DOMINIC 25 3
22 ELON 40 2
33 BRYAN 30 2
44 ROMAN 35 3

Project Table
PNO PNAME THRS

1 J&J 225
2 TESLA 390
3 RIPPLE 180
4 BTC 360

AFTER TRIGGER:
Trigger created.

1 row(s) updated.

EMP TABLE
ENO ENAME HRS PNO

55 BRAD 30 2
11 DOMINIC 25 3
22 ELON 40 -
33 BRYAN 30 2
44 ROMAN 35 3

PROJECT TABLE
PNO PNAME THRS

1 J&J 225
2 TESLA 310
3 RIPPLE 180
4 BTC 360
B.3 Observations and learning:
(Students are expected to comment on the output obtained with clear observations and learning for each task/
sub part assigned)

A trigger is a stored procedure in database which automatically invokes whenever a special event
in the database occurs. For example, a trigger can be invoked when a row is inserted into a
specified table or when certain table columns are being updated.

In this experiment, we first created two tables viz. PROJECT and EMP and inserted 4 tuples in
each of the tables. Then, the first trigger was created to insert a new employee tuple and display
the new total hours from PROJECT table. Then, the next trigger was created to change the
project of an employee and display the new total hours from PROJECT table. The last trigger
was to delete the project of an employee.
B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above and
learning/observation noted in section B.3)

We have thus successfully understood and implemented triggers in this experiment.

You might also like