您的位置:首页 > 数据库

【Sqlit数据库官方API学习—part02】触发器(Trigger)

2014-08-12 10:25 316 查看
1、Trigger
参考:http://www.sqlite.org/lang_createtrigger.html



1)总述

At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH STATEMENT triggers. Hence explicitly specifying FOR EACH ROW is optional.

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. OLD and NEW references may only be used in triggers on events for which they are relevant, as follows:

INSERT
NEW references are valid:插入时没有old数据

UPDATE
NEW and OLD references are valid:更新时有新的数据,也有旧的数据

DELETE
OLD references are valid:删除时没有新的数据

Triggers are automatically dropped when the table that they are associated with (the table-name table) is dropped.
However if the trigger actions reference other tables, the trigger is not dropped or modified if those other tables are dropped or modified.

Triggers are removed using the DROP TRIGGER statement.

2)语法限制 On UPDATE, DELETE, and INSERT Statements Within Triggers(4条)

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

· 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. The table to be modified must exist in the same database as the table or view to which the trigger is attached.// UPDATE, DELETE,
or INSERT语句中的表名必须是an unqualified table name(非完全限定的表名,即只有表名,而不用"database.tablename")

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

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

· 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.

3) INSTEAD OF trigger
Triggers may be created on views, 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).

在View上可以创建Trigger,如同一般的表一样。多个INSERT, ON DELETE or ON UPDATE triggers定义在view上可以执行。但真实的表没有被修改。
4) Cautions On The Use Of BEFORE triggers
programmers are encouraged to prefer AFTER triggers over BEFORE triggers.最好不用BEFORE triggers

5)TEMP Triggers on Non-TEMP Tables

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.
只有定义这个trigger的应用修改这个表才能触发TEMP TRIGGER,其他应用修改这个表,这个表上的trigger不会被触发
6)The RAISE() function
一些错误提示的用法。
7)例子

Assuming that customer records are stored in the "customers" table, and that order records are stored in the "orders" table, the following 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';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: