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

DBMS Lab

The document provides instructions for creating a row-level trigger on the CUSTOMERS table to display salary differences during INSERT, UPDATE, or DELETE operations. It also includes a procedure for declaring and using a cursor to extract employee data from the EMPLOYEE table, demonstrating how to handle exceptions and ensure proper cursor closure. Both sections emphasize the use of DBMS_OUTPUT for displaying relevant information during database operations.

Uploaded by

Dhrithi J
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)
2 views

DBMS Lab

The document provides instructions for creating a row-level trigger on the CUSTOMERS table to display salary differences during INSERT, UPDATE, or DELETE operations. It also includes a procedure for declaring and using a cursor to extract employee data from the EMPLOYEE table, demonstrating how to handle exceptions and ensure proper cursor closure. Both sections emphasize the use of DBMS_OUTPUT for displaying relevant information during database operations.

Uploaded by

Dhrithi J
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/ 5

BCS403 DBMS LAB MANUAL

4. CREATE A ROW LEVEL TRIGGER FOR THE CUSTOMERS TABLE THAT


WOULD FIRE FOR INSERT OR UPDATE OR DELETE OPERATIONS
PERFORMED ON THE CUSTOMERS TABLE. THIS TRIGGER WILL DISPLAY
THE SALARY DIFFERENCE BETWEEN THE OLD & NEW SALARY.
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
CREATE TABLE CUSTOMER (
CID NUMBER (5),
CNAME VARCHAR (20),
AGE NUMBER (3),
ADDRESS VARCHAR (20),
SALARY . NUMBER (5),
CONSTRAINT PK_CUST PRIMARY KEY(CID));

DESCRIPTION of the TRIGGER:


 The BEFORE INSERT OR UPDATE OR DELETE clause specifies that the trigger
should fire before any insert, update, or delete operation on the CUSTOMERS table.
 FOR EACH ROW indicates that the trigger is a row-level trigger, which means it will
execute once for each affected row.
 Inside the trigger, we declare two variables v_old_salary and v_new_salary to hold the
old and new values of the SALARY column.
 Depending on the operation (INSERTING, UPDATING, or DELETING), different
actions are taken:
 For INSERTING: Simply display the new salary (:NEW.SALARY).
 For UPDATING: Display both the old and new salaries (:OLD.SALARY and
:NEW.SALARY), and calculate and display the salary difference if both values are not
null.
 For DELETING: Display the old salary (:OLD.SALARY).
 DBMS_OUTPUT.PUT_LINE is used to print messages to the console, showing the
relevant salary information.
 Remember to enable DBMS_OUTPUT for your session to see the output of
DBMS_OUTPUT.PUT_LINE. You can enable it with the following command before
running your DML statements:

Department of CSE, BIT 2023-24 6|P a g e


BCS403 DBMS LAB MANUAL

-- Create a trigger named salary_difference_trigger


CREATE OR REPLACE TRIGGER salary_difference_trigger
BEFORE INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
DECLARE
v_old_salary CUSTOMERS.SALARY%TYPE;
v_new_salary CUSTOMERS.SALARY%TYPE;
BEGIN
IF INSERTING THEN
-- For INSERT operation, display new salary
DBMS_OUTPUT.PUT_LINE('New Salary: ' || :NEW.SALARY);

ELSIF UPDATING THEN


-- For UPDATE operation, display old and new salaries
v_old_salary := :OLD.SALARY;
v_new_salary := :NEW.SALARY;
DBMS_OUTPUT.PUT_LINE('Old Salary: ' || v_old_salary);
DBMS_OUTPUT.PUT_LINE('New Salary: ' || v_new_salary);

-- Display salary difference


IF v_old_salary IS NOT NULL AND v_new_salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Salary Difference: ' || (v_new_salary - v_old_salary));
END IF;

ELSIF DELETING THEN


-- For DELETE operation, display old salary
DBMS_OUTPUT.PUT_LINE('Old Salary: ' || :OLD.SALARY);
END IF;
END;
/
SET SERVEROUTPUT ON
Department of CSE, BIT 2023-24 7|P a g e
BCS403 DBMS LAB MANUAL

5. CREATE CURSOR FOR EMPLOYEE TABLE & EXTRACT THE VALUES FROM
THE TABLE. DECLARE THE VARIABLES, OPEN THE CURSOR & EXTRCT THE
VALUES FROM THE CURSOR. CLOSE THE CURSOR.
EMPLOYEE (E_ID, E_NAME, AGE, SALARY)

DESCRIPTION:

 We declare variables (v_e_id, v_e_name, v_age, v_salary) to hold the data


for each employee fetched from the EMPLOYEE table.
 We define a cursor emp_cursor that selects E_ID, E_NAME, AGE, and
SALARY from the EMPLOYEE table.
 The OPEN emp_cursor; statement opens the cursor to start fetching rows
from the EMPLOYEE table.
 The LOOP fetches rows one by one from the cursor into the declared
variables (v_e_id, v_e_name, v_age, v_salary).
 Inside the loop, we print out each employee's information using
DBMS_OUTPUT.PUT_LINE.
 The loop continues until all rows are fetched (emp_cursor%NOTFOUND).
 After processing all rows, the cursor is closed using CLOSE emp_cursor;.
 Exception handling (WHEN OTHERS) is included to catch any errors that
might occur during cursor operations. If an error occurs, it displays an error
message and ensures the cursor is closed properly.

Department of CSE, BIT 2023-24 8|P a g e


BCS403 DBMS LAB MANUAL

DECLARE
-- Declare variables to hold employee data
v_e_id EMPLOYEE.E_ID%TYPE;
v_e_name EMPLOYEE.E_NAME%TYPE;
v_age EMPLOYEE.AGE%TYPE;
v_salary EMPLOYEE.SALARY%TYPE;

-- Declare cursor for the employee table


CURSOR emp_cursor IS
SELECT E_ID, E_NAME, AGE, SALARY
FROM EMPLOYEE;
BEGIN
-- Open the cursor
OPEN emp_cursor;

-- Loop through the cursor and fetch values into variables


LOOP
FETCH emp_cursor INTO v_e_id, v_e_name, v_age, v_salary;

-- Exit the loop if no more rows to fetch


EXIT WHEN emp_cursor%NOTFOUND;

-- Process the fetched data (for example, display or use the values)
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_e_id);
DBMS_OUTPUT.PUT_LINE('Name: ' || v_e_name);
DBMS_OUTPUT.PUT_LINE('Age: ' || v_age);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('-------------------------------- ');
END LOOP;

Department of CSE, BIT 2023-24 9|P a g e


BCS403 DBMS LAB MANUAL

-- Close the cursor


CLOSE emp_cursor;
EXCEPTION
WHEN OTHERS THEN
-- Handle exceptions (if any)
DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);
IF emp_cursor%ISOPEN THEN
CLOSE emp_cursor;
END IF;
END;

Department of CSE, BIT 2023-24 10 | P a g e

You might also like