views:

55

answers:

2

like:

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

Is it possible to do this in MySQL?

A: 

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

See: http://stackoverflow.com/questions/706231/mysql-and-check-constraints

The MYYN
Mysql doesn't support CHECK Constraint
Svetlozar Angelov
It will parse it and you will think it is legal, but it won't enfoce it
Svetlozar Angelov
+1  A: 

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
See e.g. http://forums.mysql.com/read.php?136,152474,240479#msg-240479
jensgram