Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you add a column that only has a set choice of values?

If I wanted to add a column that could only be two values, for example, a column called 'Gender' with the two possible options of 'Male' or 'Female'

like image 722
anthonypliu Avatar asked Oct 07 '12 06:10

anthonypliu


2 Answers

use CHECK

Gender VARCHAR(6) NOT NULL CHECK (Gender IN ('Male', 'Female'))

SQLFiddle Demo

like image 125
John Woo Avatar answered Sep 30 '22 02:09

John Woo


I would probably want to use a bit field type, and not allow nulls, so that value can only be 0 or 1 (true/false).

like image 43
E.J. Brennan Avatar answered Sep 30 '22 01:09

E.J. Brennan