0% found this document useful (0 votes)
39 views4 pages

Ex 7

The document contains 4 examples demonstrating the use of PL/SQL packages and triggers. The first example creates a package with procedures to perform CRUD operations on a student table. The second implements function overloading using a package. The third creates a trigger to insert rows from a salary table to a backup table when the salary table is updated. The fourth creates a trigger to prevent DML operations on an employee table on weekends.

Uploaded by

Barath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Ex 7

The document contains 4 examples demonstrating the use of PL/SQL packages and triggers. The first example creates a package with procedures to perform CRUD operations on a student table. The second implements function overloading using a package. The third creates a trigger to insert rows from a salary table to a backup table when the salary table is updated. The fourth creates a trigger to prevent DML operations on an employee table on weekends.

Uploaded by

Barath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX.

1.Write a PL/SQL code to create Package with procedures to perform insert,


retrieve, update and
delete operations on student table.

create table student_table(regno number primary key, sname varchar(15), age


number,dept varchar(4));

insert into student_table values(7160, 'Sam', 19, 'DS');


insert into student_table values(7170, 'Ram', 18, 'AIML');
insert into student_table values(7180, 'Tom', 20, 'DCS');
insert into student_table values(7190, 'Som', 19, 'SS');

CREATE OR REPLACE PACKAGE student_op_pack AS


PROCEDURE insert_student(reg_no IN NUMBER, s_name IN VARCHAR2, new_age IN
NUMBER, new_dept IN VARCHAR2);

PROCEDURE retrieve_students;

PROCEDURE update_student(reg_no IN NUMBER, new_dept IN VARCHAR2);

PROCEDURE delete_student(reg_no IN NUMBER);


END student_op_pack;
/

CREATE OR REPLACE PACKAGE BODY student_op_pack AS

PROCEDURE insert_student(reg_no IN NUMBER, s_name IN VARCHAR2, new_age IN


NUMBER, new_dept IN VARCHAR2) IS
BEGIN
INSERT INTO student_table(regno, sname, age, dept) VALUES (reg_no, s_name,
new_age, new_dept);
END insert_student;

PROCEDURE retrieve_students IS
BEGIN
FOR rec IN (SELECT * FROM student_table) LOOP
DBMS_OUTPUT.PUT_LINE('Regno:' || rec.regno || ', Name:' || rec.sname ||
', Age:' || rec.age || ', Department:' || rec.dept);
END LOOP;
END retrieve_students;

PROCEDURE update_student(reg_no IN NUMBER, new_dept IN VARCHAR2) IS


BEGIN
UPDATE student_table
SET dept = new_dept
WHERE regno = reg_no;
END update_student;

PROCEDURE delete_student(reg_no IN NUMBER) IS


BEGIN
DELETE FROM student_table WHERE regno = reg_no;
END delete_student;
END student_op_pack;
/

set serverout on

exec student_op_pack.insert_student(7120, 'Jack', 21, 'DS');

exec student_op_pack.retrieve_students;

exec student_op_pack.update_student(7170,'AIML');

exec student_op_pack.retrieve_students;

exec student_op_pack.delete_student(7120);

exec student_op_pack.retrieve_students;

2.Write a PL/SQL code to implement function overloading using Package.

CREATE OR REPLACE PACKAGE OverloadPackage AS

FUNCTION add_numbers(x IN NUMBER) RETURN NUMBER;

FUNCTION add_numbers(x IN NUMBER, y IN NUMBER) RETURN NUMBER;

FUNCTION add_numbers(x IN NUMBER, y IN NUMBER, z IN NUMBER) RETURN NUMBER;


END OverloadPackage;
/

CREATE OR REPLACE PACKAGE BODY OverloadPackage AS

FUNCTION add_numbers(x IN NUMBER) RETURN NUMBER IS


BEGIN
RETURN x;
END add_numbers;

FUNCTION add_numbers(x IN NUMBER, y IN NUMBER) RETURN NUMBER IS


BEGIN
RETURN x + y;
END add_numbers;

FUNCTION add_numbers(x IN NUMBER, y IN NUMBER, z IN NUMBER) RETURN NUMBER IS


BEGIN
RETURN x + y + z;
END add_numbers;
END OverloadPackage;
/

DECLARE
result1 NUMBER;
result2 NUMBER;
result3 NUMBER;
BEGIN
result1 := OverloadPackage.add_numbers(9);
DBMS_OUTPUT.PUT_LINE('Result with one parameter: ' || result1);

result2 := OverloadPackage.add_numbers(1, 2);


DBMS_OUTPUT.PUT_LINE('Result with two parameters: ' || result2);

result3 := OverloadPackage.add_numbers(2, 1, 4);


DBMS_OUTPUT.PUT_LINE('Result with three parameters: ' || result3);
END;
/

3. Write a row trigger to insert the existing values of the salary table in to a
new table when the
salary table is updated.

CREATE TABLE salary (


employee_id NUMBER PRIMARY KEY,
salary NUMBER
);

INSERT INTO salary (employee_id, salary) VALUES (1, 50000);


INSERT INTO salary (employee_id, salary) VALUES (2, 60000);
INSERT INTO salary (employee_id, salary) VALUES (3, 70000);

CREATE TABLE salary_backup (


employee_id NUMBER,
old_salary NUMBER,
new_salary NUMBER,
update_date DATE
);

CREATE OR REPLACE TRIGGER salary_update_trigger


BEFORE UPDATE
ON salary
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO salary_backup (employee_id, old_salary, new_salary, update_date)
VALUES (:OLD.employee_id, :OLD.salary, :NEW.salary, SYSDATE);
END;
/

UPDATE salary SET salary = salary * 1.1 WHERE employee_id = 1;

SELECT * FROM salary;

SELECT * FROM salary_backup;

4. Create a Trigger that does not allow DML operations on the employee table during
weekends.
create table empl(employee_id number(2) primary key, salary number(8));

insert into empl values(1,53420);


insert into empl values(2,52320);
insert into empl values(3,57720);
insert into empl values(4,74420);

CREATE OR REPLACE TRIGGER prevent_weekend_operations


BEFORE INSERT OR UPDATE OR DELETE
ON empl
BEGIN
IF TO_CHAR(SYSDATE, 'DY') IN ('SAT', 'SUN') THEN
RAISE_APPLICATION_ERROR(-20001, 'DML operations are not allowed on
weekends.');
END IF;
END;
/

UPDATE empl SET salary = 55000 WHERE employee_id = 1;

INSERT INTO empl (employee_id, salary) VALUES (5, 72000);

DELETE FROM empl WHERE employee_id = 4;

You might also like