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:
-
A SQLite Trigger can be specified to fire when a DELETE, INSERT, or UPDATE occurs on a particular database table, or when columns of one or more specified tables are updated.
-
SQLite only supports FOR EACH ROW Triggers; there are no FOR EACH STATEMENT Triggers. Therefore, explicitly specifying FOR EACH ROW is optional.
-
The WHEN clause and Trigger actions may access the inserted, deleted, or updated row elements using a reference of the form NEW.column-name and OLD.column-name where column-name is the name of a column from the table associated with the trigger.
-
If a WHEN clause is provided, the SQL statement is executed only for the specified rows for which the WHEN clause is true. If no WHEN clause is provided, the SQL statement is executed for all rows.
-
The BEFORE or AFTER keyword determines when the trigger action is executed, i.e., whether the trigger action is executed before or after the insert, modification, or deletion of the associated row.
-
A Trigger is automatically dropped when the table with which it is associated is dropped.
-
The table to be modified must exist in the same database as the table or view to which the trigger is attached, and must use only tablenamerather than database.tablename。
-
the bare table name. A special SQL function RAISE() can be used within a trigger program to raise an exception.
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;