Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Trigger

SQLite Trigger (Trigger)

SQLite **Trigger** is a callback function of the database that is automatically executed/invoked when a specified database event occurs. The following are key points about SQLite Trigger:

Syntax

creation Trigger The basic syntax is as follows:


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

Here,event_name can be on the mentioned table table_name on INSERT, DELETE, and UPDATE database operations. 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 an UPDATE operation:


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

Example

Let's assume a situation where we want to keep an audit trail for every record inserted into the newly created COMPANY table (dropped and recreated if it already exists):


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

To keep the audit trail, we will create a new table named AUDIT. Whenever a new record entry appears in 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. So, 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 cause an audit log record to be created in the AUDIT table. So, let's create a record in the COMPANY table as shown below:


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

At the same time, a record will be created in the AUDIT table. This record is the result of the trigger 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 download the jQuery EasyUI version you need from sqlite_master List all triggers in the table, as shown below:


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 the triggers on a particular table, use the AND clause to connect 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 is the DROP command that can be used to drop an existing trigger:


sqlite> DROP TRIGGER trigger_name;