MySQL query to set values for NULL occurrence


Find NULL values using the IS NULL and update the new values using MySQL UPDATE and SET −

update yourTableName
set yourColumnName=yourValue
where yourColumnName IS NULL;

Let us first create a table −

mysql> create table DemoTable768 (
   Clientid int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(100),
   ClientAge int
);
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable768(ClientName,ClientAge) values('John',23);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,26);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable768(ClientName,ClientAge) values('Carol',28);
Query OK, 1 row affected (0.53 sec)
mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,24);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable768;

This will produce the following output -

+----------+------------+-----------+
| Clientid | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | John       |        23 |
|        2 | NULL       |        26 |
|        3 | Carol      |        28 |
|        4 | NULL       |        24 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Following is the query to set values for NULL occurrence −

mysql> update DemoTable768
   set ClientName='Chris'
    where ClientName IS NULL;
Query OK, 2 rows affected (0.41 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Let us check the description of the view −

mysql> select *from DemoTable768;

This will produce the following output -

+----------+------------+-----------+
| Clientid | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | John       |        23 |
|        2 | Chris      |        26 |
|        3 | Carol      |        28 |
|        4 | Chris      |        24 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Updated on: 03-Sep-2019

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements