Answer the question
In order to leave comments, you need to log in
How to limit the maximum number in a MYSQL field?
How to limit the maximum number in a MYSQL field?
For example, there is an activity field where values like activity = activity + 0.5 are written activity = activity + 1 and so on
, but you need to limit it so that the number in the activity field cannot exceed eight.
Answer the question
In order to leave comments, you need to log in
Restrict it on the application side, this is the application logic. The database stores the data and their relationships with each other, but does not validate their types.
Well, if you really need it, just set the enum type and list all possible options
there. There is another option:
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF New.activity>8 THEN
SIGNAL SQLSTATE '10000'
SET MESSAGE_TEXT = 'check constraint on table failed during insert';
END IF;
END;
CREATE TRIGGER trigger_upd_name BEFORE UPDATE ON table
FOR EACH ROW
BEGIN
IF New.activity>8 THEN
SIGNAL SQLSTATE '10000'
SET MESSAGE_TEXT = 'check constraint on table failed during update';
END IF;
END;
https://stackoverflow.com/a/34883779
but in general the idea is some kind of crazy, why do this database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question