且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将列限制为仅接受2个值

更新时间:2023-02-24 18:30:05

您需要一个检查约束。

ALTER TABLE [TableName] ADD CONSTRAINT 
my_constraint CHECK (PatientType = 'Admitted' OR PatientType = 'OPD')

你需要检查它是否可以在MySQL中运行,特别是从今天开始。

看起来不行至少它不是几年前)。

You need to check if it works though in MySQL in particular as of today.
Seems it does not work (or at least it did not a few years ago).

MySQL CHECK Constraint

CHECK约束不起作用

MySQL CHECK约束解决方法

不确定是否已修复。

如果不工作,请使用触发器而不是检查约束。

If not working, use a trigger instead of a check constraint.