将列限制为仅接受2个值 [英] Restricting a column to accept only 2 values

查看:106
本文介绍了将列限制为仅接受2个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有一个名为患者类型的列。我想确保只有2个值可以插入列,opd或允许,除此之外,所有其他输入无效。

I have a column called "Patient Type" in a table. I want to make sure that only 2 values can be inserted in to the column , either opd or admitted, other than that, all other inputs are not valid.

下面是我想要的示例

如何确保列只接受opd作为患者类型列的数据。

How do I make sure that the column only accepts "opd" or "admitted" as the data for "Patient Type" column.

推荐答案

您需要一个检查约束。

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.

这篇关于将列限制为仅接受2个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆