Create an AFTER Trigger to Log Employee Salary Changes
📖 Scenario: You work in the HR department of a company. The company wants to keep track of any changes made to employee salaries. Whenever a salary is updated, a record should be added to a log table showing the employee's ID, old salary, new salary, and the date of the change.
🎯 Goal: Create an AFTER UPDATE trigger on the employees table that inserts a record into the salary_changes table every time an employee's salary is updated.
📋 What You'll Learn
Create a table called
employees with columns employee_id (integer), name (text), and salary (integer).Create a table called
salary_changes with columns change_id (auto-increment integer primary key), employee_id (integer), old_salary (integer), new_salary (integer), and change_date (date).Create an
AFTER UPDATE trigger on the employees table.The trigger should insert a new row into
salary_changes with the employee's ID, old salary, new salary, and the current date whenever the salary changes.💡 Why This Matters
🌍 Real World
Companies often need to track changes to important data like employee salaries for auditing and compliance.
💼 Career
Knowing how to create triggers and log changes is useful for database administrators and backend developers to maintain data integrity and audit trails.
Progress0 / 4 steps