SQL Server equivalent to MySQL enum data type?

SqlSql ServerTsqlSql Server-2008Types

Sql Problem Overview


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

Sql Solutions


Solution 1 - Sql

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

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

Solution 2 - Sql

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.

Solution 3 - Sql

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

Have Fun!

Solution 4 - Sql

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.

Solution 5 - Sql

You can try something like

ALTER TABLE dbo.yourTable
ADD CONSTRAINT yourColumn CHECK(yourColumn IN('XL','L','M','S','XS'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrickView Question on Stackoverflow
Solution 1 - SqlchaosView Answer on Stackoverflow
Solution 2 - Sqluser1431422View Answer on Stackoverflow
Solution 3 - SqlJony AdamitView Answer on Stackoverflow
Solution 4 - SqlDimitrios StaikosView Answer on Stackoverflow
Solution 5 - SqlJonan87View Answer on Stackoverflow