Professional Documents
Culture Documents
Triggers :
Write a database trigger to perform : whenever a row in updated or inserted with entry (item no, item
name, rate, qty). The total is calculated and entered into the total field of the record.
Create or Replace Trigger Trans After Insert on Transaction For Each Row
Declare
mtotal number(5);
mqty number(5);
mrate number(5);
Begin
mtotal := mrate*mqty;
end;
Ans: Create table Itemmst(Item_no number(5) primary key ,Name varchar2(15),Rate number(4));
Create or replace Trigger Transmst After Update or Insert on Tran referencing OLD AS old For Each Row
Declare
mtotal number(5);
mqty number(5);
mrate number(5);
mitemno number(5);
Begin
mtotal:= mrate*mqty;
(3) Write a program to access the name of employee for a particular given empno. If there is no
employee for given empno, generate no_data found exception and insert the appropriate message and
empno into the temp file.
Ans:
desc temp1;
Declare
mempno varchar2(20);
mename varchar2(15);
Begin
mempno:=:mempno;
dbms_output.put_line(mempno || mename);
Exception
End;
Declare
Divide_By_Zero Exception;
mcount number(5);
mX number(4);
mY number(4);
mSamp_id number(6);
mRatio number(6,2);
Begin
For i IN 1..mcount
Loop
If mY=0 then
Raise Divide_By_Zero ;
End IF;
mRatio:=mX/mY;
if mRatio>0.72 then
dbms_output.put_line(mRatio);
End if;
End Loop;
Exception
End;
(5) Write a program that modifies the Accounts table based on instructions stored in the ACTION table.
Each row of the ACTION table contains an account number to act upon, an action to be taken (insert,
update or delete) and an amount to update the account to if the action is not a delete. On an insert, if
the account already exist, an update is performed instead. If the account does not exist then the
account is created via an insert. On delete, if the row does not exist, no alternative action is taken.
Create table Action(Account_id number(6) primary key, oper varchar2(10), new_value varchar(15));
Create or replace trigger Acc After Insert or Update or Delete on Accounts referencing OLD AS old For
Each Row
Declare
moper varchar2(10);
maccountid number(6);
mvalue number(6);
mbalance number(7);
Begin
maccountid:=:OLD.Account_id;
If moper='Insert' then
End if;
End;