可以包含数据库表中预定义值的字段的数据类型是什么? [英] what datatype to use for a field that can contain a value from pre-defined values in a database table?

查看:102
本文介绍了可以包含数据库表中预定义值的字段的数据类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库表中有一个名为 type 的字段。我希望该字段包含预定义值列表中的值。如果我使用 set 作为该字段的数据类型,则它可以包含列表中的多个值。

I have one field named type in a database table. I want this field to contain a value from a list of pre-defined values. If I use set as the datatype of this field, then it can contain more than one values from the list.

要使用哪种数据类型,以便它只能包含值列表中的一个值(预定义)?

What datatype to use, so that It can contain only one value from the list of values (pre-defined)?

推荐答案

对于mySQL,您需要 ENUM

For mySQL, you want ENUM.


ENUM是一个字符串对象,其值是从允许的值列表中选择的值,这些值在表的列规范中明确枚举创建时间。

An ENUM is a string object with a value chosen from a list of allowed values that are enumerated explicitly in the column specification at table creation time.

据我所知,MS SQL没有ENUM类型。 (如果我错了,请纠正我。)您通常会使用整数值,然后在应用程序中对其进行解析。为了限制可能值的范围,可以使用 CHECK约束

MS SQL does not have a ENUM type as far as I know. (Correct me if I'm wrong.) You would usually use integer values, and resolve them in the application. To limit the range of possible values, the CHECK constraint can be used.

这篇关于可以包含数据库表中预定义值的字段的数据类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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