You are on page 1of 4

Ex. No.

: 6

Date :

TRIGGER
AIM:

To write a PL/SQL block for transactions of a typical applications using.

ALGORITHM:

Step 1: Create a table and insert records using sql query.

Step 2: Create a trigger and trigger name and when the trigger is execute is given.

Step 3: Declare a cursor and fetch the cursor student table record into the cursor variable.

Step 4: Create a variable and data types to store the updated values.

Step 5: If condition use the check the mark is pass or fail.

Step 6: Update the table using update query,we use the loop here so all record are updated.

Step 7: End the trigger and execute it display the out.

QUERY:

SQL>create table student(Rno varchar2(10),Name varchar2(25),Subl number(2),sub2

number(2),sub3 number(2), sub4 number(2), sub5 number(2),Total number(5),Result

varchar2(10),constraint pk_student primary key(Rno));

Table Created

SQL>DESC STUDENT

Name Null? Type

RNO NOT NULL VARCHAR2(10)

NAME VARCHAR2(25)

SUB1 NUMBER(2)

SUB2 NUMBER(2)

SUB3 NUMBER(2)

SUB4 NUMBER(2)

SUB5 NUMBER(2)

TOTAL. NUMBER(3)

RESULT. VARCHAR2(10)
SQL>Create or replace trigger student

Befare insert on student

for each row

declare

cursor c is select * from student;

x student%rowtype;

tot student.total%type;

res student.result%type;

begin

for x in c

loop

tot:=x.sub1+ x.sub2+ x.sub3+ x.sub4+ x.sub5;

if(x.sub1>=50)and(x.sub2>=50)and(x.sub3>=50)and(x.sub4>=50)and(x.sub5>=50)then

res:=’PASS’;

else

res:=’FAIL’;

end if;

update student set total=tot where rno=x.rno;

update student set result=res where rno=x.rno;

dbms_output.put_line(‘Roll No:’||x.rno);

dbms_output.put_line(‘Name:’||x.name);

dbms_output.put_line(‘Total:’||tot);

dbms_output.put_line(‘Result’||res);

end loop;

dbms_output.put_line(‘RECORD UPDATED’);

Exception
When no_data_found then

Dbms_output.put_line(‘No Data Found’);

End studriger;

Trigger created.

SQL>set serveroutput on

SQL>insert into student(rno,name,sub1,sub2,sub3,sub4,sub5)

Values(‘&rno’,’ &name’,&sub1,&sub2,&sub3,&sub4,&sub5)

Enter value for rno:MCA003

Enter value for name:HUMAYUN

Enter value for Sub1:80

Enter value for Sub2:70

Enter value for Sub3:80

Enter value for Sub4:90

Enter value for Sub5:60

Old 2:values(‘&rno’,’ &name’,&sub1,&sub2,&sub3,&sub4,&sub5)

New 2:values(‘MCA003’,’MOHAN’,80,70,80,90,60)

Roll No:MCA001

Name:HARI

Total:410

Result:FAIL

Roll No:MCA002

Name:MAHESH

Total:420

Result:FAIL

RECORD UPDATED

1 row created.
SQL>SELECT * FROM STUDENT;

RNO NAME. SUB1 SUB2 SUB3 SUB4 SUB5 TOTAL RESULT

MCA001 Hari. 80 80 90 90 70 410 Pass

MCA002 Mahesh 70 80. 90. 90 90. 420 Pass

Result:-

Thus the Trigger program executed success fully.

You might also like