SQLite Trigger AFTER DELETE

It specifies how to create trigger after delete the data. We have two tables COMPANY and AUDIT.

COMPANY table:

snippet
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

AUDIT table:

snippet
CREATE TABLE AUDIT(
    EMP_ID INT NOT NULL,
    ENTRY_DATE TEXT NOT NULL
);

CREATE trigger after delete:

Use the following syntax to create a trigger named "after_del" on COMPANY table after delete operation.

snippet
CREATE TRIGGER after_del  AFTER DELETE  
ON COMPANY
BEGIN
INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now'));
END;
Sqlite Trigger after delete 1

Now delete the record from the old table:

snippet
DELETE FROM COMPANY;
Sqlite Trigger after delete 2

See the trigger:

snippet
SELECT name FROM sqlite_master
WHERE type = 'trigger';

Output:

Sqlite Trigger after delete 3
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +