{"id":6742,"date":"2021-01-13T13:57:26","date_gmt":"2021-01-13T13:57:26","guid":{"rendered":"https:\/\/www.temok.com\/blog\/?p=6742"},"modified":"2025-11-24T14:34:55","modified_gmt":"2025-11-24T10:34:55","slug":"what-is-mysql-trigger-and-how-to-create-it","status":"publish","type":"post","link":"https:\/\/www.temok.com\/blog\/what-is-mysql-trigger-and-how-to-create-it\/","title":{"rendered":"What is MySQL Trigger: How To Create It?"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">min read<\/span><\/span>\r\n<p>Like me, you are also a MySQL user. Congratulations, you have chosen the world&#8217;s best and most popular open-source database. Now, there is no need to worry about your <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_programming_languages\" target=\"_blank\" rel=\"noopener\">programming language<\/a> or framework because MySQL offers application developers everything they need to build database-driven information systems. Today, I will discuss a comprehensive knowledge of MySQL triggers, also known as SQL triggers.\u00a0<span style=\"box-sizing: border-box; margin: 0px; padding: 0px;\">You can read this article if you are tech-savvy and want detailed information about MySQL triggers<\/span>.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>What is MySQL Trigger?<\/strong><\/h2>\r\n\r\n\r\n\r\n<p>MySQL Triggers are the database objects or stored programs invoked automatically when a defined action such as INSERT, UPDATE, or DELETE is executed for an associated database table. So, if you want to run database triggers on MySQL, you need SUPERUSER privileges. If you are hosting your website on a shared hosting environment, then you can&#8217;t run any trigger MySQL.<\/p>\r\n\r\n\r\n\r\n<p>It would be best to have a VPS (Virtual Private Server) Or <a href=\"https:\/\/www.temok.com\/dedicated-servers-japan\" target=\"_blank\" rel=\"noopener\">Dedicated Server<\/a> to get SUPERUSER rights to run triggers in MySQL.<\/p>\r\n\r\n\r\n\r\n<p>Mainly there are two types of SQL triggers: row-level triggers and statement-level triggers.<\/p>\r\n\r\n\r\n\r\n<p><strong>Row-Level trigger:<\/strong> It is activated or executed whenever you need to insert, update, or delete an event for each row. For example, if you want to delete 50 rows, then the database trigger should be invoked automatically 50 times for the 50 rows affected.<\/p>\r\n\r\n\r\n\r\n<p><strong>Statement-level Trigger:<\/strong> As the name indicates, it is executed once for each database transaction instead of how many rows are updated, deleted, or inserted.<\/p>\r\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/blog.temok.com\/sql-vs-mysql\/\" target=\"_blank\" rel=\"noopener\">SQL vs MySQL: Making the Right Database Choice For You<\/a><\/p>\r\n\r\n\r\n\r\n<p>MySQL doesn&#8217;t support statement-level SQL triggers and only executes the row-level triggers.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-6745\" src=\"https:\/\/i0.wp.com\/www.blog.temok.com\/wp-content\/uploads\/2021\/01\/01-1.jpg?resize=750%2C500&#038;ssl=1\" alt=\"What is MySQL Trigger\" width=\"750\" height=\"500\" srcset=\"https:\/\/i0.wp.com\/blog.temok.com\/wp-content\/uploads\/2021\/01\/01-1.jpg?w=750&amp;ssl=1 750w, https:\/\/i0.wp.com\/blog.temok.com\/wp-content\/uploads\/2021\/01\/01-1.jpg?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\"><strong>How to Create a MySQL Trigger?<\/strong><\/h2>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">CREATE TRIGGER [schema_name.]trigger_name\r\nON table_name\r\n{FOR | AFTER | INSTEAD OF} {[INSERT] [,] [UPDATE] [,] [DELETE]}\r\nAS\r\n{sql_statements}<\/pre>\r\n\r\n\r\n\r\n<p>What do you have to do? Define a set of SQL statements that you want to be executed when the trigger is fired. Now specify when the trigger will be executed, and it is set by the {FOR | AFTER | INSTEAD OF} section. INSERT, UPDATE, and DELETE options denote which SQL commands fire this trigger.<\/p>\r\n\r\n\r\n\r\n<p>If you want to drop or delete a MySQL trigger, then run the following command:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">DROP TRIGGER [schema_name.]trigger_name;<\/pre>\r\n\r\n\r\n\r\n<h3><strong>MySQL Trigger Example:<\/strong><\/h3>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Open SSH and create a database table so that we can execute a trigger on it.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mysql&gt; CREATE TABLE student (age INT, name varchar(150));<\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Now, define a trigger, which will be executed before every insert statement for the student table.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mysql&gt; delimiter \/\/ \r\nmysql&gt; CREATE TRIGGER agecheck BEFORE INSERT ON student FOR EACH ROW IF NEW.age &lt; 0 \r\nTHEN SET NEW.age = 0; END IF;\/\/ \r\nQuery OK, 0 rows affected (0.00 sec) mysql&gt; delimiter ;<\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Insert three records to confirm the trigger functionality.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mysql&gt; INSERT INTO student VALUES (-10, 'david'), (20, 'sara'), (30, 'viktor');<\/pre>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Here is the output.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT * FROM student;<\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">+\u2014\u2014-+\u2014\u2014-+ \r\n| age | name |\r\n+\u2014\u2014-+\u2014\u2014-+\r\n| 0 | David |\r\n| 20 | Sara |\r\n| 30 | Viktor |\r\n+\u2014\u2014-+\u2014\u2014-+<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">min read<\/span><\/span>Like me, you are also a MySQL user. Congratulations, you have chosen the world&#8217;s best and most popular open-source database. Now, there is no need to worry about your programming language or framework because MySQL offers application developers everything they need to build database-driven information systems. Today, I will discuss a comprehensive knowledge of MySQL [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":18185,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"pmpro_default_level":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2044],"tags":[868,872,869,873,876,874,870,875,871,3784],"class_list":["post-6742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","tag-database-triggers","tag-mysql-trigger","tag-mysql-triggers","tag-sql-triggers","tag-trigger-in-sql","tag-trigger-mysql","tag-triggers-in-mysql","tag-triggers-in-sql","tag-triggers-on-mysql","tag-what-is-mysql-trigger","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blog.temok.com\/wp-content\/uploads\/2021\/01\/What-is-MySQL-Trigger.jpg?fit=750%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/posts\/6742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/comments?post=6742"}],"version-history":[{"count":6,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/posts\/6742\/revisions"}],"predecessor-version":[{"id":16672,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/posts\/6742\/revisions\/16672"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/media\/18185"}],"wp:attachment":[{"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/media?parent=6742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/categories?post=6742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.temok.com\/blog\/wp-json\/wp\/v2\/tags?post=6742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}