Search code examples
mysql

MySQL datatype to store one of two options


I have been looking at the datatypes for MySQL but there is nothing really that I can see that mandates that input is either one thing or another. E.g. "option1" or "option2".

How might I do something like this when creating a table?


Solution

  • You can use enum datatype for restricting input within a range of values. Below is a sample table that shows the usage.

    CREATE TABLE Sample 
        (
         id int auto_increment primary key, 
         options enum('option1', 'option2')
        );
    

    The options field will only accept NULL, option1 OR option2 values. However, you can mark it NOT NULL as well.

    You can further read the doco here