How do I insert a NULL value in MySQL?

MySQLMySQLi Database

To insert a NULL value, you can use UPDATE command. Following is the syntax −

UPDATE yourTableName SET yourColumnName=NULL;

Let us first create a table −

mysql> create table insertNullValue
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(100),
   -> ClientCountryName varchar(20)
   -> );
Query OK, 0 rows affected (0.54 sec)

Following is the query to insert some records in the table using insert command −

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Larry','US');
Query OK, 1 row affected (0.19 sec)

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('David','AUS');
Query OK, 1 row affected (0.09 sec)

mysql> insert into insertNullValue(ClientName,ClientCountryName) values('Bob','UK');
Query OK, 1 row affected (0.17 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from insertNullValue;

This will produce the following output −

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | Larry      | US                |
| 2  | David      | AUS               |
| 3  | Bob        | UK                |
+----+------------+-------------------+
3 rows in set (0.00 sec)

Following is the query to insert NULL value for a column −

mysql> update insertNullValue set ClientCountryName=NULL;
Query OK, 3 rows affected (0.19 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the NULL value is inserted for the column ‘ClientCountryName’ or not. Following is the query −

mysql> select * from insertNullValue;

This will produce the following output displaying the NULL values −

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | Larry      | NULL              |
| 2  | David      | NULL              |
| 3  | Bob        | NULL              |
+----+------------+-------------------+
3 rows in set (0.00 sec)
raja
Published on 16-Apr-2019 13:04:47
Advertisements