SQL Server等同于MySQL enum数据类型?

时间:2022-02-24 16:24:06

Does SQL Server 2008 have a a data-type like MySQL's enum?

SQL Server 2008是否有数据类型,比如MySQL的enum?

5 个解决方案

#1


112  

It doesn't. There's a vague equivalent:

它不是。有一个模糊的等价:

mycol VARCHAR(10) NOT NULL CHECK (mycol IN('Useful', 'Useless', 'Unknown'))

#2


68  

The best solution I've found in this is to create a lookup table with the possible values as a primary key, and create a foreign key to the lookup table.

我发现的最佳解决方案是创建一个查找表,其中可能的值作为主键,并为查找表创建一个外键。

#3


3  

IMHO Lookup tables is the way to go, with referential integrity. But only if you avoid "Evil Magic Numbers" by following an example such as this one: Generate enum from a database lookup table using T4

IMHO查找表是一种使用引用完整性的方法。但是,只有当您遵循如下示例:使用T4从数据库查找表生成enum时,才能避免“邪恶的魔法数字”

Have Fun!

玩得开心!

#4


2  

CREATE FUNCTION ActionState_Preassigned()
RETURNS tinyint
AS
BEGIN
    RETURN 0
END

GO

CREATE FUNCTION ActionState_Unassigned()
RETURNS tinyint
AS
BEGIN
    RETURN 1
END

-- etc...

Where performance matters, still use the hard values.

如果性能很重要,仍然使用硬值。

#5


1  

Found this interesting approach when I wanted to implement enums in SQL Server.

当我想在SQL Server中实现枚举时,发现了这种有趣的方法。

The approach mentioned below in the link is quite compelling, considering all your database enum needs could be satisfied with 2 central tables.

考虑到您的数据库enum需要满足两个中心表,下面链接中提到的方法非常有吸引力。

http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/

http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/

#1


112  

It doesn't. There's a vague equivalent:

它不是。有一个模糊的等价:

mycol VARCHAR(10) NOT NULL CHECK (mycol IN('Useful', 'Useless', 'Unknown'))

#2


68  

The best solution I've found in this is to create a lookup table with the possible values as a primary key, and create a foreign key to the lookup table.

我发现的最佳解决方案是创建一个查找表,其中可能的值作为主键,并为查找表创建一个外键。

#3


3  

IMHO Lookup tables is the way to go, with referential integrity. But only if you avoid "Evil Magic Numbers" by following an example such as this one: Generate enum from a database lookup table using T4

IMHO查找表是一种使用引用完整性的方法。但是,只有当您遵循如下示例:使用T4从数据库查找表生成enum时,才能避免“邪恶的魔法数字”

Have Fun!

玩得开心!

#4


2  

CREATE FUNCTION ActionState_Preassigned()
RETURNS tinyint
AS
BEGIN
    RETURN 0
END

GO

CREATE FUNCTION ActionState_Unassigned()
RETURNS tinyint
AS
BEGIN
    RETURN 1
END

-- etc...

Where performance matters, still use the hard values.

如果性能很重要,仍然使用硬值。

#5


1  

Found this interesting approach when I wanted to implement enums in SQL Server.

当我想在SQL Server中实现枚举时,发现了这种有趣的方法。

The approach mentioned below in the link is quite compelling, considering all your database enum needs could be satisfied with 2 central tables.

考虑到您的数据库enum需要满足两个中心表,下面链接中提到的方法非常有吸引力。

http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/

http://blog.sqlauthority.com/2010/03/22/sql-server-enumerations-in-relational-database-best-practice/