'Create enum in SQL Server
In MySQL one can create an enum as such:
USE WorldofWarcraft;
CREATE TABLE [users]
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
username varchar(255),
password varchar(255),
mail varchar (255),
rank ENUM ('Fresh meat', 'Intern','Janitor','Lieutenant','Supreme being')DEFAULT 'Fresh meat',
);
This is not possible in SQL Server, so what are the alternatives?
I've read this post
SQL Server equivalent to MySQL enum data type?
Suggesting something like
mycol VARCHAR(10) NOT NULL CHECK (mycol IN('Useful', 'Useless', 'Unknown'))
How can one get that work and create a default value?
The purpose of the enum would be able to tie it to a graphical dropdown on the site which presents the user with values and has a default value pre-specified.
Solution 1:[1]
There is no enum datatype available in SQL Server like in MySQL.
But using the CHECK constraint enum functionality can be implemented.
USE WorldofWarcraft;
CREATE TABLE [users]
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
username nvarchar(255),
password nvarchar(255),
mail nvarchar (255),
[rank] nvarchar (255) NOT NULL CHECK ([rank] IN('Fresh meat', 'Intern','Janitor','Lieutenant','Supreme being')) DEFAULT 'Fresh meat'
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivix4u |
