Open In App

How to Update NULL Values in a Field in MySQL

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

There is a situation where we need to update certain columns with NULL values in a MySQL database, you’re in the right place. It’s a common task when you’re working with databases and dealing with incomplete or undefined data. In this article, we’ll walk through the process, break down the syntax, and provide some real-life examples to make it all crystal clear.

So, in the database world, NULL means there’s no value present. Updating columns with NULL is handy for various reasons, like fixing missing data or resetting certain values. To pull off this database magic, you’ll be using the trusty UPDATE statement with the SET clause.

Syntax:

Updating a column with a NULL value in MySQL is surprisingly straightforward. Check out this basic syntax:

UPDATE table_name

SET column_name= ‘N/A’

WHERE condition;

  • table_name: The name of your table.
  • column_name: The column you want to turn into NULL.
  • condition: When you want to be selective. If you skip this, it’ll update all rows.

Examples of Updating MySQL Column with NULL

Example 1

Let’s create a scenario with a table named ’employees’ and a ‘salary’ column. First, we’ll create the table and add some dummy data:

-- Creating the table (if not already created)
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
phone_number VARCHAR(15)
);

-- Adding some dummy data with NULL values in the 'phone_number' column
INSERT INTO employees VALUES
(1, 'John Doe', 'Sales', NULL),
(2, 'Jane Smith', 'Marketing', '555-1234'),
(3, 'Bob Johnson', 'Sales', NULL);

Now to update the null values we will use the below query :

-- Updating NULL values in the 'phone_number' column with 'N/A'
UPDATE employees
SET phone_number = 'N/A'
WHERE phone_number IS NULL;
output

Output

Output:

Output

Output

Explanation:

The output of this code results in the ’employees’ table being updated, and any previously NULL ‘phone_number‘ entries are now set to ‘N/A‘. This enhances data consistency and provides a standardized representation for missing phone numbers in the specified table.

Example 2

create a table named ‘products‘ with a column ‘description’ that contains NULL values, and you want to update those NULL values to a default description like ‘No description available’:

-- Creating the 'products' table (if not already created)
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
description VARCHAR(200)
);

-- Adding some dummy data with NULL values in the 'description' column
INSERT INTO products VALUES
(1, 'Laptop', NULL),
(2, 'Smartphone', 'High-performance device'),
(3, 'Headphones', NULL),
(4, 'Tablet', 'Compact and versatile');

Use the following query to update:

-- Updating NULL values in the 'description' column with a default value
UPDATE products
SET description = 'No description available'
WHERE description IS NULL;
output

Output

Output:

Output

Output

Explanation:

After the execution of the UPDATE statement, the ‘products‘ table is altered, and any instances of NULL in the ‘description‘ column are replaced with the default value. The result is an updated ‘products’ table where missing or NULL descriptions have been uniformly set to ‘No description available‘.

Conclusion

In conclusion , setting NULL columns in MySQL is an easy but important process in database administration. Whether you want to edit existing records or prepare data for future records, setting a NULL column in MySQL allows you to be flexible and adapt to different data conditions. The examples showed how the UPDATE statement can be used to substitute NULL entries to maintain consistency and make data analysis more efficient.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads