Triggers in MariaDB are database operations that run automatically when a particular event happens, such as when data is modified. The events can be insertions, updates, or deletions.
Follow these steps to create a trigger:
1. Use `CREATE TRIGGER` statement: The statement for creating a trigger is `CREATE TRIGGER`. This statement is tailored with the trigger name, time of trigger activation, triggering event, the table associated with the trigger and the trigger body.
1. Specify trigger name: Choose a unique name for the trigger for quick identification.
1. Trigger activation time: This could either be `BEFORE` or `AFTER`. If it’s `BEFORE`, the trigger is activated before the triggering event happens, and if it’s `AFTER`, it happens after the event.
1. Triggering event: This is the event that activates the trigger. It could be `INSERT`, `UPDATE` or `DELETE`.
1. Associated table: This is the table on which the operations are performed.
1. Trigger body: This is the actual SQL code to be executed when the trigger is activated. It can be a simple or compound statement enclosed within `BEGIN…END`.
Here is an example of how to create a simple trigger:
```
CREATE TRIGGER before_employee_update
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
SET NEW.updated_at = NOW;
END;
```
In the example above, we’ve created a trigger named `before_employee_update` which updates the `updated_at` field with the current time each time any record is updated in the `employees` table.
Remember, in MariaDB, you must use `//` to change the statement delimiter before you define a trigger, because triggers often consist of multiple statements, which will cause errors if you try to create them with the standard `;` delimiter.
You can remove a trigger using the `DROP TRIGGER` statement:
```
DROP TRIGGER before_employee_update;
```
The `DROP TRIGGER` statement above will drop the trigger named `before_employee_update`.