Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Tuesday, 6 February 2024

PostgreSQL Triggers: Creating and Managing

PostgreSQL triggers are special types of stored procedures that are automatically executed or fired in response to specific events occurring in the database. Triggers can be useful for enforcing data integrity, implementing complex business logic, or auditing changes to database tables. Here's how you can create and manage triggers in PostgreSQL:


 Creating Triggers:


To create a trigger in PostgreSQL, you use the CREATE TRIGGER statement. The basic syntax is as follows:



CREATE TRIGGER trigger_name

    { BEFORE | AFTER | INSTEAD OF } { event(s) }

    ON table_name

    [ FOR EACH { ROW | STATEMENT } ]

    EXECUTE FUNCTION trigger_function();



- trigger_name: The name of the trigger.

- BEFORE / AFTER / INSTEAD OF: Specifies when the trigger will be fired in relation to the event.

- event(s): The database event(s) that will activate the trigger (e.g., INSERT, UPDATE, DELETE).

- table_name: The name of the table on which the trigger is defined.

- FOR EACH { ROW | STATEMENT }: Specifies whether the trigger should be fired for each row affected by the event or once per statement.

- trigger_function(): The name of the function to be executed when the trigger is fired.


 Example:


Let's create a simple trigger that logs changes to the employees table:



CREATE OR REPLACE FUNCTION log_employee_changes()

RETURNS TRIGGER AS $$

BEGIN

    IF TG_OP = 'INSERT' THEN

        INSERT INTO employee_audit (event_type, event_time, employee_id, employee_name)

        VALUES ('INSERT', NOW(), NEW.employee_id, NEW.employee_name);

    ELSIF TG_OP = 'UPDATE' THEN

        INSERT INTO employee_audit (event_type, event_time, employee_id, employee_name)

        VALUES ('UPDATE', NOW(), NEW.employee_id, NEW.employee_name);

    ELSIF TG_OP = 'DELETE' THEN

        INSERT INTO employee_audit (event_type, event_time, employee_id, employee_name)

        VALUES ('DELETE', NOW(), OLD.employee_id, OLD.employee_name);

    END IF;

    RETURN NULL; -- We don't want to change the original operation

END;

$$ LANGUAGE plpgsql;


CREATE TRIGGER employee_changes_trigger

AFTER INSERT OR UPDATE OR DELETE ON employees

FOR EACH ROW

EXECUTE FUNCTION log_employee_changes();



 Managing Triggers:


# Listing Triggers:


You can list the triggers associated with a table using the \d+ command in psql:



\d+ table_name;



# Dropping Triggers:


To drop a trigger, you use the DROP TRIGGER statement:



DROP TRIGGER [ IF EXISTS ] trigger_name ON table_name;



PostgreSQL triggers offer a powerful way to automate actions based on database events. They can be used for various purposes such as auditing, enforcing business rules, and maintaining data integrity. When creating triggers, make sure to consider their impact on performance and maintainability, and use them judiciously.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.