【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:
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';
参考: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:删除时没有新的数据 |
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';
相关文章推荐
- 【Sqlit数据库官方API学习—part01】9个基本函数
- 数据库的触发器trigger中如何执行socket操作?
- 数据库触发器(Trigger)的一点使用心得
- 数据库学习之触发器
- JDBC数据库的API对照实例学习
- Mysql Trigger触发器学习总结
- [数据库]一个利用触发器(trigger)实现数据库表的审计功能(audit)的例子--针对ms sql实现
- [Bada开发]API官方学习2-风格
- oracle 数据库 触发器 trigger 语法
- 触发器(trigger)学习
- 数据库学习之触发器
- MySQL存储过程和触发器的实现--数据库学习笔记
- 数据库学习四:触发器
- 数据库 触发器 trigger 语法
- 使用函数与触发器 【数据库高效编程 - 学习笔记 第十章】
- 数据库触发器(Trigger)的一点使用心得
- TC官方文档翻译08----内存树数据库API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- ORACLE PL/SQL 触发器(trigger)学习笔记
- 了解数据库视图建设的好处,和怎么建设;了解触发器trigger
- 怎样给Oracle 数据库的表加触发器: how to add trigger for Oracle Database table