What are MySQL triggers and how to use them?

The MySQL trigger is a database object that is associated with a table. It will be activated when a defined action is executed for the table. The trigger can be executed when you run one of the following MySQL statements on the table: INSERT, UPDATE and DELETE and it can be invoked before or after the event.

You can find detailed explanation of the trigger functionality and syntax in this article.

The main requirement for running such MySQL Triggers is having MySQL SUPERUSER privileges.

On the servers offered by SiteGround, such privileges can be granted on the Cloud and Dedicated hosting solutions. Granting SUPERUSER MySQL privileges to a user hosted on a shared server is not possible due to our server setup.

Here is an example of a MySQL trigger:

  • First we will create the table for which the trigger will be set via SSH:

  • Next we will define the trigger. It will be executed before every INSERT statement for the people table:

  • We will insert two records to check the trigger functionality.

  • At the end we will check the result.

cPanel is easy to work with when you have the right host to support you. If you need a reliable partner to help you manage your website with cPanel, check out our cPanel hosting services!