Home > Database > Mysql Tutorial > body text

How do we create and use ENUM columns in MySQL?

王林
Release: 2023-09-13 11:33:07
forward
987 people have browsed it

我们如何在 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’),
   …
);
Copy after login

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template