在sqlite不同数据库中触发 [英] Trigger in sqlite different database

查看:89
本文介绍了在sqlite不同数据库中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数据库'A'和'B'。我需要创建一个触发器,当我在数据库'A'的表'T1'中插入任何条目时,然后在数据库'B的表'T2'中插入条目

I have 2 different database 'A' and 'B'.I need to create a trigger that when I would insert any entry in table 'T1' of database 'A' then entries of table 'T2' of database 'B' would gets deleted.

请给我一种方法!

推荐答案

这是不可能的。


  1. 在SQLite中,触发器内部的DML只能修改同一数据库的表(请参阅此处)。您不能修改附加数据库的表。

  2. 类似地,除非声明它们为TEMPORARY,否则您不能为附加数据库声明触发器(反之亦然)。

  1. In SQLite, DML inside triggers can only modify tables of the same database (see here). You cannot modify tables of an attached database.
  2. Similarly, you cannot declare triggers for an attached database (to do it the other way) unless you declare them TEMPORARY.

因此,(仅)可能出现以下情况:

Hence, (only) the following is possible:

对于A.sqlite:

For A.sqlite:

create table T1(id integer primary key);

对于B.sqlite:

For B.sqlite:

create table T2(id integer primary key);
attach 'A.sqlite' as A;
create temporary trigger T1_del after delete on A.T1 
begin
    delete from T2 where id = OLD.id;
end;

但这只会在声明临时触发器的连接内将删除从T1传播到T2。如果单独打开A.sqlite,触发器将不会在那里。

But that would only propagate deletes from T1 to T2 within the connection that declared the temporary trigger. If you opened A.sqlite separately, the trigger would not be there.

这篇关于在sqlite不同数据库中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆