Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL datatype to store one of two options

Tags:

mysql

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?

like image 285
user3453021 Avatar asked Feb 14 '23 10:02

user3453021


1 Answers

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

like image 157
Riz Avatar answered Feb 16 '23 01:02

Riz