- 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.
CREATE TABLE employee(empid NUMBER, empname VARCHAR(10), dept VARCHAR(10),salary NUMBER);
CREATE TABLE salary_log(log_id NUMBER , empid NUMBER,empname VARCHAR(10),
old_salary NUMBER,new_salary NUMBER,update_date DATE);
CREATE OR REPLACE TRIGGER log_salary_update
BEFORE UPDATE ON employee
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 salary_log (empid, empname, old_salary, new_salary, update_date)
VALUES (:old.empid, :old.empname, v_old_salary, v_new_salary, SYSDATE);
END IF;
END;
/
Thus the trigger using pl/sql has been created sucessfully.