Home > OS >  Pseudorecord best practice when trigger fires but value should never change
Pseudorecord best practice when trigger fires but value should never change

Time:11-17

Consider the following tables and trigger, is there a need to specify :new.employee_id in the insert statement or is this generally considered best practice. The trigger will only fire on the salary column of the table and the employee_id should not be affected. Is the :new.employee_id syntax just good practice when creating triggers or unnecessary? Could there be a potential issue if this is not added?

Link to Oracle HR SCHEMA employees table Employees Table HR Schema

CREATE TABLE salary_log (
    whodidit VARCHAR2(25), whendidit timestamp,
    oldsalary NUMBER,
    newsalary NUMBER,
    emp_affected NUMBER);
    
    CREATE OR REPLACE TRIGGER saltrig
        AFTER INSERT OR UPDATE OF salary ON employees
        FOR EACH ROW 
        BEGIN
        INSERT INTO salary_log
        VALUES(user,sysdate, :old.salary, :new.salary, :new.employee_id);
        END;

CodePudding user response:

If you were to NOT include it:

  1. You would have to alter your trigger code to explicitly name the columns you are supplying values for, and
  2. Your log table would show that someone's salary changed, who did it and what the old and new values are, but you would not know whose salary was changed. The data in that column would be null.

Is the :new.employee_id syntax just good practice when creating triggers or unnecessary?

If you want this value in your log table, then it is required.

  • Related