Artifact Content
Not logged in

Artifact a602360d93d6d27a724bf9d7abc85a43e7c2ae75:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="sqlite.css" rel="stylesheet">
<title>SQLite Query Language: CREATE TRIGGER</title>
<!-- path= -->
<div class=nosearch>
<a href="index.html">
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
<div><!-- IE hack to prevent disappearing logo --></div>
<div class="tagline desktoponly">
Small. Fast. Reliable.<br>Choose any three.
<div class="menu mainmenu">
<li><a href="index.html">Home</a>
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
<li class='wideonly'><a href='about.html'>About</a>
<li class='desktoponly'><a href="docs.html">Documentation</a>
<li class='desktoponly'><a href="download.html">Download</a>
<li class='wideonly'><a href='copyright.html'>License</a>
<li class='desktoponly'><a href="support.html">Support</a>
<li class='desktoponly'><a href="prosupport.html">Purchase</a>
<li class='search' id='search_menubutton'>
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
<div class="menu submenu" id="submenu">
<li><a href='about.html'>About</a>
<li><a href='docs.html'>Documentation</a>
<li><a href='download.html'>Download</a>
<li><a href='support.html'>Support</a>
<li><a href='prosupport.html'>Purchase</a>
<div class="searchmenu" id="searchmenu">
<form method="GET" action="search">
<select name="s" id="searchtype">
<option value="d">Search Documentation</option>
<option value="c">Search Changelog</option>
<input type="text" name="q" id="searchbox" value="">
<input type="submit" value="Go">
function toggle_div(nm) {
var w = document.getElementById(nm);
if("block" ){ = "none";
}else{ = "block";
function toggle_search() {
var w = document.getElementById("searchmenu");
if("block" ){ = "none";
} else { = "block";
}, 30);
function div_off(nm){document.getElementById(nm).style.display="none";}
window.onbeforeunload = function(e){div_off("submenu");}
/* Disable the Search feature if we are not operating from CGI, since */
/* Search is accomplished using CGI and will not work without it. */
if( !location.origin.match || !location.origin.match(/http/) ){
document.getElementById("search_menubutton").style.display = "none";
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */
function hideorshow(btn,obj){
var x = document.getElementById(obj);
var b = document.getElementById(btn);
if(!='none' ){ = 'none';
}else{ = '';
return false;
<div class=nosearch><h1 align="center">SQL As Understood By SQLite</h1><p><a href="lang.html">[Top]</a></p><h2>CREATE TRIGGER</h2></div><p><b><a href="syntax/create-trigger-stmt.html">create-trigger-stmt:</a></b>
<button id='x1309' onclick='hideorshow("x1309","x1310")'>hide</button></p>
 <div id='x1310' class='imgcontainer'>
 <img alt="syntax diagram create-trigger-stmt" src="images/syntax/create-trigger-stmt.gif" />
<p><b><a href="syntax/delete-stmt.html">delete-stmt:</a></b>
<button id='x1311' onclick='hideorshow("x1311","x1312")'>show</button></p>
 <div id='x1312' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram delete-stmt" src="images/syntax/delete-stmt.gif" />
<p><b><a href="syntax/qualified-table-name.html">qualified-table-name:</a></b>
<button id='x1313' onclick='hideorshow("x1313","x1314")'>show</button></p>
 <div id='x1314' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram qualified-table-name" src="images/syntax/qualified-table-name.gif" />
<p><b><a href="syntax/with-clause.html">with-clause:</a></b>
<button id='x1315' onclick='hideorshow("x1315","x1316")'>show</button></p>
 <div id='x1316' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram with-clause" src="images/syntax/with-clause.gif" />
<p><b><a href="syntax/cte-table-name.html">cte-table-name:</a></b>
<button id='x1317' onclick='hideorshow("x1317","x1318")'>show</button></p>
 <div id='x1318' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram cte-table-name" src="images/syntax/cte-table-name.gif" />
<p><b><a href="syntax/expr.html">expr:</a></b>
<button id='x1319' onclick='hideorshow("x1319","x1320")'>show</button></p>
 <div id='x1320' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram expr" src="images/syntax/expr.gif" />
<p><b><a href="syntax/filter.html">filter:</a></b>
<button id='x1321' onclick='hideorshow("x1321","x1322")'>show</button></p>
 <div id='x1322' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram filter" src="images/syntax/filter.gif" />
<p><b><a href="syntax/literal-value.html">literal-value:</a></b>
<button id='x1323' onclick='hideorshow("x1323","x1324")'>show</button></p>
 <div id='x1324' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram literal-value" src="images/syntax/literal-value.gif" />
<p><b><a href="syntax/raise-function.html">raise-function:</a></b>
<button id='x1325' onclick='hideorshow("x1325","x1326")'>show</button></p>
 <div id='x1326' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif" />
<p><b><a href="syntax/type-name.html">type-name:</a></b>
<button id='x1327' onclick='hideorshow("x1327","x1328")'>show</button></p>
 <div id='x1328' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram type-name" src="images/syntax/type-name.gif" />
<p><b><a href="syntax/signed-number.html">signed-number:</a></b>
<button id='x1329' onclick='hideorshow("x1329","x1330")'>show</button></p>
 <div id='x1330' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram signed-number" src="images/syntax/signed-number.gif" />
<p><b><a href="syntax/window-defn.html">window-defn:</a></b>
<button id='x1331' onclick='hideorshow("x1331","x1332")'>show</button></p>
 <div id='x1332' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram window-defn" src="images/syntax/window-defn.gif" />
<p><b><a href="syntax/frame-spec.html">frame-spec:</a></b>
<button id='x1333' onclick='hideorshow("x1333","x1334")'>show</button></p>
 <div id='x1334' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram frame-spec" src="images/syntax/frame-spec.gif" />
<p><b><a href="syntax/ordering-term.html">ordering-term:</a></b>
<button id='x1335' onclick='hideorshow("x1335","x1336")'>show</button></p>
 <div id='x1336' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
<p><b><a href="syntax/insert-stmt.html">insert-stmt:</a></b>
<button id='x1337' onclick='hideorshow("x1337","x1338")'>show</button></p>
 <div id='x1338' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram insert-stmt" src="images/syntax/insert-stmt.gif" />
<p><b><a href="syntax/upsert-clause.html">upsert-clause:</a></b>
<button id='x1339' onclick='hideorshow("x1339","x1340")'>show</button></p>
 <div id='x1340' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram upsert-clause" src="images/syntax/upsert-clause.gif" />
<p><b><a href="syntax/column-name-list.html">column-name-list:</a></b>
<button id='x1341' onclick='hideorshow("x1341","x1342")'>show</button></p>
 <div id='x1342' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram column-name-list" src="images/syntax/column-name-list.gif" />
<p><b><a href="syntax/indexed-column.html">indexed-column:</a></b>
<button id='x1343' onclick='hideorshow("x1343","x1344")'>show</button></p>
 <div id='x1344' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram indexed-column" src="images/syntax/indexed-column.gif" />
<p><b><a href="syntax/with-clause.html">with-clause:</a></b>
<button id='x1345' onclick='hideorshow("x1345","x1346")'>show</button></p>
 <div id='x1346' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram with-clause" src="images/syntax/with-clause.gif" />
<p><b><a href="syntax/cte-table-name.html">cte-table-name:</a></b>
<button id='x1347' onclick='hideorshow("x1347","x1348")'>show</button></p>
 <div id='x1348' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram cte-table-name" src="images/syntax/cte-table-name.gif" />
<p><b><a href="syntax/select-stmt.html">select-stmt:</a></b>
<button id='x1349' onclick='hideorshow("x1349","x1350")'>show</button></p>
 <div id='x1350' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntax/common-table-expression.html">common-table-expression:</a></b>
<button id='x1351' onclick='hideorshow("x1351","x1352")'>show</button></p>
 <div id='x1352' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram common-table-expression" src="images/syntax/common-table-expression.gif" />
<p><b><a href="syntax/compound-operator.html">compound-operator:</a></b>
<button id='x1353' onclick='hideorshow("x1353","x1354")'>show</button></p>
 <div id='x1354' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
<p><b><a href="syntax/join-clause.html">join-clause:</a></b>
<button id='x1355' onclick='hideorshow("x1355","x1356")'>show</button></p>
 <div id='x1356' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntax/join-constraint.html">join-constraint:</a></b>
<button id='x1357' onclick='hideorshow("x1357","x1358")'>show</button></p>
 <div id='x1358' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
<p><b><a href="syntax/join-operator.html">join-operator:</a></b>
<button id='x1359' onclick='hideorshow("x1359","x1360")'>show</button></p>
 <div id='x1360' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
<p><b><a href="syntax/ordering-term.html">ordering-term:</a></b>
<button id='x1361' onclick='hideorshow("x1361","x1362")'>show</button></p>
 <div id='x1362' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
<p><b><a href="syntax/result-column.html">result-column:</a></b>
<button id='x1363' onclick='hideorshow("x1363","x1364")'>show</button></p>
 <div id='x1364' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
<p><b><a href="syntax/table-or-subquery.html">table-or-subquery:</a></b>
<button id='x1365' onclick='hideorshow("x1365","x1366")'>show</button></p>
 <div id='x1366' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
<p><b><a href="syntax/window-defn.html">window-defn:</a></b>
<button id='x1367' onclick='hideorshow("x1367","x1368")'>show</button></p>
 <div id='x1368' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram window-defn" src="images/syntax/window-defn.gif" />
<p><b><a href="syntax/frame-spec.html">frame-spec:</a></b>
<button id='x1369' onclick='hideorshow("x1369","x1370")'>show</button></p>
 <div id='x1370' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram frame-spec" src="images/syntax/frame-spec.gif" />
<p><b><a href="syntax/update-stmt.html">update-stmt:</a></b>
<button id='x1371' onclick='hideorshow("x1371","x1372")'>show</button></p>
 <div id='x1372' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram update-stmt" src="images/syntax/update-stmt.gif" />
<p><b><a href="syntax/column-name-list.html">column-name-list:</a></b>
<button id='x1373' onclick='hideorshow("x1373","x1374")'>show</button></p>
 <div id='x1374' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram column-name-list" src="images/syntax/column-name-list.gif" />
<p><b><a href="syntax/qualified-table-name.html">qualified-table-name:</a></b>
<button id='x1375' onclick='hideorshow("x1375","x1376")'>show</button></p>
 <div id='x1376' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram qualified-table-name" src="images/syntax/qualified-table-name.gif" />
<p><b><a href="syntax/with-clause.html">with-clause:</a></b>
<button id='x1377' onclick='hideorshow("x1377","x1378")'>show</button></p>
 <div id='x1378' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram with-clause" src="images/syntax/with-clause.gif" />
<p><b><a href="syntax/cte-table-name.html">cte-table-name:</a></b>
<button id='x1379' onclick='hideorshow("x1379","x1380")'>show</button></p>
 <div id='x1380' style='display:none;' class='imgcontainer'>
 <img alt="syntax diagram cte-table-name" src="images/syntax/cte-table-name.gif" />

<p>The CREATE TRIGGER statement is used to add triggers to the 
database schema. Triggers are database operations 
that are automatically performed when a specified database event
occurs.  </p>

<p>A trigger may be specified to fire whenever a <a href="lang_delete.html">DELETE</a>, <a href="lang_insert.html">INSERT</a>,
or <a href="lang_update.html">UPDATE</a> of a
particular database table occurs, or whenever an <a href="lang_update.html">UPDATE</a> occurs on
on one or more specified columns of a table.</p>

<p>At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH
STATEMENT triggers. Hence explicitly specifying FOR EACH ROW is optional.
FOR EACH ROW implies that the SQL statements specified in the trigger
may be executed (depending on the WHEN clause) for each database row being
inserted, updated or deleted by the statement causing the trigger to fire.</p>

<p>Both the WHEN clause and the trigger actions may access elements of 
the row being inserted, deleted or updated using references of the form 
"NEW.<i>column-name</i>" and "OLD.<i>column-name</i>", where
<i>column-name</i> is the name of a column from the table that the trigger
is associated with. OLD and NEW references may only be used in triggers on
events for which they are relevant, as follows:</p>

<table border=0 cellpadding=10>
<td valign="top" align="right" width=120><i>INSERT</i></td>
<td valign="top">NEW references are valid</td>
<td valign="top" align="right" width=120><i>UPDATE</i></td>
<td valign="top">NEW and OLD references are valid</td>
<td valign="top" align="right" width=120><i>DELETE</i></td>
<td valign="top">OLD references are valid</td>

<p>If a WHEN clause is supplied, the SQL statements specified
are only executed if the WHEN clause is true.
If no WHEN clause is supplied, the SQL statements
are executed every time the trigger fires.</p>

<p>The BEFORE or AFTER keyword determines when the trigger actions
will be executed relative to the insertion, modification or removal of the
associated row.</p>

<p>An <a href="lang_conflict.html">ON CONFLICT</a> clause may be specified as part of an <a href="lang_update.html">UPDATE</a> or <a href="lang_insert.html">INSERT</a>
action within the body of the trigger.
However if an <a href="lang_conflict.html">ON CONFLICT</a> clause is specified as part of 
the statement causing the trigger to fire, then conflict handling
policy of the outer statement is used instead.</p>

<p>Triggers are automatically <a href="lang_droptrigger.html">dropped</a>
when the table that they are 
associated with (the <i>table-name</i> table) is 
<a href="lang_droptable.html">dropped</a>.  However if the trigger actions reference
other tables, the trigger is not dropped or modified if those other
tables are <a href="lang_droptable.html">dropped</a> or <a href="lang_altertable.html">modified</a>.</p>

<p>Triggers are removed using the <a href="lang_droptrigger.html">DROP TRIGGER</a> statement.</p>

<h3>Syntax Restrictions On UPDATE, DELETE, and INSERT Statements Within

<p>The <a href="lang_update.html">UPDATE</a>, <a href="lang_delete.html">DELETE</a>, and <a href="lang_insert.html">INSERT</a>
statements within triggers do not support
the full syntax for <a href="lang_update.html">UPDATE</a>, <a href="lang_delete.html">DELETE</a>, and <a href="lang_insert.html">INSERT</a> statements.  The following
restrictions apply:</p>

  The name of the table to be modified in an <a href="lang_update.html">UPDATE</a>, <a href="lang_delete.html">DELETE</a>, or <a href="lang_insert.html">INSERT</a>
  statement must be an unqualified table name.  In other words, one must
  use just "<i>tablename</i>" not "<i>database</i><b>.</b><i>tablename</i>"
  when specifying the table. </p></li>

 For non-TEMP triggers,
  the table to be modified or queried must exist in the
  same database as the table or view to which the trigger is attached.
  TEMP triggers are not subject to the same-database rule.  A TEMP
  trigger is allowed to query or modify any table in any <a href="lang_attach.html">ATTACH</a>-ed database.

  The "INSERT INTO <i>table</i> DEFAULT VALUES" form of the <a href="lang_insert.html">INSERT</a> statement
  is not supported.

  The INDEXED BY and NOT INDEXED clauses are not supported for <a href="lang_update.html">UPDATE</a> and
  <a href="lang_delete.html">DELETE</a> statements.

  The ORDER BY and LIMIT clauses on <a href="lang_update.html">UPDATE</a> and <a href="lang_delete.html">DELETE</a> statements are not
  supported.  ORDER BY and LIMIT are not normally supported for <a href="lang_update.html">UPDATE</a> or
  <a href="lang_delete.html">DELETE</a> in any context but can be enabled for top-level statements
  using the <a href="compile.html#enable_update_delete_limit">SQLITE_ENABLE_UPDATE_DELETE_LIMIT</a> compile-time option.  However,
  that compile-time option only applies to top-level <a href="lang_update.html">UPDATE</a> and <a href="lang_delete.html">DELETE</a>
  statements, not <a href="lang_update.html">UPDATE</a> and <a href="lang_delete.html">DELETE</a> statements within triggers.

  <a href="syntax/common-table-expression.html">Common table expression</a> are not supported for
  statements inside of triggers.

<a name="instead_of_trigger"></a>

<h3>INSTEAD OF triggers</h3>

<p>Triggers may be created on <a href="lang_createview.html">views</a>, as well as ordinary tables, by
specifying INSTEAD OF in the CREATE TRIGGER statement. 
If one or more ON INSERT, ON DELETE
or ON UPDATE triggers are defined on a view, then it is not an
error to execute an INSERT, DELETE or UPDATE statement on the view, 
respectively.  Instead,
executing an INSERT, DELETE or UPDATE on the view causes the associated
triggers to fire. The real tables underlying the view are not modified
(except possibly explicitly, by a trigger program).</p>

<p>Note that the <a href="c3ref/changes.html">sqlite3_changes()</a> and <a href="c3ref/total_changes.html">sqlite3_total_changes()</a> interfaces
do not count INSTEAD OF trigger firings, but the
<a href="pragma.html#pragma_count_changes">count_changes pragma</a> does count INSTEAD OF trigger firing.</p>

<h3>Some Example Triggers</h3>

<p>Assuming that customer records are stored in the "customers" table, and
that order records are stored in the "orders" table, the following
UPDATE trigger
ensures that all associated orders are redirected when a customer changes
his or her address:</p>

CREATE TRIGGER update_customer_address UPDATE OF address ON customers 
    UPDATE orders SET address = new.address WHERE customer_name =;

<p>With this trigger installed, executing the statement:</p>

UPDATE customers SET address = '1 Main St.' WHERE name = 'Jack Jones';

<p>causes the following to be automatically executed:</p>

UPDATE orders SET address = '1 Main St.' WHERE customer_name = 'Jack Jones';

<p>For an example of an INSTEAD OF trigger, consider the following schema:

CREATE TABLE customer(
  cust_name TEXT,
  cust_addr TEXT
CREATE VIEW customer_address AS
   SELECT cust_id, cust_addr FROM customer;
CREATE TRIGGER cust_addr_chng
INSTEAD OF UPDATE OF cust_addr ON customer_address
  UPDATE customer SET cust_addr=NEW.cust_addr
   WHERE cust_id=NEW.cust_id;

<p>With the schema above, a statement of the form:</p>

UPDATE customer_address SET cust_addr=$new_address WHERE cust_id=$cust_id;

<p>Causes the customer.cust_addr field to be updated for a specific
customer entry that has customer.cust_id equal to the $cust_id parameter.
Note how the values assigned to the view are made available as field
in the special "NEW" table within the trigger body.</p>

<a name="undef_before"></a>

<h3>Cautions On The Use Of BEFORE triggers</h3>

<p>If a BEFORE UPDATE or BEFORE DELETE trigger modifies or deletes a row
that was to have been updated or deleted, then the result of the subsequent
update or delete operation is undefined.  Furthermore, if a BEFORE trigger
modifies or deletes a row, then it is undefined whether or not AFTER triggers
that would have otherwise run on those rows will in fact run.

<p>The value of NEW.rowid is undefined in a BEFORE INSERT trigger in which
the rowid is not explicitly set to an integer.</p>

<p>Because of the behaviors described above, programmers are encouraged to
prefer AFTER triggers over BEFORE triggers.</p>

<a name="raise"></a>

<h3>The RAISE() function</h3>

<p>A special SQL function RAISE() may be used within a trigger-program,
with the following syntax</p> 

<p><b><a href="syntax/raise-function.html">raise-function:</a></b></p><div class='imgcontainer'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif"></img>

<p>When one of RAISE(ROLLBACK,...), RAISE(ABORT,...) or RAISE(FAIL,...)
is called during trigger-program
execution, the specified <a href="lang_conflict.html">ON CONFLICT</a> processing is performed
the current query terminates.
An error code of <a href="rescode.html#constraint">SQLITE_CONSTRAINT</a> is returned to the application,
along with the specified error message.</p>

<p>When RAISE(IGNORE) is called, the remainder of the current trigger program,
the statement that caused the trigger program to execute and any subsequent
trigger programs that would have been executed are abandoned. No database
changes are rolled back.  If the statement that caused the trigger program
to execute is itself part of a trigger program, then that trigger program
resumes execution at the beginning of the next step.

<a name="temptrig"></a>

<h3>TEMP Triggers on Non-TEMP Tables</h3>

<p>A trigger normally exists in the same database as the table named
after the "ON" keyword in the CREATE TRIGGER statement.  Except, it is
possible to create a TEMP TRIGGER on a table in another database.  
Such a trigger will only fire when changes
are made to the target table by the application that defined the trigger.
Other applications that modify the database will not be able to see the
TEMP trigger and hence cannot run the trigger.</p>

<p>When defining a TEMP trigger on a non-TEMP table, it is important to
specify the database holding the non-TEMP table.  For example,
in the following statement, it is important to say "main.tab1" instead
of just "tab1":</p>


<p>Failure to specify the schema name on the target table could result
in the TEMP trigger being reattached to a table with the same name in
another database whenever any schema change occurs.</p>