{"id":4717,"date":"2021-07-29T04:39:43","date_gmt":"2021-07-29T04:39:43","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=4717"},"modified":"2021-07-29T05:07:03","modified_gmt":"2021-07-29T05:07:03","slug":"mysql-triggers","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/mysql-triggers\/","title":{"rendered":"MySQL Triggers"},"content":{"rendered":"<p>MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event. Each Trigger is activated on any DML statement such as INSERT, UPDATE, DELETE, SELECT. Each Trigger is associated with the database table.<\/p>\n<p>A trigger seems to be the same as a procedure, the main difference is that trigger is called automatically when any data modification is made against the table. <a href=\"https:\/\/studysection.com\/blog\/introduction-to-triggers-and-views-in-sql\/\">Triggers<\/a> cannot be called directly that\u2019s why they are called special procedures. In contrast, stored procedures must be called explicitly.<\/p>\n<h2>Triggers are of 2 types according to the SQL standard.<\/h2>\n<ul>\n<li>Row Level Triggers<\/li>\n<li>Statement Level Triggers<\/li>\n<\/ul>\n<p><strong>Row Level Trigger:<\/strong> A row Level trigger is a trigger that is activated for each row by a triggering statement such as insert, update or delete. For example, if insertion, deletion or updation is made in a table then the triggers are called automatically upon the rows affected by the respective statement. <\/p>\n<p><strong>Statement Level Trigger:<\/strong> A statement Level trigger is a trigger that is fired once for each event that occurs on a table regardless of how many rows are inserted, updated, or deleted.<\/p>\n<p>We need triggers for the following reasons.<\/p>\n<ul>\n<li>Triggers help us to validate data even before it is inserted.<\/li>\n<li>An Alternate way is provided by triggers to run the scheduled task.<\/li>\n<li>Triggers increase the performance of SQL queries because it does not need to be compiled each time the query is executed. <\/li>\n<li>Triggers save time and effort as client-side code is reduced.<\/li>\n<li>Triggers also help to scale our application across different platforms.<\/li>\n<li>Triggers are easy to maintain.<\/li>\n<\/ul>\n<h3>Limitations of using Triggers in MySql<\/h3>\n<ul>\n<li>MySQL triggers do not allow the use of all validations; they only provide extended validations. For example, we can use the NOT NULL, UNIQUE, CHECK, and FOREIGN KEY constraints for simple validations.<\/li>\n<li>A Client application is not aware of the execution of triggers. Therefore, it is not easy to recognize what happens in the database layer.<\/li>\n<li>Triggers may increase the overall cost of the database server.<\/li>\n<\/ul>\n<h3>Types of triggers in MYSQL<\/h3>\n<ul>\n<li><strong>Before Insert:<\/strong> Trigger is activated before the insertion of data into the table.<\/li>\n<li><strong>After Insert:<\/strong> Trigger is activated after the insertion of data into the table.<\/li>\n<li><strong>Before Update:<\/strong> Trigger is activated before the data update into the table.<\/li>\n<li><strong>After Update:<\/strong> Trigger is activated after the data update into the table.<\/li>\n<li><strong>Before Delete:<\/strong> Trigger is activated before the data is removed from the table.<\/li>\n<li><strong>After Delete:<\/strong> Trigger is activated after the data deletion from the table.<\/li>\n<\/ul>\n<p><strong>Naming Conventions<\/strong><br \/>\nA unique name is used for each trigger associated with each table.<br \/>\nFollowing are the instructions to name the trigger.<\/p>\n<p><strong><em>(BEFORE | AFTER) table_name (INSERT | UPDATE | DELETE)<\/em><\/strong><\/p>\n<p><em>Trigger Activation Time : Before\/After<br \/>\nTrigger Event : Insert\/Update\/Delete<\/em><\/p>\n<p><strong>Statement to create the trigger:<\/strong><br \/>\n<code>CREATE TRIGGER trigger_name<br \/>\n    (AFTER | BEFORE) (INSERT | UPDATE | DELETE)<br \/>\n         ON table_name FOR EACH ROW<br \/>\n         BEGIN<br \/>\n        --variable declarations<br \/>\n        --trigger code<br \/>\n        END;<\/code><\/p>\n<h3>Let&#8217;s Take an example to understand the Triggers<\/h3>\n<p>Now, we will create a Trigger that makes modifications to the employee table. Let\u2019s create a new table employee by executing the below statement:<br \/>\n<code>CREATE TABLE employee(<br \/>\n    name varchar(45) NOT NULL,<br \/>\n    occupation varchar(35) NOT NULL,<br \/>\n    working_date date,<br \/>\n    working_hours varchar(10)<br \/>\n);<br \/>\nNext, execute the below statement to fill the records into the employee table:<br \/>\nINSERT INTO employee VALUES<br \/>\n('Sunil', 'Scientist', '2021-10-04', 12),<br \/>\n('Rahul', 'Engineer', '2021-10-04', 10),<br \/>\n('Raman', 'Teacher', '2021-10-04', 13),<br \/>\n('Mandeep', 'Doctor', '2021-10-04', 14)<\/code><br \/>\nNext, execute the SELECT statement to verify the inserted record:<\/p>\n<table class=\"table table-striped\">\n<thead>\n<tr>\n<th>name<\/th>\n<th>occupation<\/th>\n<th>working_date<\/th>\n<th>working_hours<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Sunil<\/td>\n<td>Scientist<\/td>\n<td>2021-10-04<\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td>Rahul<\/td>\n<td>Engineer<\/td>\n<td>2021-10-04<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>Raman<\/td>\n<td>Teacher<\/td>\n<td>2021-10-04<\/td>\n<td>13<\/td>\n<\/tr>\n<tr>\n<td>Mandeep<\/td>\n<td>Doctor<\/td>\n<td>2021-10-04<\/td>\n<td>14<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Next, we will create a BEFORE INSERT trigger. This trigger is automatically invoked,  setting the working_hours to 0 if someone tries to insert a negative value for working_hours.<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/07\/cmd-1.png\" alt=\"SQL\"\/><br \/>\nNow, we can use the following statements to invoke this trigger:<\/p>\n<p><code>mysql> INSERT INTO employee VALUES<br \/>\n('Rajan', 'Former', '2021-10-08', 14);<br \/>\nmysql> INSERT INTO employee VALUES<br \/>\n('Atinder', 'Actor', '2021-10-12', -13);<br \/>\nNow run SELECT * FROM emp;<\/code><\/p>\n<table class=\"table table-striped\">\n<thead>\n<tr>\n<th>name<\/th>\n<th>occupation<\/th>\n<th>working_date<\/th>\n<th>working_hours<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Sunil<\/td>\n<td>Scientist<\/td>\n<td>2021-10-04<\/td>\n<td>12<\/td>\n<\/tr>\n<tr>\n<td>Rahul<\/td>\n<td>Engineer<\/td>\n<td>2021-10-04<\/td>\n<td>10<\/td>\n<\/tr>\n<tr>\n<td>Raman<\/td>\n<td>Teacher<\/td>\n<td>2021-10-04<\/td>\n<td>13<\/td>\n<\/tr>\n<tr>\n<td>Mandeep<\/td>\n<td>Doctor<\/td>\n<td>2021-10-04<\/td>\n<td>14<\/td>\n<\/tr>\n<tr>\n<td>Rajan<\/td>\n<td>Former<\/td>\n<td>2021-10-08<\/td>\n<td>14<\/td>\n<\/tr>\n<tr>\n<td>Atinder<\/td>\n<td>Actor<\/td>\n<td>2021-10-12<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Note: Working hours inserted as 0 for the name: Atinder<\/p>\n<p><small><em>Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a <a href=\"https:\/\/www.studysection.com\/c-sharp-net-web-developer-advanced-diploma\">.NET Certification Exams<\/a> from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to<\/p>\n","protected":false},"author":1,"featured_media":4726,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[306,43],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MySQL Triggers - StudySection Blog<\/title>\n<meta name=\"description\" content=\"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/mysql-triggers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Triggers - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/mysql-triggers\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-29T04:39:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-29T05:07:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/07\/mysql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"466\" \/>\n\t<meta property=\"og:image:height\" content=\"293\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"MySQL Triggers\",\"datePublished\":\"2021-07-29T04:39:43+00:00\",\"dateModified\":\"2021-07-29T05:07:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/\"},\"wordCount\":666,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"SQL\",\"triggers\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/mysql-triggers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/\",\"url\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/\",\"name\":\"MySQL Triggers - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-07-29T04:39:43+00:00\",\"dateModified\":\"2021-07-29T05:07:03+00:00\",\"description\":\"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/mysql-triggers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/mysql-triggers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Triggers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Triggers - StudySection Blog","description":"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/mysql-triggers\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Triggers - StudySection Blog","og_description":"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.","og_url":"https:\/\/studysection.com\/blog\/mysql-triggers\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-07-29T04:39:43+00:00","article_modified_time":"2021-07-29T05:07:03+00:00","og_image":[{"width":466,"height":293,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/07\/mysql.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"MySQL Triggers","datePublished":"2021-07-29T04:39:43+00:00","dateModified":"2021-07-29T05:07:03+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/"},"wordCount":666,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["SQL","triggers"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/mysql-triggers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/","url":"https:\/\/studysection.com\/blog\/mysql-triggers\/","name":"MySQL Triggers - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-07-29T04:39:43+00:00","dateModified":"2021-07-29T05:07:03+00:00","description":"MySQL Triggers are a set of SQL statements. It is a stored procedure that is invoked automatically in response to any event.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/mysql-triggers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/mysql-triggers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"MySQL Triggers"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":468,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4717"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=4717"}],"version-history":[{"count":7,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4717\/revisions"}],"predecessor-version":[{"id":4727,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4717\/revisions\/4727"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/4726"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=4717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=4717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=4717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}