SQL Server List of Default Values
How do I create a list of default values in a SQL Server 2008 R2 column? I want a col开发者_C百科umn to have the default values of easy, medium, expert.
Unlike MySql, SQL Server doesn't have enum types that you can constrain a column with ease.
The closest you can come is use a CHECK
constraint.
An an example, imagine a table of different math tests with a difficulty rating which must be either 'easy', 'medium' or 'expert'
CREATE TABLE Test
(
MathTestID smallint NOT NULL,
DifficultyRating varchar(6) NOT NULL,
CHECK (DifficultyRating In ('Easy', 'Medium','Expert'))
)
INSERT INTO Test
VALUES (1, 'Easy') //Works...
INSERT INTO Test
VALUES (1, 'Medium') //Works...
INSERT INTO Test
VALUES (1, 'Expert') //Works...
INSERT INTO Test
VALUES (1, 'Genius') //Fails...INSERT statement conflicted with CHECK constraint
精彩评论