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

DBMS Lab-9

This document discusses triggers and cursors in Oracle SQL. It contains examples of creating triggers on tables to log changes, and using cursors to iterate through records and output data.

Uploaded by

goravedu007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

DBMS Lab-9

This document discusses triggers and cursors in Oracle SQL. It contains examples of creating triggers on tables to log changes, and using cursors to iterate through records and output data.

Uploaded by

goravedu007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DBMS LAB-9

Triggers & Cursors


-22BAI1037
Gorav Jhabakh
1.
create table salary_1037(id int,name varchar2(30),age int,address
varchar2(100),salary int);

insert into salary_1037 values (101,'Prem',20,'ABC street',50000);


insert into salary_1037 values (102,'Siddharth',18,'DEF street',60000);
insert into salary_1037 values (103,'Aditya',19,'XYZ street',70000);

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON salary_1037
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
2.
create table student_1037 (regno int primary key,name varchar2(30),age int,dept
varchar2(10),semester int);

insert into student_1037 values(101,'Prem',20,'SCOPE',3);


insert into student_1037 values(102,'Siddahrth',18, 'SCOPE',4);
insert into student_1037 values(103,'Aditya',19, 'SENSE',3);

CREATE OR REPLACE TRIGGER display_semester_changes


BEFORE UPDATE OF semester ON student_1037
FOR EACH ROW
DECLARE
old_semester NUMBER;
new_semester NUMBER;
BEGIN
old_semester := :OLD.semester;
new_semester := :NEW.semester;

dbms_output.put_line('Old semester: ' || old_semester);


dbms_output.put_line('New semester: ' || new_semester);
END;
/

3.
DECLARE
cursor high_sal is select * from salary_1037 where salary>65000;
cnt int;
BEGIN
for high_sal_rec in high_sal
loop
cnt:=high_sal%rowcount;
end loop;
dbms_output.put_line('High salary counts: '||cnt);
END;
/

4.

DECLARE
cursor c_customers is select id,name,address from customers_1037;
BEGIN
FOR c_customers_rec in c_customers
LOOP
dbms_output.put_line('id: '||c_customers_rec.id);
dbms_output.put_line('name: '||c_customers_rec.name);
dbms_output.put_line('address: '||c_customers_rec.address);
END LOOP;
END;
/

5.
insert into customers_1037 values (104,'Mukunth',60,'MNO street',80000);
insert into customers_1037 values (105,'Avikshith',55, 'PQR street',40000);

DECLARE
cursor c_customers is select * from customers_1037 where age>50;
BEGIN
FOR c_customers_rec in c_customers
LOOP
dbms_output.put_line('id: '||c_customers_rec.id);
dbms_output.put_line('name: '||c_customers_rec.name);
dbms_output.put_line('age: '||c_customers_rec.age);
dbms_output.put_line('address: '||c_customers_rec.address);
dbms_output.put_line('salary: '||c_customers_rec.salary);
END LOOP;
END;
/

6.
in this case lets assume minors as people less than 20 yrs old

DECLARE
cursor c_customers is select * from customers_1037 where age<20;
BEGIN
FOR c_customers_rec in c_customers
LOOP
dbms_output.put_line('id: '||c_customers_rec.id);
dbms_output.put_line('name: '||c_customers_rec.name);
dbms_output.put_line('age: '||c_customers_rec.age);
dbms_output.put_line('address: '||c_customers_rec.address);
dbms_output.put_line('salary: '||c_customers_rec.salary);
END LOOP;
END;
/

You might also like