Trigger for audit logging
📖 Scenario: You are managing a PostgreSQL database for a small company. You want to keep track of changes made to the employees table. This helps you see who changed what and when.
🎯 Goal: Create a trigger that logs every insert, update, and delete on the employees table into an audit_log table.
📋 What You'll Learn
Create an
employees table with columns id, name, and position.Create an
audit_log table with columns change_id, employee_id, operation, and change_time.Write a trigger function called
log_employee_changes that inserts a record into audit_log whenever employees is changed.Create a trigger on
employees that calls log_employee_changes after insert, update, or delete.Test the trigger by inserting a new employee and printing the
audit_log contents.💡 Why This Matters
🌍 Real World
Audit logging is used in companies to track changes in databases for security and compliance.
💼 Career
Database administrators and DevOps engineers often create triggers to automate audit logging and monitor data changes.
Progress0 / 4 steps