Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger in sqlite different database

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!!

like image 780
xTMNTxRaphaelx Avatar asked Dec 05 '13 05:12

xTMNTxRaphaelx


1 Answers

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.

like image 165
Fabian Avatar answered Oct 19 '22 07:10

Fabian