Question

I want to write a trigger in SQLite.

Lets say I have 2 tables A and B

Computer

int id   
int foo1

Keyboard

int id  
int computerId (foreign key)  
int foo2

I want to write a trigger which updates Keyboard foo1 when Computer foo2 updated.

 CREATE TRIGGER update_trigger 
    AFTER UPDATE OF foo1 ON  Computer  FOR EACH ROW 
    BEGIN
      UPDATE Keyboard  
         SET foo2 = 'someValue' 
           WHERE computerId = ???? ;
     END;

How can i get updated Computer id for where command ? Can someone help me to complete where command ?

Was it helpful?

Solution

SQLITE has NEW and OLD references you can use to obtain current or previous values from the update. Given your requirement either would probably be usable.

 CREATE TRIGGER update_trigger 
    AFTER UPDATE OF foo1 ON  Computer  FOR EACH ROW 
    BEGIN
      UPDATE Keyboard  
         SET foo2 = 'someValue' 
           WHERE computerId = OLD.id ;
     END;

See http://www.sqlite.org/lang_createtrigger.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top