How can I insert default value in MySQL ENUM data type?

MySQLMySQLi Database

We can do it with the help of the DEFAULT attribute of the ENUM data type. The DEFAULT attribute causes an ENUM data type to have a default value when a value is not specified. In other words, we can say that INSERT statement does not have to include a value for this field because if it does not include then the value following DEFAULT will be inserted. Functions are not allowed in the DEFAULT expression. For ENUM data type the DEFAULT values include NULL and 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 have not inserted any value in ‘result’ column hence it will pick the word following DEFAULT as the value. In this case by default value ‘fail’ would be inserted.

mysql> Select * from enum123;
+---------+--------+--------+
| Rollno  | Name   | result |
+---------+--------+--------+
| 25      | Raman  |   Fail |
+---------+--------+--------+
1 row in set (0.00 sec)
raja
Published on 20-Feb-2018 06:48:14
Advertisements