{"id":18285,"date":"2024-05-24T16:38:49","date_gmt":"2024-05-24T16:38:49","guid":{"rendered":"https:\/\/hostvento.com\/kb\/web-hosting\/getting-started-guide\/how-to-use-the-knowledge-base\/mysql-triggers-what-are-they-and-how-to-use-them\/"},"modified":"2024-06-17T09:08:06","modified_gmt":"2024-06-17T09:08:06","slug":"mysql-triggers-what-are-they-and-how-to-use-them","status":"publish","type":"docs","link":"https:\/\/www.hostvento.com\/kb\/docs\/hosting-faqs\/mysql-triggers-what-are-they-and-how-to-use-them\/","title":{"rendered":"MySQL Triggers: What Are They and How to Use Them?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">MySQL Trigger Explained<\/h3>\n\n\n\n<p>A MySQL trigger is a database object that is connected to a table. When a certain action is carried out for the table, it becomes active. After executing the MySQL statements on the table, you can perform the trigger.as\u00a0<strong>INSERT<\/strong>,\u00a0<strong>UPDATE<\/strong>\u00a0and\u00a0<strong>DELETE<\/strong>\u00a0and it can be cited either prior to or after the event.<\/p>\n\n\n\n<p>A unique name should be used for each trigger associated with a table. But, you can have the same trigger name for different tables though it\u2019s a good practice.<\/p>\n\n\n\n<p>To run such MySQL Triggers you need to have&nbsp;<strong>MySQL SUPERUSER<\/strong>&nbsp;privileges.<\/p>\n\n\n\n<p>Hostvento doesn\u2019t offer such privileges on the&nbsp;<strong>shared<\/strong>&nbsp;and&nbsp;<strong>reseller<\/strong>&nbsp;hosting solutions. They offer these privileges on&nbsp;<strong>VPS<\/strong>,&nbsp;<strong>dedicated<\/strong>&nbsp;and&nbsp;<strong>cloud<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating and Using Triggers<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up a Test Database<\/h3>\n\n\n\n<p>We must build a database for testing in order to provide a simple example of a trigger in operation. Enter your account username in place of the username in the SQL query below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE DATABASE username_test;<\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;It is possible to run the previous SQL command (and the following SQL commands) from the command line with the MySQL tool, or in your web browser with the use of phpMyAdmin.<\/p>\n\n\n\n<p>In case of phpMyAdmin, select the database by clicking the name username_test . Or else if your are using the MySQL command-line program, input the below SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">USE username_test;<\/pre>\n\n\n\n<p>Now, create a table with the name products in the username_test database. Run the following SQL statement for this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE products(prod_id INT NOT NULL AUTO_INCREMENT, prod_name VARCHAR(20) NOT NULL,\nprod_cost FLOAT NOT NULL DEFAULT 0.0, prod_price FLOAT NOT NULL DEFAULT 0.0, PRIMARY KEY(prod_id));<\/pre>\n\n\n\n<p>Some sample data is added to the products table by using the below SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO products (prod_name, prod_cost, prod_price) VALUES ('Basic Widget',5.95,8.35),\n('Micro Widget',0.95,1.35),('Mega Widget',99.95,140.00);<\/pre>\n\n\n\n<p>Now we can create a trigger for our table!<\/p>\n\n\n\n<p><strong>Related:&nbsp;<a href=\"https:\/\/www.hostvento.com\/kb\/hosting-faqs\/import-export-mysql-database-phpmyadmin\/\">How to Import and Export the MySQL Database with phpMyAdmin?<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating the Trigger<\/h3>\n\n\n\n<p>We will create a trigger named as&nbsp;<strong>updateProductPrice<\/strong>. This type of trigger is activated every time the products table is updated. During the occurrence of this event, the trigger verifies each row to check if the product cost&nbsp;<strong>(prod_cost)<\/strong>&nbsp;value has got changed. If it is changed, then the item\u2019s new price&nbsp;<strong>(prod_price)<\/strong>&nbsp;will be automatically set up by the trigger to 1.40 times the item\u2019s new cost (in simple words, a 40% markup).<\/p>\n\n\n\n<p>Run the below MySQL statements to create this trigger:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DELIMITER $$\nCREATE TRIGGER `updateProductPrice`\nBEFORE UPDATE ON `products`\nFOR EACH ROW\nBEGIN\nIF NEW.prod_cost &lt;&gt; OLD.prod_cost\nTHEN\nSET NEW.prod_price = NEW.prod_cost * 1.40;\nEND IF ;\nEND$$\nDELIMITER ;<\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The&nbsp;<strong>DELIMITER<\/strong>&nbsp;command used at the start of these statements stops MySQL from processing the trigger definition too early. The&nbsp;<strong>DELIMITER<\/strong>&nbsp;command used at the end of these statements returns processing to normal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the Trigger<\/h3>\n\n\n\n<p>The&nbsp;<strong>updateProductPrice<\/strong>&nbsp;trigger can now be invoked automatically every time a row in the products table is updated.<\/p>\n\n\n\n<p>For example, let\u2019s change the cost of the&nbsp;<strong>Basic Widget<\/strong>&nbsp;by running the below SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">UPDATE products SET prod_cost = 7.00 WHERE prod_id = 1;<\/pre>\n\n\n\n<p>After running this SQL statement, the trigger activates and also, automatically updates the Basic Widget\u2019s price in proportion to the new cost. You can check this by running the following SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM products;<\/pre>\n\n\n\n<p>You get the following results:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+---------+--------------+-----------+------------+\n| prod_id | prod_name | prod_cost | prod_price |\n+---------+--------------+-----------+------------+\n| 1 | Basic Widget | 7 | 9.8 |\n| 2 | Micro Widget | 0.95 | 1.35 |\n| 3 | Mega Widget | 99.95 | 140 |\n+---------+--------------+-----------+------------+\n3 rows in set (0.00 sec)<\/pre>\n\n\n\n<p>You can check that the&nbsp;<strong>updateProductPrice<\/strong>&nbsp;trigger automatically updated the&nbsp;<strong>Basic Widget\u2019s<\/strong>&nbsp;price (9.80) based on the new cost (7.00). This is just a simple example of what a trigger can do but you use the same techniques in your own databases \u2013 you will get endless possibilities.<\/p>\n\n\n\n<p>So, you have learnt about MySQL triggers and their usage now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL Trigger Explained A MySQL trigger is a database object that is connected to a table. When a certain action is carried out for the table, it becomes active. After executing the MySQL statements on the table, you can perform the trigger.as\u00a0INSERT,\u00a0UPDATE\u00a0and\u00a0DELETE\u00a0and it can be cited either prior to or after the event. A unique [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":23377,"menu_order":42,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-18285","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/comments?post=18285"}],"version-history":[{"count":2,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18285\/revisions"}],"predecessor-version":[{"id":18290,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/18285\/revisions\/18290"}],"up":[{"embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/docs\/23377"}],"wp:attachment":[{"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/media?parent=18285"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.hostvento.com\/kb\/wp-json\/wp\/v2\/doc_tag?post=18285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}