0

Cant seem to figure out how to add constraints to attributes as seen below.

create table Children
(
  child_id int not null,
  fname varchar(24),
  lname varchar(24),
  phone int,
  grade varchar(24),
  primary key(child_id),
  check(grade = 'Beginner' OR 'Novice' OR 'Skilled' OR 'Expert')
)

The main thing i want to achieve here is for the grade variable to only be made one of the possible grades 'Beginner', 'Novice', 'Skilled' or 'Expert' (Working in SQL-SERVER-2008-R2)

M.Ali
  • 67,945
  • 13
  • 101
  • 127
MrArtard
  • 3
  • 3
  • `CHECK (grade IN ('Beginner','Novice','Skilled','Expert'))`. Also related: http://stackoverflow.com/questions/1434298/sql-server-equivalent-to-mysql-enum-data-type – Brad Christie Mar 29 '15 at 00:56
  • possible duplicate of [Restrict varchar() column to specific values?](http://stackoverflow.com/questions/2441427/restrict-varchar-column-to-specific-values) – har07 Mar 29 '15 at 00:56

1 Answers1

0

You need to use the IN operator, also you can simply add the key word PRIMARY KEY in column definition.

create table Children
(
  child_id int not null primary key,
  fname varchar(24),
  lname varchar(24),
  phone int,
  grade varchar(24),
  CONSTRAINT chk_Grade check grade IN ('Beginner' ,'Novice' ,'Skilled' ,'Expert')
)
GO
M.Ali
  • 67,945
  • 13
  • 101
  • 127