Small. Fast. Reliable.
Choose any three.

SQLite Requirement Matrix Details
lang_createtrigger.html

Index Summary Markup Original


R-10346-40046-35521-21776-43630-65428-20399-28702 tcl slt th3 src

The CREATE TRIGGER statement is used to add triggers to the database schema.

slt/slt_lang_createtrigger.test:21

/* IMP: R-10346-40046 */
# EVIDENCE-OF: R-10346-40046 The CREATE TRIGGER statement is used to add
# triggers to the database schema.

R-49475-10767-10056-16287-22604-26853-09642-54500 tcl slt th3 src

Triggers are database operations that are automatically performed when a specified database event occurs.

/* IMP: R-49475-10767 */
# EVIDENCE-OF: R-49475-10767 Triggers are database operations that are
# automatically performed when a specified database event occurs.

R-31322-10196-14986-33891-01213-47655-00762-39748 tcl slt th3 src

Each trigger must specify that it will fire for one of the following operations: DELETE, INSERT, UPDATE.

/* IMP: R-31322-10196 */
# EVIDENCE-OF: R-31322-10196 Each trigger must specify that it will fire
# for one of the following operations: DELETE, INSERT, UPDATE.

R-52227-24890-51100-17044-40956-25330-61806-31356 tcl slt th3 src

At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH STATEMENT triggers.

/* IMP: R-52227-24890 */
# EVIDENCE-OF: R-52227-24890 At this time SQLite supports only FOR EACH
# ROW triggers, not FOR EACH STATEMENT triggers.

R-38336-05023-13605-39367-03302-30111-01531-40111 tcl slt th3 src

Hence explicitly specifying FOR EACH ROW is optional.

/* IMP: R-38336-05023 */
# EVIDENCE-OF: R-38336-05023 Hence explicitly specifying FOR EACH ROW is
# optional.

R-32235-53300-57293-28888-55345-58030-49173-41178 tcl slt th3 src

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.

/* IMP: R-32235-53300 */
# EVIDENCE-OF: R-32235-53300 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.

R-25950-00887-26391-00141-61376-60148-41906-53274 tcl slt th3 src

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.column-name" and "OLD.column-name", where column-name is the name of a column from the table that the trigger is associated with.

/* IMP: R-25950-00887 */
# EVIDENCE-OF: R-25950-00887 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.column-name" and
# "OLD.column-name", where column-name is the name of a column from the
# table that the trigger is associated with.

R-63660-13730-40361-36600-49819-61310-05389-38402 tcl slt th3 src

OLD and NEW references may only be used in triggers on events for which they are relevant, as follows:

INSERT NEW references are valid
UPDATE NEW and OLD references are valid
DELETE OLD references are valid

slt/slt_lang_createtrigger.test:65

/* IMP: R-63660-13730 */
# EVIDENCE-OF: R-63660-13730 OLD and NEW references may only be used in
# triggers on events for which they are relevant, as follows: INSERT NEW
# references are valid UPDATE NEW and OLD references are valid DELETE
# OLD references are valid

R-45175-37688-08419-23660-35132-45148-11733-21680 tcl slt th3 src

If a WHEN clause is supplied, the SQL statements specified are only executed if the WHEN clause is true.

slt/slt_lang_createtrigger.test:70

/* IMP: R-45175-37688 */
# EVIDENCE-OF: R-45175-37688 If a WHEN clause is supplied, the SQL
# statements specified are only executed if the WHEN clause is true.

R-12597-09253-01914-52695-40165-57043-34342-03657 tcl slt th3 src

If no WHEN clause is supplied, the SQL statements are executed every time the trigger fires.

slt/slt_lang_createtrigger.test:73

/* IMP: R-12597-09253 */
# EVIDENCE-OF: R-12597-09253 If no WHEN clause is supplied, the SQL
# statements are executed every time the trigger fires.

R-35362-38850-39624-46125-42742-24667-28362-39621 tcl slt th3 src

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

slt/slt_lang_createtrigger.test:76

/* IMP: R-35362-38850 */
# EVIDENCE-OF: R-35362-38850 The BEFORE or AFTER keyword determines when
# the trigger actions will be executed relative to the insertion,
# modification or removal of the associated row.

R-65489-09702-43837-30882-04138-20719-06311-14611 tcl slt th3 src

BEFORE is the default when neither keyword is present.

/* IMP: R-65489-09702 */
# EVIDENCE-OF: R-65489-09702 BEFORE is the default when neither keyword
# is present.

R-57724-61571-01377-08004-10226-53040-57398-43203 tcl slt th3 src

An ON CONFLICT clause may be specified as part of an UPDATE or INSERT action within the body of the trigger.

/* IMP: R-57724-61571 */
# EVIDENCE-OF: R-57724-61571 An ON CONFLICT clause may be specified as
# part of an UPDATE or INSERT action within the body of the trigger.

R-35856-58769-58714-05667-00872-44195-43278-26599 tcl slt th3 src

However if an ON CONFLICT clause is specified as part of the statement causing the trigger to fire, then conflict handling policy of the outer statement is used instead.

/* IMP: R-35856-58769 */
# EVIDENCE-OF: R-35856-58769 However if an ON CONFLICT clause is
# specified as part of the statement causing the trigger to fire, then
# conflict handling policy of the outer statement is used instead.

R-32333-58476-07670-32465-34848-10846-33271-03352 tcl slt th3 src

Triggers are automatically dropped when the table that they are associated with (the table-name table) is dropped.

/* IMP: R-32333-58476 */
# EVIDENCE-OF: R-32333-58476 Triggers are automatically dropped when the
# table that they are associated with (the table-name table) is dropped.

R-45164-23268-36240-53561-39664-08143-25092-01923 tcl slt th3 src

However if the trigger actions reference other tables, the trigger is not dropped or modified if those other tables are dropped or modified.

/* IMP: R-45164-23268 */
# EVIDENCE-OF: R-45164-23268 However if the trigger actions reference
# other tables, the trigger is not dropped or modified if those other
# tables are dropped or modified.

R-31067-37494-56120-35098-11105-54623-54147-27483 tcl slt th3 src

Triggers are removed using the DROP TRIGGER statement.

/* IMP: R-31067-37494 */
# EVIDENCE-OF: R-31067-37494 Triggers are removed using the DROP TRIGGER
# statement.

R-46291-22228-33337-03438-09062-41936-26465-59313 tcl slt th3 src

The UPDATE, DELETE, and INSERT statements within triggers do not support the full syntax for UPDATE, DELETE, and INSERT statements.

/* IMP: R-46291-22228 */
# EVIDENCE-OF: R-46291-22228 The UPDATE, DELETE, and INSERT statements
# within triggers do not support the full syntax for UPDATE, DELETE, and
# INSERT statements.

R-42881-44982-56235-44486-35660-20338-31764-62780 tcl slt th3 src

The name of the table to be modified in an UPDATE, DELETE, or INSERT statement must be an unqualified table name. In other words, one must use just "tablename" not "database.tablename" when specifying the table.

/* IMP: R-42881-44982 */
# EVIDENCE-OF: R-42881-44982 The name of the table to be modified in an
# UPDATE, DELETE, or INSERT statement must be an unqualified table name.
# In other words, one must use just "tablename" not "database.tablename"
# when specifying the table.

R-33443-64794-37777-63092-51102-53301-52354-10216 tcl slt th3 src

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.

/* IMP: R-33443-64794 */
# EVIDENCE-OF: R-33443-64794 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.

R-57051-20322-05054-14581-57313-02803-59746-15019 tcl slt th3 src

TEMP triggers are not subject to the same-database rule. A TEMP trigger is allowed to query or modify any table in any ATTACH-ed database.

/* IMP: R-57051-20322 */
# EVIDENCE-OF: R-57051-20322 TEMP triggers are not subject to the
# same-database rule. A TEMP trigger is allowed to query or modify any
# table in any ATTACH-ed database.

R-21148-64834-51953-28586-58289-58298-04009-03354 tcl slt th3 src

The "INSERT INTO table DEFAULT VALUES" form of the INSERT statement is not supported.

/* IMP: R-21148-64834 */
# EVIDENCE-OF: R-21148-64834 The "INSERT INTO table DEFAULT VALUES" form
# of the INSERT statement is not supported.

R-34918-27009-22149-14301-56506-15307-06517-16770 tcl slt th3 src

The INDEXED BY and NOT INDEXED clauses are not supported for UPDATE and DELETE statements.

/* IMP: R-34918-27009 */
# EVIDENCE-OF: R-34918-27009 The INDEXED BY and NOT INDEXED clauses are
# not supported for UPDATE and DELETE statements.

R-43310-35438-04399-12485-38547-48138-39815-28430 tcl slt th3 src

The ORDER BY and LIMIT clauses on UPDATE and DELETE statements are not supported. ORDER BY and LIMIT are not normally supported for UPDATE or DELETE in any context but can be enabled for top-level statements using the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option. However, that compile-time option only applies to top-level UPDATE and DELETE statements, not UPDATE and DELETE statements within triggers.

/* IMP: R-43310-35438 */
# EVIDENCE-OF: R-43310-35438 The ORDER BY and LIMIT clauses on UPDATE
# and DELETE statements are not supported. ORDER BY and LIMIT are not
# normally supported for UPDATE or DELETE in any context but can be
# enabled for top-level statements using the
# SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option. However, that
# compile-time option only applies to top-level UPDATE and DELETE
# statements, not UPDATE and DELETE statements within triggers.

R-02944-19877-16321-40331-61553-58376-42533-63822 tcl slt th3 src

Common table expression are not supported for statements inside of triggers.

/* IMP: R-02944-19877 */
# EVIDENCE-OF: R-02944-19877 Common table expression are not supported
# for statements inside of triggers.

R-01709-17419-52618-05160-43640-08131-58818-16777 tcl slt th3 src

BEFORE and AFTER triggers work only on ordinary tables.

/* IMP: R-01709-17419 */
# EVIDENCE-OF: R-01709-17419 BEFORE and AFTER triggers work only on
# ordinary tables.

R-36849-55679-50276-26926-30909-09054-60506-32367 tcl slt th3 src

INSTEAD OF triggers work only on views.

/* IMP: R-36849-55679 */
# EVIDENCE-OF: R-36849-55679 INSTEAD OF triggers work only on views.

R-60277-47120-38313-48039-28824-40511-28187-23184 tcl slt th3 src

If an INSTEAD OF INSERT trigger exists on a view, then it is possible to execute an INSERT statement against that view. No actual insert occurs. Instead, the statements contained within the trigger are run.

/* IMP: R-60277-47120 */
# EVIDENCE-OF: R-60277-47120 If an INSTEAD OF INSERT trigger exists on a
# view, then it is possible to execute an INSERT statement against that
# view. No actual insert occurs. Instead, the statements contained
# within the trigger are run.

R-58080-31767-64590-28365-48177-49869-42154-18790 tcl slt th3 src

Note that the sqlite3_changes() and sqlite3_total_changes() interfaces do not count INSTEAD OF trigger firings, but the count_changes pragma does count INSTEAD OF trigger firing.

/* IMP: R-58080-31767 */
# EVIDENCE-OF: R-58080-31767 Note that the sqlite3_changes() and
# sqlite3_total_changes() interfaces do not count INSTEAD OF trigger
# firings, but the count_changes pragma does count INSTEAD OF trigger
# firing.

R-16646-21584-46697-05681-31663-32907-36469-00200 tcl slt th3 src

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:

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

With this trigger installed, executing the statement:

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

causes the following to be automatically executed:

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

/* IMP: R-16646-21584 */
# EVIDENCE-OF: R-16646-21584 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: CREATE TRIGGER update_customer_address UPDATE OF address ON
# customers BEGIN UPDATE orders SET address = new.address WHERE
# customer_name = old.name; END; With this trigger installed, executing
# the statement: UPDATE customers SET address = '1 Main St.' WHERE name
# = 'Jack Jones'; causes the following to be automatically executed:
# UPDATE orders SET address = '1 Main St.' WHERE customer_name = 'Jack
# Jones';

R-53099-14426-29346-17813-35365-40265-31413-38800 tcl slt th3 src

A special SQL function RAISE() may be used within a trigger-program,

/* IMP: R-53099-14426 */
# EVIDENCE-OF: R-53099-14426 A special SQL function RAISE() may be used
# within a trigger-program,

R-62308-64553-46829-46708-05864-23145-57136-36663 tcl slt th3 src

When one of RAISE(ROLLBACK,...), RAISE(ABORT,...) or RAISE(FAIL,...) is called during trigger-program execution, the specified ON CONFLICT processing is performed and the current query terminates.

/* IMP: R-62308-64553 */
# EVIDENCE-OF: R-62308-64553 When one of RAISE(ROLLBACK,...),
# RAISE(ABORT,...) or RAISE(FAIL,...) is called during trigger-program
# execution, the specified ON CONFLICT processing is performed and the
# current query terminates.

R-10546-57588-48216-07313-35751-26470-62407-13922 tcl slt th3 src

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.

/* IMP: R-10546-57588 */
# EVIDENCE-OF: R-10546-57588 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.

R-64082-04685-34430-02984-31872-50387-26964-32862 tcl slt th3 src

No database changes are rolled back.

/* IMP: R-64082-04685 */
# EVIDENCE-OF: R-64082-04685 No database changes are rolled back.

R-01402-03601-05785-14929-05185-63336-09442-11982 tcl slt th3 src

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.

/* IMP: R-01402-03601 */
# EVIDENCE-OF: R-01402-03601 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.

R-56934-42700-38980-08605-33652-52881-52643-23900 tcl slt th3 src

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.

/* IMP: R-56934-42700 */
# EVIDENCE-OF: R-56934-42700 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.