Let’s consider table
Video(
IDvideo(PK),
Date,
Description,
User
)
with mysql I have no way of writing assertions.
Is it possible to simulate the following assertion using one or more triggers ?
create assertion asser1
check (0 =
( select count(*)
from Video
where Date >= DATE_SUB(current_date(),INTERVAL 1 YEAR )
&& Date<=current_date()
group by User
having count(*) > 200
)
)
how should I write that trigger?
Well, the problem is that MySQL doesn’t have an equivalent of a
STOP ACTIONcommand. So basically, the work arounds are quite dirty:One way is that you can violate a constraint inside the trigger to bubble an error and cancel the insert:
Then, in the trigger, just try to:
The benefit of that, is that the error that’s returned will be a duplicate key error, and the text will include “Assert Failure”.
So then your trigger would become:
Now, you’d need to do this before
UPDATEas well, otherwise you could update the date into an invalid state. But otherwise, that should at least get you started…