- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I update NULL values in a field in MySQL?
Let us first create a table −
mysql> create table OrderDemo -> ( -> OrderId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> OrderPrice int, -> OrderDatetime datetime -> ); Query OK, 0 rows affected (0.66 sec)
Example
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(200,'2016-09-12'); Query OK, 1 row affected (0.24 sec) mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(NULL,'2002-11-18'); Query OK, 1 row affected (0.26 sec) mysql> insert into OrderDemo(OrderPrice,OrderDatetime) values(1000,'2017-12-28'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from OrderDemo;
Output
+---------+------------+---------------------+ | OrderId | OrderPrice | OrderDatetime | +---------+------------+---------------------+ | 1 | 200 | 2016-09-12 00:00:00 | | 2 | NULL | 2002-11-18 00:00:00 | | 3 | 1000 | 2017-12-28 00:00:00 | +---------+------------+---------------------+ 3 rows in set (0.00 sec)
Here is the query to add a row in OrderPrice column where OrderPrice is NULL i.e. updating the NULL −
mysql> update OrderDemo set OrderPrice = 6500 where OrderPrice IS NULL; Query OK, 1 row affected (0.17 sec) Rows matched: 1 Changed: 1 Warnings: 0
Now check the table record once again. The query is as follows −
mysql> select *from OrderDemo;
Output
+---------+------------+---------------------+ | OrderId | OrderPrice | OrderDatetime | +---------+------------+---------------------+ | 1 | 200 | 2016-09-12 00:00:00 | | 2 | 6500 | 2002-11-18 00:00:00 | | 3 | 1000 | 2017-12-28 00:00:00 | +---------+------------+---------------------+ 3 rows in set (0.00 sec)
The NULL value has been replaced with 6500.
Advertisements