Enum with NOT NULL in a MySQL field?


In ENUM data type, if you do not declare NOT NULL then it gives the default value NULL. However, if you declare NOT NULL then it gives the first value from the ENUM.

CASE 1 −When the ENUM gives NULL value. Let us first create a table:

mysql> create table DemoTable1(isMarried ENUM('YES','NO'));
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1 values();
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1;

This will produce the following output −

+-----------+
| isMarried |
+-----------+
| NULL      |
+-----------+
1 row in set (0.00 sec)

CASE 2 − When the ENUM gives first value from ENUM. Let us first create a table:

mysql> create table DemoTable2 (isMarried ENUM('YES','NO') NOT NULL);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2 values();
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2;

This will produce the following output −

+-----------+
| isMarried |
+-----------+
| YES       |
+-----------+
1 row in set (0.00 sec)

Updated on: 22-Aug-2019

681 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements