Wednesday , April 24 2024

Triggers in MySQL

In the article, the use of triggers in MySQL Systems is explained with examples.

What is Trigger?

Triggers in MySQL are a special type of MySQL Stored Procedures.

The most important feature that distinguishes triggers from stored procedures is that we need to run the stored procedures manually, while triggers work spontaneously when changes are made to the data.

Triggers are often used to ensure data integrity and security.

Triggers in MySQL do not return any value.

Create a MySQL Trigger

MySQL triggers are run before or after INSERT, UPDATE and DELETE operations.

Triggers are created with the keyword CREATE TRIGGER.

Let’s create a trigger that will record information about the added product to the “process” table when a product is added to the “products” table.

After creating the trigger, when a new record is added to the “products” table, the record for the product will be added to the “process”.

Using the trigger creation structure above, let’s create the trigger that inserts the deleted product name to the “process” table if a record is deleted from the “products” table.

Note that the information about the added product is obtained with the keyword NEW, and the information about the deleted product with the keyword OLD.

We can also use expressions such as variable definition, if, elseif, else, case when, while used in MySQL stored procedures in the trigger.

However, using too many triggers will negatively affect the performance of the database server.

Maybe you may have wondered the answer to a question.

Maybe you may have wondered the answer to a question.

Does MySQL support DDL Trigger?

Unfortunately MySQL does not support DDL Trigger.

Have a good day.

Loading

About Yusuf SEZER

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories