Is it necessary to add DEFAULT NULL in MySQL?

MySQLMySQLi Database

No, it isn’t necessary because without adding DEFAULT NULL, it gives NULL value. For example, let’s say you haven’t added DEFAULT NULL and inserted a record with no value, then the result would display the NULL value as the inserted value.

Let us first create a table −

mysql> create table DemoTable
(
   FirstName varchar(100)
);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Chris     |
| NULL      |
| NULL      |
| David     |
+-----------+
4 rows in set (0.00 sec)
raja
Published on 26-Sep-2019 11:47:43
Advertisements