1

I have to call a trigger which updates fields of total and percentage after query is inserted heres the code

create table bhrugus1 (
  student_name varchar2(100),
  dbms number,
  dsa number,
  mco number,
  total number,
  percentage number,
  roll_no number primary key

);


CREATE OR REPLACE TRIGGER trial1 
AFTER INSERT on bhrugus1
REFERENCING new AS new
FOR EACH ROW 
BEGIN
      :new.total := :new.dbms + :new.dsa + :new.mco; 
      :new.percentage := (:new.dbms + :new.dsa + :new.mco) / 3 ;
END;


insert into bhrugus1 values ('bhrugu',90,90,90,1,1,2);

SELECT * from bhrugus1;

the table and while running trigger i get error


the new error

2 Answers2

0

Please modify your trigger:

CREATE OR REPLACE TRIGGER trial1 
before INSERT on bhrugus1
REFERENCING new AS new
FOR EACH ROW 
BEGIN
      :new.total := :new.dbms + :new.dsa + :new.mco; 
      :new.percentage := (:new.dbms + :new.dsa + :new.mco) / 3 ;
END;

You shouldn't run an update statement in your trigger, use the :new... instead of update.

The other important thing is:

before insert
Kapitany
  • 1,319
  • 1
  • 6
  • 10
0

You are missing a component, what happens when someone UPDATEs any or all of dbms,dsa,mco. There is no update trigger to recalculate total or percentage. You could of course just include 'OR UPDATE' on the trigger. A better way however would be defining them a virtual columns.

create table bhrugus1 (
  student_name varchar2(100),
  dbms number,
  dsa number,
  mco number,
  total number generated always as (dbms+dsa+mco) virtual,
  percentage number generated always as ((dbms+dsa+mco)/3) virtual,
  roll_no number primary key
); 

With that both total and percentage (bad name it is the average - but that is another issue) will always calculated properly. And drop the trigger it is no longer needed.

Belayer
  • 13,578
  • 2
  • 11
  • 22