Set value to NULL in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In MySQL, setting a column to NULL means storing the SQL null marker, not the string 'NULL' and not an empty string. The basic rules are simple, but many bugs come from quoting NULL, using the wrong comparison syntax, or forgetting that the column definition may not allow nulls.
Updating an existing row to NULL
To clear a column value in an existing row, use NULL without quotes in an UPDATE statement.
That stores an actual SQL null in email. If you write 'NULL' instead, MySQL stores the literal text NULL, which is a normal string value.
Inserting rows with NULL
You can also insert null values directly.
This is valid only if the target column permits nulls. Otherwise, MySQL raises an error.
The column definition must allow nulls
A null assignment fails when the schema declares the column as NOT NULL.
If email were declared as NOT NULL, neither the INSERT nor the UPDATE example above would succeed.
When a null write does not work, inspect the table definition first rather than debugging the query blindly.
Querying for NULL
Nulls do not behave like ordinary values in SQL comparisons. You cannot reliably query them with =.
This is wrong:
Use IS NULL instead:
And use IS NOT NULL for the opposite case.
This rule is fundamental to SQL null handling, not just a MySQL quirk.
NULL versus empty string and zero
NULL, '', and 0 are three different states.
- '
NULLmeans missing or unknown' - '
''means a known string with zero characters' - '
0means a known numeric value'
Treating them as interchangeable causes reporting bugs, bad filters, and inconsistent validation logic. If your application wants to normalize one into another, do it intentionally.
Cleaning placeholder values into true nulls
Imported data often uses placeholders such as empty strings or N/A. You can convert those placeholders into real nulls with UPDATE.
This is common after CSV imports or legacy migrations where missing values were not modeled properly.
Reading nullable columns with defaults
Sometimes you want to preserve nulls in storage but display a fallback in query results. That is what COALESCE is for.
COALESCE changes the result set, not the stored data. That is often preferable when the underlying absence of data should remain intact.
Common Pitfalls
The most common mistake is writing 'NULL' instead of NULL. Those values are not equivalent.
Another problem is using = NULL in a WHERE clause and wondering why no rows match. Use IS NULL and IS NOT NULL instead.
It is also easy to overlook schema constraints. If the column is NOT NULL, MySQL is correctly rejecting the write rather than misinterpreting your query.
Summary
- Use
SET column = NULLorVALUES (..., NULL)to store a real null in MySQL. - Do not quote
NULLunless you literally want the text'NULL'. - The target column must allow null values.
- Query nulls with
IS NULLandIS NOT NULL, not=. - Keep the difference between null, empty string, and numeric zero clear in your schema and queries.
Related reading
- Setting default values for columns in JPA
- Setting Django up to use MySQL
- Setting Django up to use MySQL
- Setting global sql_mode in MySQL
- setting multiple column using one update
- Setting the MySQL root user password on OS X
- Setting up foreign keys in phpMyAdmin?
- Setting up MySQL and importing dump within Dockerfile

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.