Trigger in sqlite different database

拜拜、爱过 提交于 2020-01-01 03:36:06

问题


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.

Kindly suggest me a way!!


回答1:


This is not possible.

  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:

For A.sqlite:

create table T1(id integer primary key);

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;

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.



来源:https://stackoverflow.com/questions/20392219/trigger-in-sqlite-different-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!