0% found this document useful (0 votes)
20 views

Cursor Triggers Sample

This lab exercise aims to demonstrate the use of triggers and cursors in Oracle. Students and employee tables are created and populated with sample data. Various triggers are created, including before and after triggers for insert, update, and delete operations. The triggers log information about the operation performed. Implicit and explicit cursors are also demonstrated, including the use of cursors with loops to fetch and display records.

Uploaded by

mikku ninavi
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)
20 views

Cursor Triggers Sample

This lab exercise aims to demonstrate the use of triggers and cursors in Oracle. Students and employee tables are created and populated with sample data. Various triggers are created, including before and after triggers for insert, update, and delete operations. The triggers log information about the operation performed. Implicit and explicit cursors are also demonstrated, including the use of cursors with loops to fetch and display records.

Uploaded by

mikku ninavi
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/ 13

PRINCIPLES OF DATABASE SYSTEMS

LAB EXERCISE 6
Aim: To execute Triggers and Cursors.
Triggers:
General syntax:
CREATE [ OR REPLACE ] TRIGGER trigger_name
BEFORE INSERT
ON table_name
[ FOR EACH ROW ]
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
Creating tables for students:
Command:
create table students(s_id number(2), sname varchar2(10), sub_id number(3), marks
number(2));
Result: tables are created for students.

Inserting values into the tables:


Command:
insert into students values(2, 'Anjali', 002, 85 );
insert into students values(4, 'Rahul', 003, 77);
insert into students values(6, 'Thanusree', 001, 90);
insert into students values(8, 'praveen', 005, 60);
insert into students values(10, 'Deepika', 004, 94);
Result: the values are entered into the table.
Command: select * from students;
Result: displays selected table with data.

Creating a trigger:
Before trigger- insert, delete, update
set serveroutput on;
create or replace trigger mark_changes
before delete or insert or update on students
for each row
when (new.s_id>0)
declare
diff_inmark number;
begin
diff_inmark := :new.marks -:old.marks;
dbms_output.put_line('old marks:' || :old.marks);
dbms_output.put_line('new marks:' || :new.marks);
dbms_output.put_line('mark change:' || diff_inmark);
end;
/
Result: trigger is created.

Command: insert into students values(12, 'Kruthi', 001, 78);


Result: value is inserted into the table.
Command: update students set marks = marks + 10 where s_id = 8;
Result: mark is updated for the particular student.

Command: delete from students where sname = 'Anjali';


Result: The value is deleted from the table.

After trigger- insert, delete, update


After insert
Command:
set serveroutput on;
create trigger AI after insert on students for each row
declare
username varchar2(20);
begin
select user into username from dual;
dbms_output.put_line('after insert:' || username);
end;
/
Result: trigger is created.
Command: insert into students values(13, 'Ashwathi', 004, 97);
Result: the values are inserted into the table.

After update
Command:
set serveroutput on;
create trigger AU after update on students for each row
declare
username varchar2(20);
begin
select user into username from dual;
dbms_output.put_line('after update:' || username);
end;
/
Result: trigger is created.
Command: update students set sub_id= 002 where s_id = 6;
Result: table is updated.

After delete
Command:
set serveroutput on;
create trigger AD after update on students for each row
declare
username varchar2(20);
begin
select user into username from dual;
dbms_output.put_line('after update:' || username);
end;
/
Result: trigger is created.
Command: delete from students where sname = 'Thanusree';
Result: value is deleted from the table.

Disabling trigger:
Command: alter trigger mark_changes disable;
Result: trigger is disabled.

Enabling trigger:
Command: alter trigger mark_changes enable;
Result: trigger is enabled.

Drop trigger:
Command: drop trigger mark_changes;
Result: trigger is dropped.
Cursors:
Creating table for employees:
Command: create table emp(eid number(3), ename varchar2(20), salary number(5), location
varchar2(20));
Result: table for employees is created.

Inserting values:
Command:
insert into emp values(002, 'Sanjana', 50000, 'Mumbai');
insert into emp values(004, 'Madhav', 60000, 'Pune');
insert into emp values(006, 'john', 70000, 'Chennai');
insert into emp values(008, 'Nancy', 30000, 'Vellore');
insert into emp values(010, 'Gwen', 40000, 'Coimbatore');
Result: values are inserted into the table.
Command: select * from emp;
Result: displays selected table with data.

Implicit cursors:
Command:
set serveroutput on;
declare
total_rows number(2);
begin
update emp
set salary = salary +700;
if sql%notfound then
dbms_output.put_line('none selected');
end if;
if sql%found then
total_rows := sql%rowcount;
dbms_output.put_line(total_rows || 'employees selected');
end if;
end;
Result: 5 employees get selected.
Command: select * from emp;
Result: displays the table after execution.

Explicit cursors
Command:
declare
e_id emp.eid%type;
e_name emp.ename%type;
e_loc emp.location%type;
cursor e_emp is
select eid, ename, location from emp;
begin
open e_emp;
loop
fetch e_emp into e_id, e_name, e_loc;
exit when e_emp%notfound;
dbms_output.put_line(e_id||''||e_name||''||e_loc);
end loop;
close e_emp;
end;
/
Result: displays employee id, employee name and location.

For loops in cursors:


Command:
set serveroutput on;
DECLARE
CURSOR e_emp
IS
SELECT ename,salary
FROM emp
ORDER BY salary DESC;
BEGIN
FOR r_emp IN e_emp
LOOP
dbms_output.put_line(r_emp.ename ||':$'||r_emp.salary);
END LOOP;
END;
/
Result: displays the employee names with their salary.
For loop with select statement
Using where clause:
Command:
set serveroutput on;
declare
cursor e_emp
is
select ename,salary
from emp
where salary = '50700';
begin
for r_emp in e_emp
loop
dbms_output.put_line(r_emp.ename ||':$'||r_emp.salary);
END LOOP;
END;
/
Result: displays Sanjana:$50700

You might also like