{"id":21049,"date":"2020-08-10T09:27:03","date_gmt":"2020-08-10T16:27:03","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=21049"},"modified":"2021-01-04T05:51:39","modified_gmt":"2021-01-04T13:51:39","slug":"sql-triggers","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/sql-triggers\/","title":{"rendered":"SQL Triggers"},"content":{"rendered":"\n<p>An SQL Trigger is an event that sets off a number of other events that happens when a user interacts with a particular table. This particular SQL statement helps by validating form data, or enforcing rules.<br><\/p>\n\n\n\n<p>A trigger is executed either before or after the following events:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>INSERT \u2013 when a new row is inserted.<\/li><li>UPDATE \u2013 when an existing row is updated.<\/li><li>DELETE \u2013 when a row is deleted.<\/li><\/ul>\n\n\n\n<p>Which trigger goes off and what happens depends on how it is written. Let\u2019s get started by crafting the create statement:&nbsp;<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>CREATE TRIGGER &lt;trigger_name&gt; [BEFORE|AFTER] [INSERT|UPDATE|DELETE]\nON &lt;table_name&gt; [FOR EACH ROW|FOR EACH STATEMENT]\nBEGIN\n  &lt;trigger_logic&gt;\nEND;<\/pre><\/div>\n\n\n\n<p>Between any set of &lt;&gt;, replace with your custom names or logic. Inside the [ ], choose one choice from the available given.&nbsp;<br><\/p>\n\n\n\n<p>BEFORE\/AFTER and INSERT|UPDATE|DELETE: Choose one from each list. BEFORE\/AFTER will determine when the trigger happens. INSERT\/UPDATE\/DELETE is the event that occurs that will start the trigger.&nbsp;<br><\/p>\n\n\n\n<p>FOR EACH ROW|FOR EACH STATEMENT: The difference between the two choices here is the difference between how many times the trigger runs. FOR EACH ROW triggers on each row affected in the table. FOR EACH STATEMENT is always guaranteed to run at least once, whether or not any rows are updated.<br><\/p>\n\n\n\n<p>Similar to databases or tables, there is a DROP TRIGGER option:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>DROP TRIGGER [IF EXISTS] trigger_name;<\/pre><\/div>\n\n\n\n<p>IF EXISTS is an optional portion of the syntax. If included in the statement block, the portion prevents a \u201ctrigger doesn\u2019t exist\u201d error. A deleted table automatically drops all associated triggers as well.<br><\/p>\n\n\n\n<p>Know that SQL trigger requires three main things: a create statement that gives the trigger its name and when the event occurs, how often the trigger occurs, and then a BEGIN\/END block that lays out the logic for the actual trigger.&nbsp;<br><\/p>\n\n\n\n<p>Keep practicing if you don\u2019t get it at first \u2013 because it\u2019s an advanced concept in SQL, it will likely take more repetition than other topics and functions in SQL to cover.<\/p>\n","protected":false},"excerpt":{"rendered":"An SQL Trigger is an event that sets off a number of other events that happens when a user interacts with a particular table. This particular SQL statement helps by validating form data, or enforcing rules. A trigger is executed either before or after the following events: INSERT \u2013 when a new row is inserted.UPDATE&hellip;","protected":false},"author":77,"featured_media":21050,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17284],"tags":[],"class_list":{"0":"post-21049","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-sql"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"SQL","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SQL Triggers: A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Triggers\" \/>\n<meta property=\"og:description\" content=\"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-10T16:27:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-04T13:51:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Christina Kopecky\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Christina Kopecky\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\"},\"author\":{\"name\":\"Christina Kopecky\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"headline\":\"SQL Triggers\",\"datePublished\":\"2020-08-10T16:27:03+00:00\",\"dateModified\":\"2021-01-04T13:51:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\"},\"wordCount\":321,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg\",\"articleSection\":[\"SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\",\"name\":\"SQL Triggers: A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg\",\"datePublished\":\"2020-08-10T16:27:03+00:00\",\"dateModified\":\"2021-01-04T13:51:39+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\"},\"description\":\"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/sql-triggers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg\",\"width\":1020,\"height\":680,\"caption\":\"\u201cStarting\u201d a transaction that triggers a number of events\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/sql-triggers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL\",\"item\":\"https:\/\/careerkarma.com\/blog\/sql\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Triggers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e\",\"name\":\"Christina Kopecky\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg\",\"caption\":\"Christina Kopecky\"},\"description\":\"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.\",\"sameAs\":[\"http:\/\/www.linkedin.com\/in\/cmvnk\"],\"url\":\"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SQL Triggers: A Complete Guide | Career Karma","description":"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!","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:\/\/careerkarma.com\/blog\/sql-triggers\/","og_locale":"en_US","og_type":"article","og_title":"SQL Triggers","og_description":"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!","og_url":"https:\/\/careerkarma.com\/blog\/sql-triggers\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-08-10T16:27:03+00:00","article_modified_time":"2021-01-04T13:51:39+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg","type":"image\/jpeg"}],"author":"Christina Kopecky","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Christina Kopecky","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/"},"author":{"name":"Christina Kopecky","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"headline":"SQL Triggers","datePublished":"2020-08-10T16:27:03+00:00","dateModified":"2021-01-04T13:51:39+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/"},"wordCount":321,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg","articleSection":["SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/sql-triggers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/","url":"https:\/\/careerkarma.com\/blog\/sql-triggers\/","name":"SQL Triggers: A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg","datePublished":"2020-08-10T16:27:03+00:00","dateModified":"2021-01-04T13:51:39+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e"},"description":"Triggers in SQL are basically events that occur when a use is interacting with a table in our application. Learn about these in this article on Career Karma!","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/sql-triggers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/morning-brew-HPEFY5R2XQQ-unsplash-1.jpg","width":1020,"height":680,"caption":"\u201cStarting\u201d a transaction that triggers a number of events"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/sql-triggers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SQL","item":"https:\/\/careerkarma.com\/blog\/sql\/"},{"@type":"ListItem","position":3,"name":"SQL Triggers"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/ae0cdc4a5d198690d78482646894074e","name":"Christina Kopecky","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/06\/image-3-150x150.jpg","caption":"Christina Kopecky"},"description":"Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.","sameAs":["http:\/\/www.linkedin.com\/in\/cmvnk"],"url":"https:\/\/careerkarma.com\/blog\/author\/christina-kopecky\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/21049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=21049"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/21049\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/21050"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=21049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=21049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=21049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}