Posts

Showing posts with the label auditing

Using Trigger

Image
Introduction A trigger is a special structure that automatically executes a function (called a Trigger Function) when a specific event occurs on a table or a view, meaning that when someone performs actions such as INSERT, UPDATE or DELETE, the Trigger will immediately be activated to perform a pre-programmed task. Use cases Triggers are extremely useful when you want to automate data management tasks without writing additional code on the application side, these are the most common uses: Data Validation Although Postgres has built-in constraints like CHECK, NOT NULL or FOREIGN KEY, sometimes you need more complex rules. Example: Disallowing updates to the date_of_birth column if the calculated age is less than 18, or not allowing employees to schedule appointments on Sundays. Data Manipulation Triggers can be used to automatically change data before it is saved into storage. Example: Automatically updating the updated_at column to the current time whenever someone edits a row of data....