SQLite Forum

Unique every N seconds. How?
Login
Actually, I (OP) meant that a new entry should not be accepted unless it is at least 10 (N) seconds later than the previous one.  Not exactly N, at least N.
So, it could be 10 hours later but not less than N seconds.

I quickly realized it is impossible to do this with a simple `UNIQUE` constraint.

It would require `SELECT`ing the max dt for the given `COL1` and `COL2` combination, adding N seconds and comparing current dt value to be higher than that.

If triggers can do arbitrary `SELECT`s outside of the current row, maybe something like this would work.  I haven't tried yet but it is syntactically accepted by SQLite3.

```
CREATE TRIGGER log_trig BEFORE INSERT ON LOG
BEGIN
  SELECT IIF(new.dt >= DATETIME((SELECT max(dt) FROM LOG a
                                   WHERE a.col1 = new.col1
                                     AND a.col2 = new.col2),
                                '+10 seconds'),
             new.dt,
             RAISE(ABORT,'Log too soon'));
END;
```