Easy Tutorial
❮ Sqlite Data Types Sqlite Create Database ❯

SQLite Triggers

SQLite Triggers are database callback functions that are automatically executed or invoked when a specified database event occurs. Here are the key points about SQLite triggers:

Syntax

The basic syntax for creating a trigger is as follows:

CREATE TRIGGER trigger_name [BEFORE|AFTER] event_name 
ON table_name
BEGIN
 -- Trigger logic....
END;

Here, eventname can be *INSERT, DELETE, and UPDATE* database operations on the mentioned table tablename. You can optionally specify FOR EACH ROW after the table name.

The following is the syntax for creating a trigger on one or more specified columns of a table for the UPDATE operation:

CREATE TRIGGER trigger_name [BEFORE|AFTER] UPDATE OF column_name 
ON table_name
BEGIN
 -- Trigger logic....
END;

Example

Let's assume a scenario where we want to maintain an audit trail for every record inserted into a newly created COMPANY table (if it already exists, it will be dropped and recreated):

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

To maintain an audit trail, we will create a new table named AUDIT. Whenever a new entry is inserted into the COMPANY table, a log message will be inserted into it:

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

Here, ID is the ID of the AUDIT record, EMP_ID is the ID from the COMPANY table, and DATE will hold the timestamp when the record in COMPANY was created. Now, let's create a trigger on the COMPANY table as follows:

sqlite> CREATE TRIGGER audit_log AFTER INSERT 
ON COMPANY
BEGIN
   INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now'));
END;

Now, we will start inserting records into the COMPANY table, which will result in creating an audit log record in the AUDIT table. So, let's create a record in the COMPANY table as follows:

sqlite> INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 );

This will create the following record in the COMPANY table: ID NAME AGE ADDRESS SALARY


1 Paul 32 California 20000.0

A record will also be created in the AUDIT table. This record is the result of a trigger, which we created on the INSERT operation on the COMPANY table. Similarly, triggers can be created on UPDATE and DELETE operations as needed.

EMP_ID ENTRY_DATE


1 2013-04-05 06:26:00

Listing Triggers

You can list all triggers from the sqlite_master table as follows:

sqlite> SELECT name FROM sqlite_master
WHERE type = 'trigger';

The above SQLite statement will list only one entry, as follows:

name
----------
audit_log

If you want to list triggers on a specific table, use the AND clause with the table name, as shown below:

sqlite> SELECT name FROM sqlite_master
WHERE type = 'trigger' AND tbl_name = 'COMPANY';

The above SQLite statement will list only one entry, as follows:

name
----------
audit_log

Dropping Triggers

The following DROP command can be used to delete an existing trigger:

sqlite> DROP TRIGGER trigger_name;
❮ Sqlite Data Types Sqlite Create Database ❯