0

like:

create table test(
    score integer unsigned between 1 and 100
...
);

Is it possible to do this in MySQL?

Mask
  • 31,463
  • 47
  • 100
  • 125

2 Answers2

1

Mysql doesn't support CHECK constraints

You can create BEFORE UPDATE/INSERT TRIGGER with RAISE ERROR

EDIT: Raise Error with

SET new.score = 1 / 0;

Something like that:

CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW BEGIN
    IF  NEW.score < 1 OR NEW.score > 100
              SET NEW.score = 1 / 0; 
  END;
Svetlozar Angelov
  • 20,444
  • 6
  • 61
  • 67
0

In short: MySQL does not support CHECK constraints, although for compatibility with other engines it will parse them (but ignores them).

See: MySQL and Check Constraints

Community
  • 1
  • 1
miku
  • 172,072
  • 46
  • 300
  • 307