SQL Server 2008中的枚举类型? [英] Enumerated types in SQL Server 2008?
问题描述
SQL Server中是否存在某种机制来允许枚举类型的功能?
Is there some kind of mechanism in SQL Server to allow Enumerated type like functionality?
例如,如果我有一个名为 UpdateStatus的列,则通常会进行设置像这样的单个字母值:
For example, if I have a column Called "UpdateStatus" it usually gets setup with single letter values like so:
- D
- X
- U
- I
- D
- X
- U
- I
这可能等同于很多事情。这导致混乱。另一种选择是使它成为这样的字符串列:
This could equate to a lot of things. That leads to confusion. The alternative is to have it be a string column like this:
- 已下载
- 已删除
- 已更新
- 已初始化
- Downloaded
- Deleted
- Updated
- Initialized
但这有它自己的问题。最终有人会这样写: UpdateUpdateus ='Initalized'
(拼写错误)。另外,我听说键入字符串并不是全部性能。
But that has its own problems. Eventually someone is going to write something like this: where UpdateStatus = 'Initalized'
(spelled wrong). Plus I hear that keying off of strings is not all that performant.
因此,SQL Server是否有任何枚举类型可以帮助解决此问题?基本上,我正在寻找编译时检查是否要比较的值(即初始化)是值列表的一部分。
So, is there any kind of enumerated type for SQL Server that can help out with this? Basically I am looking for compile time checking that a value being compared (ie "Initialized") is part of a list of values.
我正在使用SQL Server 2008。 / p>
I am using SQL Server 2008.
推荐答案
为什么没有包含代码和描述的查找表。为该查询表创建外键将导致仅使用有效代码。
Why not have lookup table that contains the code and description. Creating a foreign key to this lookup table will result in only valid codes being used.
这篇关于SQL Server 2008中的枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!