Home  >  Article  >  Database  >  How to insert default value in MySQL ENUM data type?

How to insert default value in MySQL ENUM data type?

WBOY
WBOYforward
2023-08-26 18:41:171036browse

如何在 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)

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)

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!

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