I think you should do two things:
This question and its answers reflect this second thing, and here is an example:
delimiter $$ CREATE TRIGGER `cancel_insert_if_empty` BEFORE INSERT ON `your_table` FOR EACH ROW BEGIN declare msg varchar(255); if NEW.your_column is null or length(NEW.your_column) = 0 then set msg = "You're doing something wrong! Now suffer the consequences"; SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg; end if; END$$ delimiter ;
In this example, if you try to insert a null value or a zero-length string in your_column , the error will increase and the insert will be canceled. Quote from the reference guide :
MySQL handles errors during the start of a trigger as follows:
- If the
BEFORE trigger does not work, the operation on the corresponding line is not performed. - The
BEFORE trigger is activated by an attempt to insert or change a row, regardless of whether an attempt is subsequently performed. - An error during a
BEFORE or AFTER trigger causes the entire statement to fail, which triggers the trigger.
Of course, you can write a trigger to check for updates.
Hope this helps.
source share