Home  >  Article  >  Database  >  How do we create and use ENUM columns in MySQL?

How do we create and use ENUM columns in MySQL?

王林
王林forward
2023-09-13 11:33:07988browse

我们如何在 MySQL 中创建和使用 ENUM 列?

To create an ENUM column, the enumeration value must be a quoted string literal. 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 also be more than three.

Example:

The following is an example of creating a table containing the 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 above query will create a table named marks that contains the 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 the above query, we can insert values ​​into the table.

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

The above is the detailed content of How do we create and use ENUM columns in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete