Computer >> Computer tutorials >  >> Programming >> MySQL

How can we create and use ENUM columns in MySQL?


For creating an ENUM column, the enumeration value must be a quoted string literals. We can create ENUM columns in MySQL with the help of the following syntax −

CREATE TABLE table_name(
   …
   Col ENUM(‘Value1’,’Value2’,’Value3’),
   …
);

In the above syntax, we have three enumeration values. It can be more than three also.

Example:

 Following is an example of creating a table with ENUM column −

mysql> Create table marks(id int Primary key NOT NULL, Name Varchar(255) NOT NULL, Result ENUM('Pass', 'Fail') NOT NULL);
Query OK, 0 rows affected (0.18 sec)

The query above will create a table named marks with an ENUM field.

mysql> Insert into marks(id, name, result) values(101,'Aarav','Pass');
Query OK, 1 row affected (0.07 sec)

mysql> Insert into marks(id, name, result) values(102,'Yashraj','Fail');
Query OK, 1 row affected (0.02 sec)

With the help of queries above we can insert the values in the table.

mysql> Select * from marks;
+-----+---------+--------+
| id  | Name    | Result |
+-----+---------+--------+
| 101 | Aarav   | Pass   |
| 102 | Yashraj | Fail   |
+-----+---------+--------+
2 rows in set (0.00 sec)