简体   繁体   English

如何约束MySQL的列?

[英]How to constraint column of MySQL?

like: 喜欢:

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

Is it possible to do this in MySQL? 在MySQL中可以做到这一点吗?

Mysql doesn't support CHECK constraints MySQL不支持CHECK约束

You can create BEFORE UPDATE/INSERT TRIGGER with RAISE ERROR 您可以使用RAISE ERROR创建BEFORE UPDATE / INSERT TRIGGER

EDIT: Raise Error with 编辑:引发错误

SET new.score = 1 / 0; 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;

In short: MySQL does not support CHECK constraints, although for compatibility with other engines it will parse them (but ignores them). 简而言之:MySQL不支持CHECK约束,尽管为了与其他引擎兼容,它将解析它们(但忽略它们)。

See: MySQL and Check Constraints 请参阅: MySQL和检查约束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM