0

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:

  1. Is it possible to write SQLite trigger for multiple tables, at once (if so how)?
  2. Is it possible to write SQLite trigger for multiple actions (e.i. UPDATE, INSERT, DELETE), at once (if so how)?
  3. Maybe there is a better way to achieve what I am aiming for?
1
  • What I would like, is something like this: I think that the only option is to create your own SQLite...
    – Akina
    Dec 3 '20 at 12:30
1
  1. No, SQLite doesn't appear to have database or server level triggers (like other database systems). Instead you'd have to create the same trigger on each table.

  2. Unfortunately this is also a limitation in SQLite and not possible either.

  3. You might be better off letting the app that connects to the SQLite database (or a secondary process / job depending on if your database is stored on a mobile device vs a computer) control what records get deleted and when. For example, if you were hosting your SQLite database on a Windows machine, you could schedule a Task that routinely cleans up the database. Otherwise you can build it into the framework of your application that whenever a database call is made, the clean up script executes first.

Here is the syntax tree for the CREATE TRIGGER statement on SQLite. It may help clarify what you can and can't do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.