Dino Geek, try to help you

How to use triggers in MariaDB?


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`.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use