To create a Trigger using PL/SQL.
- Create employee table with following attributes (empid NUMBER, empname VARCHAR(10), dept VARCHAR(10),salary NUMBER);
- Create salary_log table with following attributes (log_id NUMBER GENERATED ALWAYS AS IDENTITY, empid NUMBER,empname VARCHAR(10),old_salary NUMBER,new_salary NUMBER,update_date DATE);
- Create a trigger named as log_salary-update.
- Inside the trigger block, Insert the values into the salary_log table whenever the salary is updated.
- End the trigger.
- Update the salary of an employee in employee table.
- Whenever a salary is updated for the employee it must be logged into the salary_log table with old salary and new salary.
- Display the employee table, salary_log table.
DEVOLEPED BY : SUBRAMANIYA PILLAI B
REG NO : 212221230109
create table empe( empid number,empname varchar(10),dept varchar(10),salary number);
create table slog(log_id number GENERATED ALWAYS AS IDENTITY,empid NUMBER,empname varchar(20),old_salary number,new_salary number,update_date date);
CREATE OR REPLACE TRIGGER slog_update
BEFORE UPDATE ON empe
FOR EACH ROW
DECLARE
v_old_salary NUMBER;
v_new_salary NUMBER;
BEGIN
v_old_salary := :OLD.salary;
v_new_salary := :NEW.salary;
IF v_old_salary <> v_new_salary THEN
INSERT INTO slog (empid,empname,old_salary,new_salary,update_date)
VALUES (:OLD.empid,:OLD.empname,v_old_salary,v_new_salary, SYSDATE);
END IF;
END;
/
update empe set salary =65000 where empid=2;
select *from slog;
Thus a trigger is created using PL/SQL.