Implementing salary change auditing trigger in PL/SQL
PL/SQL Trigger: Exercise-7 with Solution
Write a code in PL/SQL to design a trigger that captures changes made to specific columns and logs them in an audit table.
Sample Solution:
PL/SQL Code:
Let’s see how the trigger functions:
PL/SQL Code:
After executing the UPDATE statement, to get the captured audit information you can query the salary_audit.
PL/SQL Code:
Output:
AUDIT_ID EMPLOYEE_ID OLD_SALARY NEW_SALARY CHANGE_DATE 1 1 50000 60000 17-AUG-23 10.06.21.631889 AM
Flowchart:


Explanation:
The provided PL/SQL code implementing this trigger, for any update that modifies an employee's salary will be logged in the salary_audit table, providing a comprehensive audit trail for monitoring salary changes.
Assume that the two tables employees and salary_audit are involved with this trigger.
A sequence seq_salary_audit is created to generate unique audit IDs for entries in the salary_audit table. This sequence ensures that each change recorded in the audit log has a distinct identifier.
The trigger salary_change_audit is defined to capture changes in the salary column which fires automatically after an UPDATE operation on the employees table when the salary value is altered.
Inside the trigger body, the unique audit_id is generated using the seq_salary_audit sequence. Then, an INSERT statement is used to record the details of the salary change into the salary_audit table.
Previous: Restricting total order amount using PL/SQL Triggers.
Next: Methods in PL/SQL for a "Person" class with display and age update.
What is the difficulty level of this exercise?