SQLite classic ...login
SQLite classic tutorial
author:php.cn  update time:2022-04-13 17:05:02

SQLite trigger


SQLite's trigger is a callback function of the database, which will automatically execute/call when a specified database event occurs. Here are the key points about SQLite’s triggers: SQLite Trigger (Trigger) is the callback function of the database, which will be automatically executed/called when the specified database event occurs. The following are the key points about SQLite's triggers:

  • SQLite's triggers can be specified to trigger when DELETE, INSERT or UPDATE occurs in a specific database table, or when Fires when one or more columns of the specified table are updated.

  • SQLite only supports FOR EACH ROW triggers (Triggers), not FOR EACH STATEMENT triggers (Triggers). Therefore, explicitly specifying FOR EACH ROW is optional.

  • WHEN clauses and trigger actions may be accessed using the forms NEW.column-name and OLD.column-name References an inserted, deleted, or updated row element, 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 to execute the trigger action before or after the insertion, modification, or deletion of the associated row.

  • When the table associated with the trigger is deleted, the trigger (Trigger) is automatically deleted.

  • The table to be modified must exist in the same database as the table or view to which the trigger is attached, and only tablename must be used, not database.tablename.

  • A special SQL function RAISE() can be used to trigger exceptions thrown within the program.

Syntax

Create Trigger (Trigger)The basic syntax is as follows:

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

Here, event_name Can be INSERT, DELETE and UPDATE database operations on the mentioned table table_name. You can optionally specify FOR EACH ROW after the table name.

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

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

Example

Let us assume a situation where we want to maintain an audit test for every record inserted into the newly created COMPANY table (if it already exists, delete and recreate it):

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

To keep the audit experiment going, we will create a new table called AUDIT. Whenever there is a new record item in the COMPANY table, the 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 in the COMPANY table, which will result in the AUDIT table Create an audit log record in . 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 records in the Company table:

za -------- ---------- ---------- ---------- ----------
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 COMPANY table on the INSERT operation. Similarly, triggers can be created on UPDATE and DELETE operations as needed.

EMP_ID ENTRY_DATE
---------- ------------------------
1 2013-04 -05 06:26:00

List triggers (TRIGGERS)

You can list all triggers from the sqlite_master table as shown below :

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

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

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

Use AND if you want to list triggers on a specific table The clause connects the table name as follows:

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

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

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

Delete Triggers (TRIGGERS)

The following is the DROP command, which can be used to delete existing triggers:

sqlite> DROP TRIGGER trigger_name;