SQLite和递归触发器

时间:2015-06-11 12:25:24

标签: sqlite recursion triggers

我想准确理解什么是递归触发器。我当然知道递归是什么,但是,在sqlite的情况下,是递归触发那些调用自身?,或者调用自己但只在同一行?在那个定义中包括间接递归的情况?只是在同一行或不是?

2 个答案:

答案 0 :(得分:4)

递归触发器是指在同一行上直接或间接触发相同触发器的触发器。

pragma命令recursive_triggers控制是否启用递归触发器。默认情况下它们不是(版本3.8.2)。

示例:

CREATE TABLE example (a INTEGER);

CREATE TRIGGER example_1
AFTER UPDATE ON example WHEN NEW.a = 5 OR NEW.a = 6
BEGIN UPDATE example SET a = NEW.a + 1; END;

INSERT INTO example SELECT 1;
UPDATE example SET a = 5;
SELECT * FROM example; -- a = 6. Direct recursion forbidden.

PRAGMA recursive_triggers = ON;

UPDATE example SET a = 5;
SELECT * FROM example; -- a = 7. Direct recursion enabled.

DROP TRIGGER example_1;

PRAGMA recursive_triggers = OFF;

CREATE TRIGGER example_1
AFTER UPDATE ON example WHEN NEW.a = 5 OR NEW.a = 7
BEGIN UPDATE example SET a = NEW.a + 1; END;

CREATE TRIGGER example_2
AFTER UPDATE ON example WHEN NEW.a = 6
BEGIN UPDATE example SET a = NEW.a + 1; END;

UPDATE example SET a = 5;
SELECT * FROM example; -- a = 7. Indirect recursion forbidden.

PRAGMA recursive_triggers = ON;

UPDATE example SET a = 5;
SELECT * FROM example; -- a = 8. Indirect recursion enabled.

答案 1 :(得分:0)

递归触发器是一个触发器,其执行会导致任何触发器运行(本身或任何其他触发器)。