如何在MYSQL中设置列的最大值?

时间:2019-05-10 13:04:32

标签: mysql check-constraints

我有一个名为WORKERS的表,该表由totalNumberOfWorkers组成,我想将worker的最大值设置为30,并且不应超过30,它的范围应在0到30之间。 / p>

我已经尝试过了,但是它不起作用并显示错误,我的代码不正确。

ALTER TABLE WORKERS
CONSTRAINT WORKERS_CHECK CHECK (totalNumberOfWorkers => 0 AND totalNumberOfWorkers <31);

以下是称为“工作人员”的表

+------------------------+-------------+------+-----+---------+-------+
| Field                  | Type        | Null | Key | Default | Extra |
+------------------------+-------------+------+-----+---------+-------+
| WorkerID               | int(6)      | NO   | PRI | NULL    |       |
| dateOfWork             | date        | NO   |     | NULL    |       |
| timeOfWork             | time        | NO   |     | NULL    |       |
| descOfWorker           | varchar(50) | NO   |     | NULL    |       |
| totalNumberOfWorkers   | int(2)      | NO   |     | NULL    |       |
+------------------------+-------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:1)

找到了解决方案

ALTER TABLE WORKERS
ADD CONSTRAINT TOTALNUMBER_CHECK1 CHECK(totalNumberOfWorkers BETWEEN 1 AND 30);