I am looking for a way to write SQLite trigger which will affect multiple actions and multiple tables (in fact I want it executed on a particular table each time any query is executed on any table in the database).
Here is an example, to explain what I mean.
I have 3 tables: Students
, Courses
, Professors
.
Each student has activation_deadline
field, which is set to NULL
upon activation.
I want each time any query is executed, to check if there are students with expired activation_deadline
and if there are -- remove them.
Currently I have three separate triggers on Students
table for this.
One for UPDATE
:
CREATE TRIGGER Remove_Unactivated_Students_Update
AFTER UPDATE
ON Students
FOR EACH ROW
BEGIN
DELETE FROM Students
WHERE (activation_deadline IS NOT NULL) AND (activation_deadline <= strftime('%s', 'now'));
END;
One for INSERT
:
CREATE TRIGGER Remove_Unactivated_Students_Insert
BEFORE INSERT
ON Students
FOR EACH ROW
BEGIN
DELETE FROM Students
WHERE (activation_deadline IS NOT NULL) AND (activation_deadline <= strftime('%s', 'now'));
END;
And one for DELETE
:
CREATE TRIGGER Remove_Unactivated_Students_Delete
AFTER DELETE
ON Students
FOR EACH ROW
BEGIN
DELETE FROM Students
WHERE (activation_deadline IS NOT NULL) AND (activation_deadline <= strftime('%s', 'now'));
END;
What I would like, is something like this:
CREATE TRIGGER Remove_Unactivated_Students
AFTER INSERT, UPDATE, DELETE
ON Students, Courses, Professors
FOR EACH ROW
BEGIN
DELETE FROM Students
WHERE (activation_deadline IS NOT NULL) AND (activation_deadline <= strftime('%s', 'now'));
END;
Which, of course, gives me:
Error: near line 1: near ",": syntax error
Given what is said above, here are the questions:
- Is it possible to write SQLite trigger for multiple tables, at once (if so how)?
- Is it possible to write SQLite trigger for multiple actions (e.i.
UPDATE
,INSERT
,DELETE
), at once (if so how)? - Maybe there is a better way to achieve what I am aiming for?