{"id":30069,"date":"2021-01-18T03:07:47","date_gmt":"2021-01-17T19:07:47","guid":{"rendered":"https:\/\/web.mwwsb.com.my\/pjci\/?post_type=kb&#038;p=30069"},"modified":"2022-09-08T21:33:56","modified_gmt":"2022-09-08T13:33:56","slug":"what-are-mysql-triggers-and-how-to-use-them","status":"publish","type":"kb","link":"https:\/\/www.casbay.com\/guide\/kb\/what-are-mysql-triggers-and-how-to-use-them","title":{"rendered":"What are MySQL triggers and how to use them?"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"30069\" class=\"elementor elementor-30069\" data-elementor-post-type=\"kb\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-f32c675 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f32c675\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-e03f6bf\" data-id=\"e03f6bf\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-bce759f elementor-widget elementor-widget-heading\" data-id=\"bce759f\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">What are MySQL triggers and how to use them?<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fd6c757 elementor-widget elementor-widget-text-editor\" data-id=\"fd6c757\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>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. Then, the trigger can be executed when you run one of the following MySQL statements on the table:\u00a0<em><strong>INSERT<\/strong>,\u00a0<strong>UPDATE<\/strong><\/em>\u00a0and\u00a0<em><strong>DELETE<\/strong><\/em> and it can be invoked before or after the event.\u00a0<\/p><p>In addition, you can find detailed explanation of the trigger functionality and syntax\u00a0<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/triggers.html\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" link=\"external\" class=\"broken_link\">in this article<\/a>.<\/p><p>In fact, the main requirement for running such MySQL Triggers is having MySQL\u00a0<strong>SUPERUSER<\/strong>\u00a0privileges.<\/p><p>Such privileges can be granted on the\u00a0<a href=\"https:\/\/www.casbay.com\/vps-hosting-malaysia\">VPS\u00a0<\/a>and <a href=\"https:\/\/www.casbay.com\/asia-dedicated-server\/malaysia\">Dedicated Servers<\/a>. Granting\u00a0<strong>SUPERUSER<\/strong>\u00a0MySQL privileges to a user hosted on a Shared Server is not possible due to our server setup.<\/p><p>Here is an example of a MySQL trigger:<\/p><p>1. Firstly, we will create the table for which the trigger will be set via <strong>SSH.<\/strong><\/p><pre class=\"terminal\">mysql&gt;\u00a0CREATE\u00a0TABLE\u00a0people\u00a0(age\u00a0INT,\u00a0name\u00a0varchar(150));<\/pre><p>2. Next we will define the trigger. It will be executed before every INSERT statement for the people table.<\/p><pre class=\"terminal\">mysql&gt; delimiter \/\/mysql&gt; CREATE TRIGGER agecheck BEFORE INSERT ON people <strong>FOR\u00a0EACH<\/strong>\u00a0ROW\u00a0<strong>IF\u00a0NEW<\/strong>.age\u00a0&lt;\u00a00\u00a0<strong>THEN<\/strong>\u00a0SET\u00a0<strong>NEW<\/strong>.age\u00a0=\u00a00;\u00a0<strong>END\u00a0IF<\/strong>;\/\/\u00a0Query\u00a0OK,\u00a00\u00a0rows\u00a0affected\u00a0(0.00\u00a0sec)mysql&gt;\u00a0delimiter\u00a0;<\/pre><p>3. Then, we will insert two records to check the trigger functionality.<\/p><pre class=\"terminal\">mysql&gt;\u00a0INSERT\u00a0INTO\u00a0people\u00a0VALUES\u00a0(-20,\u00a0\u2018Adam\u2019),\u00a0(30,\u00a0\u2018Mark\u2019);Query\u00a0OK,\u00a02\u00a0rows\u00a0affected\u00a0(0.00\u00a0sec)Records:\u00a02\u00a0Duplicates:\u00a00\u00a0Warnings:\u00a00<\/pre><p>4. Lastly, we will check the result.<\/p><pre class=\"terminal\">mysql&gt;\u00a0SELECT *\u00a0FROM\u00a0people;+\u2014\u2014-+\u2014\u2014-+|\u00a0age\u00a0|\u00a0name\u00a0|+\u2014\u2014-+\u2014\u2014-+|\u00a00\u00a0|\u00a0Adam\u00a0||\u00a030\u00a0|\u00a0Mark\u00a0|+\u2014\u2014-+\u2014\u2014-+2\u00a0rows\u00a0in\u00a0set\u00a0(0.00\u00a0sec)<\/pre>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8a041b0 elementor-widget elementor-widget-text-editor\" data-id=\"8a041b0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>We hope this article helped you to learn about what are MySQL triggers and how to use them. For more articles, kindly visit our <a href=\"https:\/\/www.casbay.com.my\/guide\/\">Knowledge Base<\/a>.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>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. Then, the trigger can be executed when you run one of the following MySQL statements on the table:\u00a0INSERT,\u00a0UPDATE\u00a0and\u00a0DELETE and it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"no-sidebar","site-content-layout":"page-builder","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}}},"kbtopic":[117,60],"kbtag":[106],"mkb_version":[],"_links":{"self":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30069"}],"collection":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/comments?post=30069"}],"version-history":[{"count":5,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30069\/revisions"}],"predecessor-version":[{"id":36548,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/30069\/revisions\/36548"}],"wp:attachment":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/media?parent=30069"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=30069"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtag?post=30069"},{"taxonomy":"mkb_version","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/mkb_version?post=30069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}