{"id":824,"date":"2016-07-14T02:47:59","date_gmt":"2016-07-14T02:47:59","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=824"},"modified":"2025-02-08T01:28:02","modified_gmt":"2025-02-08T08:28:02","slug":"sql-triggers","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-triggers\/","title":{"rendered":"SQL Triggers"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you will learn about SQL triggers and how to create triggers that automatically invoke a piece of code in response to an event in the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-triggers'>Introduction to SQL Triggers <a href=\"#introduction-to-sql-triggers\" class=\"anchor\" id=\"introduction-to-sql-triggers\" title=\"Anchor for Introduction to SQL Triggers\">#<\/a><\/h2>\n\n\n\n<p>A trigger is a database object that executes a piece of code, a user-defined function, or a stored procedure in response to a specific event in a table.<\/p>\n\n\n\n<p>A trigger is always associated with a specific table. If the <a href=\"https:\/\/www.sqltutorial.org\/sql-drop-table\/\">table is deleted<\/a>, all the associated triggers are automatically deleted.<\/p>\n\n\n\n<p>A trigger is invoked either before or after the following event:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqltutorial.org\/sql-insert\/\">INSERT<\/a> \u2013 when a new row is inserted<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqltutorial.org\/sql-update\/\">UPDATE<\/a> \u2013 when an existing row is updated<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqltutorial.org\/sql-delete\/\">DELETE<\/a> &#8211; when a row is deleted.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqltutorial.org\/sql-math-functions\/sql-truncate\/\">TRUNCATE<\/a> &#8211; when the table is truncated. (PostgreSQL).<\/li>\n<\/ul>\n\n\n\n<p>When you execute an&nbsp;<code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code> statement against a table, the relational database management system (RDBMS) fires the corresponding trigger if it exists.<\/p>\n\n\n\n<p>In some RDBMS, a trigger is also invoked as the result of executing a statement that indirectly executes the <code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code> statement.<\/p>\n\n\n\n<p>For example, MySQL has the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/import-csv-file-mysql-table\/\" target=\"_blank\" rel=\"noreferrer noopener\">LOAD DATA INFILE<\/a>, which reads data from a text file and inserts it into a table at a very high speed, invoking the <code>BEFORE INSERT<\/code> and <code>AFTER INSERT<\/code> triggers.<\/p>\n\n\n\n<p>On the other hand, a statement may delete rows in a table but does not invoke the associated triggers.<\/p>\n\n\n\n<p>For example, the <a href=\"https:\/\/www.sqltutorial.org\/sql-truncate-table\/\">TRUNCATE TABLE<\/a> statement removes all rows in the table but does not invoke the <code>BEFORE DELETE<\/code> and <code>AFTER DELETE<\/code> triggers. In PostgreSQL, the <code>TRUNCATE TABLE<\/code> statement triggers a <code>TRUNCATE<\/code> trigger.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-triggers'>Creating triggers <a href=\"#creating-triggers\" class=\"anchor\" id=\"creating-triggers\" title=\"Anchor for Creating triggers\">#<\/a><\/h2>\n\n\n\n<p>To create a trigger, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> trigger_name \n&#91;<span class=\"hljs-keyword\">BEFORE<\/span>|<span class=\"hljs-keyword\">AFTER<\/span>] <span class=\"hljs-keyword\">event<\/span>\n<span class=\"hljs-keyword\">ON<\/span> table_name trigger_type\n<span class=\"hljs-keyword\">BEGIN<\/span>\n  <span class=\"hljs-comment\">-- trigger_logic<\/span>\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the name of the&nbsp;trigger after the <code>CREATE TRIGGER<\/code> clause.<\/li>\n\n\n\n<li>Next, use either <code>BEFORE<\/code> or <code>AFTER<\/code> keyword to determine when the trigger should respond to a specific event e.g., <code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code>.<\/li>\n\n\n\n<li>Then, provide the name of the table the trigger is associated.<\/li>\n\n\n\n<li>After, define the type of trigger using either <code>FOR EACH ROW<\/code> or <code>FOR EACH STATEMENT<\/code> clause.<\/li>\n\n\n\n<li>Finally, put the logic of the trigger in the <code>BEGIN ... END<\/code> block.<\/li>\n<\/ul>\n\n\n\n<p>Besides using the code in the <code>BEGIN...END<\/code> block, you can execute a user-defined function or a stored procedure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> trigger_name \n&#91;<span class=\"hljs-keyword\">BEFORE<\/span>|<span class=\"hljs-keyword\">AFTER<\/span>] <span class=\"hljs-keyword\">event<\/span>\n<span class=\"hljs-keyword\">ON<\/span> table_name \n   trigger_type\n<span class=\"hljs-keyword\">EXECUTE<\/span> stored_procedure_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='row-level-vs-statement-level-triggers'>Row-level vs. Statement-level triggers <a href=\"#row-level-vs-statement-level-triggers\" class=\"anchor\" id=\"row-level-vs-statement-level-triggers\" title=\"Anchor for Row-level vs. Statement-level triggers\">#<\/a><\/h2>\n\n\n\n<p>There are two types of triggers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Row-level triggers<\/li>\n\n\n\n<li>Statement-level triggers<\/li>\n<\/ul>\n\n\n\n<p>A row-level trigger executes each time a row is affected by a DML statement such as <code>INSERT<\/code>, <code>UPDATE<\/code>, and <code>DELETE<\/code>.<\/p>\n\n\n\n<p>If the statement affects 10 rows, the row-level trigger will execute 10 times. If the statement does not affect any row, the row-level trigger will not execute.<\/p>\n\n\n\n<p>Unlike a row-level trigger, a statement-level trigger is called <em><span style=\"text-decoration: underline;\"><strong>once<\/strong><\/span><\/em> regardless of how many affected rows.<\/p>\n\n\n\n<p>Note that if the SQL statement does not affect any rows, the statement-level trigger will execute.<\/p>\n\n\n\n<p>When creating a trigger, you can specify whether a trigger is a row-level or statement-level trigger using the <code>FOR EACH ROW<\/code> or <code>FOR EACH STATEMENT<\/code> clause espectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='why-use-sql-triggers'>Why Use SQL Triggers <a href=\"#why-use-sql-triggers\" class=\"anchor\" id=\"why-use-sql-triggers\" title=\"Anchor for Why Use SQL Triggers\">#<\/a><\/h2>\n\n\n\n<p>You typically use the triggers in the following scenarios:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Loggings<\/strong>. Some tables have sensitive data such as customer email, employee, and salary. You want to log all the changes. In this case, you can create the <code>UPDATE<\/code> trigger to record the changes in a separate table.<\/li>\n\n\n\n<li><strong>Enforce complex integrity of data<\/strong>. You may create triggers that validate data using complex logic that you cannot accomplish with simple constraints like <code>NOT NULL<\/code>, <code>UNIQUE<\/code>, and <code>CHECK<\/code> constraints.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-sql-triggers'>Creating SQL triggers <a href=\"#creating-sql-triggers\" class=\"anchor\" id=\"creating-sql-triggers\" title=\"Anchor for Creating SQL triggers\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the <code>employees<\/code> table in the <a href=\"https:\/\/www.sqltutorial.org\/sql-sample-database\/\">sample database<\/a> for the demonstration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"273\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" alt=\"employees_table\" class=\"wp-image-237\"\/><\/figure>\n\n\n\n<p>Suppose you want to log the changes of values in the <code>salary<\/code> column of the employees table. To do that, you <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">create a separate table<\/a> for storing the changes and use a trigger to insert the changes into this table.<\/p>\n\n\n\n<p>First, create the <code>salary_changes<\/code> table to store the salary changes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> salary_changes (\n    employee_id <span class=\"hljs-built_in\">INT<\/span>,\n    changed_at <span class=\"hljs-built_in\">TIMESTAMP<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-keyword\">CURRENT_TIMESTAMP<\/span>,\n    old_salary <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">8<\/span> , <span class=\"hljs-number\">2<\/span> ),\n    new_salary <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">8<\/span> , <span class=\"hljs-number\">2<\/span> ),\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (employee_id , changed_at)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIElGIE5PVCBFWElTVFMgc2FsYXJ5X2NoYW5nZXMgKCBlbXBsb3llZV9pZCBJTlQsIGNoYW5nZWRfYXQgVElNRVNUQU1QIERFRkFVTFQgQ1VSUkVOVF9USU1FU1RBTVAsIG9sZF9zYWxhcnkgREVDSU1BTCg4LCAyKSwgbmV3X3NhbGFyeSBERUNJTUFMKDgsIDIpLCBQUklNQVJZIEtFWSAoZW1wbG95ZWVfaWQsIGNoYW5nZWRfYXQpICk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>salary_changes<\/code> table logs the employee id, old salary, new salary and the time of changes.<\/p>\n\n\n\n<p>Note that the <code>change_at<\/code> column uses the current time as the default to log the time when the change occurs.<\/p>\n\n\n\n<p>Second, create a function that logs changes into the <code>salary_changes<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CREATE OR REPLACE FUNCTION log_salary_changes () \nRETURNS TRIGGER \nAS \n$$\nBEGIN\n    IF NEW.salary != OLD.salary THEN\n    \tINSERT INTO salary_changes(employee_id,old_salary,new_salary)\n        VALUES(NEW.employee_id,OLD.salary,NEW.salary);\n    END IF;\n    RETURN NEW;\nEND;\n$$ \nLANGUAGE plpgsql;<\/code><\/span><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIEZVTkNUSU9OIGxvZ19zYWxhcnlfY2hhbmdlcyAoKSBSRVRVUk5TIFRSSUdHRVIgQVMgJCQKQkVHSU4KICAgIElGIE5FVy5zYWxhcnkgIT0gT0xELnNhbGFyeSBUSEVOCiAgICAJSU5TRVJUIElOVE8gc2FsYXJ5X2NoYW5nZXMoZW1wbG95ZWVfaWQsb2xkX3NhbGFyeSxuZXdfc2FsYXJ5KQogICAgICAgIFZBTFVFUyhORVcuZW1wbG95ZWVfaWQsT0xELnNhbGFyeSxORVcuc2FsYXJ5KTsKICAgIEVORCBJRjsKRU5EOwokJCBMQU5HVUFHRSBwbHBnc3FsOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Note that within the trigger body, you can access the <code>OLD<\/code> and <code>NEW<\/code> variables to access the row before (<code>OLD<\/code>) and after update (<code>NEW<\/code>).<\/p>\n\n\n\n<p>Third, create a trigger called <code>before_update_salary<\/code> that execute the <code>log_salary_changes<\/code> function to log the salary changes to the <code>salary_changes<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> salary_changes\n<span class=\"hljs-keyword\">AFTER<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">OF<\/span> salary <span class=\"hljs-keyword\">ON<\/span> employees \n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">EACH<\/span> <span class=\"hljs-keyword\">ROW<\/span>\n<span class=\"hljs-keyword\">EXECUTE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> log_salary_changes ();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRSSUdHRVIgc2FsYXJ5X2NoYW5nZXMKQUZURVIgVVBEQVRFIE9GIHNhbGFyeSBPTiBlbXBsb3llZXMgCkZPUiBFQUNIIFJPVwpFWEVDVVRFIEZVTkNUSU9OIGxvZ19zYWxhcnlfY2hhbmdlcyAoKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Fourth, retrieve the current salary of the employee id<code>102<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  first_name,\n  last_name,\n  salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">WHERE<\/span>\n  employee_id = <span class=\"hljs-number\">110<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee_id | first_name | last_name | salary\n-------------+------------+-----------+---------\n         110 | John       | Chen      | 8200.00<\/code><\/span><\/pre>\n\n\n<p>Fourth, increment the salary of the employee <code>110<\/code> by <code>5%<\/code> using the following <code>UPDATE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> employees\n<span class=\"hljs-keyword\">SET<\/span>\n  salary = salary * <span class=\"hljs-number\">1.05<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span>\n  employee_id = <span class=\"hljs-number\">110<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, check the <code>salary_changes<\/code> table to see if the trigger has been invoked:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  employee_id,\n  old_salary,\n  new_salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  salary_changes;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> employee_id | old_salary | new_salary\n-------------+------------+------------\n         110 |    8200.00 |    8610.00<\/code><\/span><\/pre>\n\n\n<p>The output indicates that the <code>salary_changes<\/code> table has a new entry. It means that the trigger has been invoked correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='modify-sql-triggers'>Modify SQL Triggers <a href=\"#modify-sql-triggers\" class=\"anchor\" id=\"modify-sql-triggers\" title=\"Anchor for Modify SQL Triggers\">#<\/a><\/h2>\n\n\n\n<p>To change the trigger definition, you use the &nbsp;<code>CREATE OR REPLACE TRIGGER<\/code> statement.<\/p>\n\n\n\n<p>The <code>CREATE OR REPLACE TRIGGER<\/code> creates a new trigger if it does not exist and changes it if it does exist.<\/p>\n\n\n\n<p>The <code>CREATE OR REPLACE TRIGGER<\/code> statement is similar to the <code>CREATE TRIGGER<\/code> statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR<\/span> <span class=\"hljs-keyword\">REPLACE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> trigger_name \n&#91;<span class=\"hljs-keyword\">BEFORE<\/span>|<span class=\"hljs-keyword\">AFTER<\/span>] <span class=\"hljs-keyword\">event<\/span>\n<span class=\"hljs-keyword\">ON<\/span> table_name \n   trigger_type\n<span class=\"hljs-keyword\">BEGIN<\/span>\n  <span class=\"hljs-comment\">-- trigger_logic<\/span>\n<span class=\"hljs-keyword\">END<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='removing-sql-triggers'>Removing SQL triggers <a href=\"#removing-sql-triggers\" class=\"anchor\" id=\"removing-sql-triggers\" title=\"Anchor for Removing SQL triggers\">#<\/a><\/h2>\n\n\n\n<p>To drop a trigger from a database, you use the <code>DROP TRIGGER<\/code> statement with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> &#91;<span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>] trigger_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the trigger&#8217;s name (trigger_name) after the DROP TRIGGER keywords.<\/li>\n\n\n\n<li>Second, Use the <code>IF EXISTS<\/code> option to delete a trigger only if it exists. If the trigger does not exist, then the statement does nothing. However, if you don&#8217;t have the <code>IF EXISTS<\/code> option, the database system may issue an error if you try to drop a non-existing trigger.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following statement uses the <code>DROP TRIGGER<\/code> statement to remove the <code>salary_changes<\/code> trigger:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> salary_changes;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A trigger is a database object associated with a table that automatically executes a piece of code, a user-defined function, or a stored procedure in response to an event that occurs in the table.<\/li>\n\n\n\n<li>Use the <code>CREATE TRIGGER<\/code> statement to create a new trigger.<\/li>\n\n\n\n<li>Use the <code>CREATE OR REPLACE TRIGGER<\/code> statement to replace a trigger.<\/li>\n\n\n\n<li>Use the <code>DROP TRIGGER<\/code> statement to remove a trigger.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=trigger\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL Triggers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-triggers\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL Triggers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mariadbtutorial.com\/mariadb-triggers\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB Triggers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/plsql-tutorial\/oracle-trigger\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle Triggers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.db2tutorial.com\/db2-trigger\/\" target=\"_blank\" rel=\"noreferrer noopener\">Db2 Triggers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-trigger\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite Triggers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about SQL triggers and how to create triggers that automatically invoke a piece of code in response to an event occurs in the table.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":48,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-824","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Triggers<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.\" \/>\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.sqltutorial.org\/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=\"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-triggers\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T08:28:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\" \/>\n\t<meta property=\"og:image:width\" content=\"169\" \/>\n\t<meta property=\"og:image:height\" content=\"273\" \/>\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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/\",\"name\":\"SQL Triggers\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"datePublished\":\"2016-07-14T02:47:59+00:00\",\"dateModified\":\"2025-02-08T08:28:02+00:00\",\"description\":\"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-triggers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage\",\"url\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"contentUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png\",\"width\":169,\"height\":273,\"caption\":\"SQL Correlated Subquery - employees table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-triggers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Triggers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Triggers","description":"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.","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.sqltutorial.org\/sql-triggers\/","og_locale":"en_US","og_type":"article","og_title":"SQL Triggers","og_description":"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.","og_url":"https:\/\/www.sqltutorial.org\/sql-triggers\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T08:28:02+00:00","og_image":[{"width":169,"height":273,"url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/","url":"https:\/\/www.sqltutorial.org\/sql-triggers\/","name":"SQL Triggers","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage"},"image":{"@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","datePublished":"2016-07-14T02:47:59+00:00","dateModified":"2025-02-08T08:28:02+00:00","description":"In this tutorial, you will learn about the SQL triggers concept and how to develop simple triggers in the database system.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-triggers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/#primaryimage","url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","contentUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/employees.png","width":169,"height":273,"caption":"SQL Correlated Subquery - employees table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-triggers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL Triggers"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/824\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}