How to set MySQL default value NONE?

MySQLMySQLi Database

To set the default value in MySQL, you need to use the DEFAULT keyword. Let us first create a table −

mysql> create table DemoTable
(
   ClientCountryName varchar(100) DEFAULT 'NONE'
);
Query OK, 0 rows affected (0.65 sec)

We have set DEFAULT above for values not entered while insertion. Now, let us insert some records in the table using the insert command. We haven’t inserted values here for some of the rows. The DEFAULT gets set there −

mysql> insert into DemoTable values('US');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('UK');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('AUS');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------------+
| ClientCountryName |
+-------------------+
| US                |
| NONE              |
| UK                |
| NONE              |
| AUS               |
+-------------------+
5 rows in set (0.00 sec)
raja
Published on 24-Sep-2019 13:02:23
Advertisements