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 ?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top