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

DBMS 186 Ex9

The document outlines a PL/SQL experiment involving the creation of an explicit cursor to update employee salaries based on specific conditions and a trigger to prevent the insertion of employees younger than 25. It includes SQL commands for creating an Employees table, inserting test data, and implementing the cursor and trigger logic. The document also demonstrates test insertions to validate the trigger's functionality.

Uploaded by

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

DBMS 186 Ex9

The document outlines a PL/SQL experiment involving the creation of an explicit cursor to update employee salaries based on specific conditions and a trigger to prevent the insertion of employees younger than 25. It includes SQL commands for creating an Employees table, inserting test data, and implementing the cursor and trigger logic. The document also demonstrates test insertions to validate the trigger's functionality.

Uploaded by

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

NAME-VAIBHAV

ROLL NO-2200290110186
SEC-5C
EXPERIMENT-9
A) Create a explicit cursor which updates the salary of an employee
such that,
1. If salary > 10000, then increase the salary by 15%
2. If 5000<salary <10000, then increase the salary by 12%
3. Otherwise, increase the salary by 10%
B) Write a trigger to ensure that no employee of age less than 25
can be inserted in the database.
-- Creating the Employees table for testing
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
salary DECIMAL(10, 2)
);

-- Inserting test data


INSERT INTO Employees VALUES (1, 12000), (2, 8000), (3, 4500);

-- PL/SQL Block
DECLARE
CURSOR emp_cursor IS
SELECT emp_id, salary FROM Employees;
v_emp_id Employees.emp_id%TYPE;
v_salary Employees.salary%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_emp_id, v_salary;
EXIT WHEN emp_cursor%NOTFOUND;

IF v_salary > 10000 THEN


UPDATE Employees SET salary = salary * 1.15 WHERE emp_id =
v_emp_id;
ELSIF v_salary > 5000 AND v_salary <= 10000 THEN
UPDATE Employees SET salary = salary * 1.12 WHERE emp_id =
v_emp_id;
ELSE
UPDATE Employees SET salary = salary * 1.10 WHERE emp_id =
v_emp_id;
END IF;
END LOOP;
CLOSE emp_cursor;
END;
/

-- Viewing updated salaries


SELECT * FROM Employees;
-- Creating the Employees table with an age column
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
age INT,
salary DECIMAL(10, 2)
);

-- Creating the trigger to prevent insertion of employees younger than 25


CREATE OR REPLACE TRIGGER check_age
BEFORE INSERT ON Employees
FOR EACH ROW
BEGIN
IF :NEW.age < 25 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee age must be 25 or
above.');
END IF;
END;
/

-- Test Insertion (should fail)


INSERT INTO Employees VALUES (1, 24, 12000);

-- Test Insertion (should succeed)


INSERT INTO Employees VALUES (2, 30, 8000);

-- Viewing data
SELECT * FROM Employees;
FINAL TABLE

You might also like