SQLite 触发器AFTER DELETE
它指定了在删除数据后如何创建触发器。我们有两个表:COMPANY和AUDIT。
COMPANY表:
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
追踪表:
CREATE TABLE AUDIT(
EMP_ID INT NOT NULL,
ENTRY_DATE TEXT NOT NULL
);
创建触发器after delete:
使用以下语法在删除操作后在COMPANY表上创建名为“after_del”的触发器。
CREATE TRIGGER after_del AFTER DELETE
ON COMPANY
BEGIN
INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now'));
END;
现在从旧表中删除记录:
DELETE FROM COMPANY;
查看触发器:
SELECT name FROM sqlite_master
WHERE type = 'trigger';
输出: