Home > Database > Mysql Tutorial > body text

How to insert default value in MySQL ENUM data type?

WBOY
Release: 2023-08-26 18:41:17
forward
1039 people have browsed it

如何在 MySQL ENUM 数据类型中插入默认值?

We can do this with the help of the DEFAULT attribute of the ENUM data type. The DEFAULT attribute causes the ENUM data type to have a default value when no value is specified. In other words, we can say that the INSERT statement does not have to contain the value of the field because if it does not then the value following the DEFAULT will be inserted. DEFAULT Functions are not allowed in expressions. For the ENUM data type, DEFAULT values ​​include NULL and the empty string (‘’).

Example

mysql> Create table enum123(Rollno INT, Name Varchar(20), result ENUM('Pass','Fail') DEFAULT 'Fail');
Query OK, 0 rows affected (0.12 sec)

mysql> Insert into enum123(Rollno, Name) Values(25, 'Raman');
Query OK, 1 row affected (0.13 sec)
Copy after login

We haven’t inserted any value in the Result column, so it will select the word after DEFAULT as the value. In this case, the default value "failed" will be inserted.

mysql> Select * from enum123;
+---------+--------+--------+
| Rollno  | Name   | result |
+---------+--------+--------+
| 25      | Raman  |   Fail |
+---------+--------+--------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How to insert default value in MySQL ENUM data type?. 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