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

Tuesday, 6 February 2024

MariaDB Triggers: Creating and Managing

MariaDB triggers are database objects that automatically execute in response to certain events on a particular table. They enable you to enforce business rules, implement complex data integrity constraints, and automate tasks within the database. Here's how you create and manage triggers in MariaDB:


1. Creating a Trigger:


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


   

   CREATE TRIGGER trigger_name

   {BEFORE | AFTER} {INSERT | UPDATE | DELETE}

   ON table_name

   FOR EACH ROW

   BEGIN

       -- Trigger body (SQL statements)

   END;

   


   - trigger_name: The name of the trigger.

   - {BEFORE | AFTER}: Specifies whether the trigger should execute before or after the triggering event.

   - {INSERT | UPDATE | DELETE}: Specifies the type of event that triggers the execution of the trigger.

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

   - FOR EACH ROW: Indicates that the trigger should be invoked for each row affected by the triggering event.


   Inside the BEGIN ... END block, you define the SQL statements to be executed when the trigger fires.


2. Example:


   Here's an example of creating a simple trigger that automatically updates a timestamp column whenever a row is inserted or updated in a table:


   

   CREATE TRIGGER update_timestamp

   BEFORE INSERT ON employees

   FOR EACH ROW

   BEGIN

       SET NEW.updated_at = NOW();

   END;

   


   In this example, update_timestamp is the name of the trigger, and it updates the updated_at column in the employees table with the current timestamp before each INSERT operation.


3. Managing Triggers:


   Once created, triggers can be managed using various statements:


   - SHOW TRIGGERS: Displays a list of triggers defined in the database.

   - DROP TRIGGER: Removes a trigger from the database.

   - ALTER TRIGGER: Modifies the definition of an existing trigger.

   - USE: Selects the database in which the trigger exists.


4. Example:


   To drop the update_timestamp trigger created earlier:


   

   DROP TRIGGER IF EXISTS update_timestamp;

   


   This statement removes the update_timestamp trigger from the database, provided it exists.


5. Viewing Trigger Status:


   You can check the status of triggers in the database using the INFORMATION_SCHEMA.TRIGGERS table:


   

   SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_SCHEMA = 'your_database_name';

   


   Replace 'your_database_name' with the name of your database.


By understanding how to create, manage, and use triggers in MariaDB, you can leverage them to automate tasks and enforce data integrity rules within your database applications.

No comments:

Post a Comment

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