C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
SQLite Trigger: AFTER UPDATEIt specifies how to create trigger after update the data. Suppose, we have two tables COMPANY and AUDIT, here we want to keep audit trial for every record being updated in COMPANY table. COMPANY table: CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); Create a new table named AUDIT where log messages will be inserted whenever there is an updation in COMPANY table. AUDIT table: CREATE TABLE AUDIT( EMP_ID INT NOT NULL, ENTRY_DATE TEXT NOT NULL ); CREATE trigger after update: Use the following syntax to create a trigger named "after_up" on COMPANY table after update operation. CREATE TRIGGER after_up AFTER UPDATE ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now')); END; Now update the old record as following: UPDATE COMPANY SET ADDRESS = 'Noida' WHERE ID = 1; See the result: See the trigger: SELECT name FROM sqlite_master WHERE type = 'trigger'; SQLite Trigger: BEFORE UPDATEIf you want to create the trigger before updating the data: CREATE TRIGGER befor_up BEFORE UPDATE ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now')); END; See the triggers: SELECT name FROM sqlite_master WHERE type = 'trigger';
Next TopicSQLite Trigger After Delete
|