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

1、Trigger
create-trigger-stmt.gif
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 UPDATEDELETE, and INSERT statements within triggers do not support the full syntax for UPDATEDELETE, 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.// UPDATEDELETE, 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';


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值