Creating and using triggers
You can create and use triggers on any hosting.com server that uses MySQL.Setting up a test database
To demonstrate a basic example of a trigger in action, let’s start by creating a database for testing purposes. In the following SQL statement, replace username with your account username:You can run the previous SQL command (and the following SQL commands) from the command line using the MySQL tool, or in your web browser using phpMyAdmin.
Creating the trigger
Let’s create a trigger named updateProductPrice. This particular trigger is activated whenever the products table is updated. When this event occurs, the trigger checks each row to see if the product cost (prod_cost) value is being changed. If it is, then the trigger automatically sets the item’s new price (prod_price) to 1.40 times the item’s new cost (in other words, a 40% markup). To create this trigger, run the following MySQL statements:The DELIMITER command at the beginning of these statements prevents MySQL from processing the trigger definition too soon. The DELIMITER command at the end of these statements returns processing to normal.
Using the trigger
The updateProductPrice trigger is now ready to be invoked automatically whenever a row in the products table is updated. For example, run the following SQL statement to change the cost of the Basic Widget:- For more information about MySQL triggers, please visit https://dev.mysql.com/doc/refman/8.4/en/triggers.html.
- For more information about the CREATE TRIGGER statement, please visit https://dev.mysql.com/doc/refman/8.4/en/create-trigger.html.