Question

Quick few questions on MYSQL syntax, it seems extremely hard to find these answers on google. Is it possible to set a constraint or trigger so that when a value of a column reaches a certain point it get's reset and something else is done. Or also if say hourLog is greater than zero, then licence status changes from Default value of INVALID to Valid.

I know all this could be done with a program that was using the database, but was just curious if there were MYSQL commands to do this automatically with a trigger or a Constraint.

Sorry if this was posted, I just was looking for quick clarification for a class.

Was it helpful?

Solution

You could use these two triggers, one for the UPDATE, one for the INSERT:

CREATE TRIGGER upd_yourtable BEFORE UPDATE ON yourtable 
FOR EACH ROW
  SET new.licenseStatus=
      CASE WHEN new.hourLog>0 THEN 'Valid' ELSE 'INVALID' END
;

CREATE TRIGGER ins_yourtable BEFORE INSERT ON yourtable 
FOR EACH ROW
  SET new.licenseStatus=
      CASE WHEN new.hourLog>0 THEN 'Valid' ELSE 'INVALID' END
;

Please see fiddle here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top