{"id":631,"date":"2019-06-05T01:54:33","date_gmt":"2019-06-05T09:54:33","guid":{"rendered":"https:\/\/db2tutorial.com\/?page_id=631"},"modified":"2020-04-11T06:58:47","modified_gmt":"2020-04-11T14:58:47","slug":"db2-trigger","status":"publish","type":"page","link":"https:\/\/www.db2tutorial.com\/db2-trigger\/","title":{"rendered":"Db2 Trigger"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about Db2 triggers, some types of triggers in Db2, and the differences between triggers, stored procedures, and check constraint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a trigger<\/h2>\n\n\n\n<p>A trigger is a piece of code that is automatically executed, or fired in response to a data modification event on a table. The data modification event include insert, update, delete driven by an <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-insert\/\">INSERT<\/a><\/code>, <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-delete\/\">DELETE<\/a><\/code>, <code><a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-update\/\">UPDATE<\/a><\/code>, and <code>MERGE<\/code> statement.<\/p>\n\n\n\n<p>Db2 stores the triggers in the database catalog, where each trigger is attached to a single table. You cannot call a trigger directly, only Db2 can call the trigger when an event occurs on the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DB2 trigger types<\/h2>\n\n\n\n<p>DB2 supports the following type of triggers:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>INSERT<\/code> trigger is activated when an <code>INSERT<\/code> or <code>MERGE<\/code> statement inserts data into the table.<\/li><li><code>UPDATE<\/code> trigger is activated when an <code>UPDATE<\/code> or <code>MERGE<\/code> statement modifies the data of a table.<\/li><li><code>DELETE<\/code> trigger is activated whenever a <code>DELETE<\/code> statement executes against the table.<\/li><\/ul>\n\n\n\n<p>For each type of trigger, Db2 further classifies as:<\/p>\n\n\n\n<p><strong>BEFORE <\/strong>triggers are fired before an <code>UPDATE<\/code> or <kbd>INSERT<\/kbd> operation, and you can change the values that are being updated or inserted before they are written to the database. Generally, you use these triggers for data cleansing and modification.<\/p>\n\n\n\n<p><strong>AFTER<\/strong> triggers are fired after an &nbsp;<code>INSERT<\/code>, &nbsp;<code>UPDATE<\/code>, or &nbsp;<code>DELETE<\/code>&nbsp;operation. Typically, you use these types of triggers to maintain relationships between data or to keep audit trail information.<\/p>\n\n\n\n<p><strong>INSTEAD<\/strong>&nbsp;<span class=\"Charactercode\"><strong>OF<\/strong>&nbsp;<\/span>triggers define how to perform an &nbsp;<code>INSERT<\/code>, &nbsp;<code>UPDATE<\/code>, or &nbsp;<code>DELETE<\/code> operation on a view to enable non-updateable views to become updatable views.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Triggers vs. stored procedures<\/h2>\n\n\n\n<p>Triggers are like stored procedures in that they contain procedure code, or logic.<\/p>\n\n\n\n<p>The following table illustrates the main difference between stored procedures and triggers:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Characteristics<\/th><th>Triggers<\/th><th>Stored Procedure<\/th><\/tr><\/thead><tbody><tr><td>Event-driven<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Attached to a single table<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Return a result<\/td><td>No<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Triggers vs. check constraints<\/h2>\n\n\n\n<p>Triggers are similar to check constraints because triggers can be used to control the integrity of data in a table. <a href=\"https:\/\/db2tutorial.com\/db2-basics\/db2-check-constraint\/\">Check constraints<\/a> specify what data can be stored in a table, but triggers can do that, plus make changes to data.<\/p>\n\n\n\n<p>Triggers can access data from other tables while check constraints can only access data in the table to which they belong.<\/p>\n\n\n\n<p>Additionally, triggers have more information on the data changes because they can see both old and new values of the changed columns and take actions based on that information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When to use triggers<\/h2>\n\n\n\n<p>You should use triggers whenever you cannot use check constraints to implement complex business rules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Managing triggers<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/db2tutorial.com\/db2-trigger\/db2-create-trigger\/\">Creating a new trigger<\/a> &#8211; learn how to use the <code><a href=\"https:\/\/db2tutorial.com\/db2-trigger\/db2-create-trigger\/\">CREATE TRIGGER<\/a><\/code> statement to create a new trigger in the database.<\/li><li><a href=\"https:\/\/db2tutorial.com\/db2-trigger\/db2-drop-trigger\/\">Removing a trigger<\/a> &#8211; describe how to use the <code>DROP TRIGGER<\/code> statement to remove a trigger from the database.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"631\"\n\t\t\t\tdata-post-url=\"https:\/\/www.db2tutorial.com\/db2-trigger\/\"\n\t\t\t\tdata-post-title=\"Db2 Trigger\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"631\"\n\t\t\t\tdata-post-url=\"https:\/\/www.db2tutorial.com\/db2-trigger\/\"\n\t\t\t\tdata-post-title=\"Db2 Trigger\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This section covers the Db2 triggers and shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.<\/p>\n","protected":false},"author":1,"featured_media":642,"parent":0,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-631","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Db2 Trigger<\/title>\n<meta name=\"description\" content=\"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.db2tutorial.com\/db2-trigger\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Db2 Trigger\" \/>\n<meta property=\"og:description\" content=\"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.db2tutorial.com\/db2-trigger\/\" \/>\n<meta property=\"og:site_name\" content=\"DB2 Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T14:58:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/\",\"url\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/\",\"name\":\"Db2 Trigger\",\"isPartOf\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png\",\"datePublished\":\"2019-06-05T09:54:33+00:00\",\"dateModified\":\"2020-04-11T14:58:47+00:00\",\"description\":\"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.db2tutorial.com\/db2-trigger\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage\",\"url\":\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png\",\"contentUrl\":\"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.db2tutorial.com\/db2-trigger\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.db2tutorial.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Db2 Trigger\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.db2tutorial.com\/#website\",\"url\":\"https:\/\/www.db2tutorial.com\/\",\"name\":\"DB2 Tutorial\",\"description\":\"A Comprehensive DB2 Tutorial\",\"publisher\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.db2tutorial.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.db2tutorial.com\/#organization\",\"name\":\"Db2 Tutorial\",\"url\":\"https:\/\/www.db2tutorial.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png\",\"contentUrl\":\"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png\",\"width\":500,\"height\":500,\"caption\":\"Db2 Tutorial\"},\"image\":{\"@id\":\"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Db2 Trigger","description":"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.","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:\/\/www.db2tutorial.com\/db2-trigger\/","og_locale":"en_US","og_type":"article","og_title":"Db2 Trigger","og_description":"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.","og_url":"https:\/\/www.db2tutorial.com\/db2-trigger\/","og_site_name":"DB2 Tutorial","article_modified_time":"2020-04-11T14:58:47+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/","url":"https:\/\/www.db2tutorial.com\/db2-trigger\/","name":"Db2 Trigger","isPartOf":{"@id":"https:\/\/www.db2tutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage"},"image":{"@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage"},"thumbnailUrl":"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png","datePublished":"2019-06-05T09:54:33+00:00","dateModified":"2020-04-11T14:58:47+00:00","description":"This section shows you how to create triggers which will be executed automatically when a delete, insert, or update operation occurs on the tables.","breadcrumb":{"@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.db2tutorial.com\/db2-trigger\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/#primaryimage","url":"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png","contentUrl":"https:\/\/www.db2tutorial.com\/wp-content\/uploads\/2019\/06\/Db2-Trigger.png","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.db2tutorial.com\/db2-trigger\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.db2tutorial.com\/"},{"@type":"ListItem","position":2,"name":"Db2 Trigger"}]},{"@type":"WebSite","@id":"https:\/\/www.db2tutorial.com\/#website","url":"https:\/\/www.db2tutorial.com\/","name":"DB2 Tutorial","description":"A Comprehensive DB2 Tutorial","publisher":{"@id":"https:\/\/www.db2tutorial.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.db2tutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.db2tutorial.com\/#organization","name":"Db2 Tutorial","url":"https:\/\/www.db2tutorial.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/","url":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png","contentUrl":"https:\/\/db2tutorial.com\/wp-content\/uploads\/2019\/03\/favicon.png","width":500,"height":500,"caption":"Db2 Tutorial"},"image":{"@id":"https:\/\/www.db2tutorial.com\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":1,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/631\/revisions"}],"predecessor-version":[{"id":1053,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/pages\/631\/revisions\/1053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/media\/642"}],"wp:attachment":[{"href":"https:\/\/www.db2tutorial.com\/wp-json\/wp\/v2\/media?parent=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}