MySQL Restrict Column to Specific Values

Issue

You may need to restrict MySQL Table Column to only allow some predefined values. For example, Lets assume that column “gender” can only have values “male” or “female”. Other values should not be accepted for that column.

Solution

You can use Enum

ENUM('male', 'female')

For an example,

CREATE TABLE myTable(id INT, gender ENUM('male', 'female'))

Above example, code will create a new table, with a “gender” column which will only allow “male” or “female” values to be inserted

Leave a Reply

(*) Required, Your email will not be published