目次
- 1 1. Introduction
- 2 2. NULL Basics
- 3 3. How to Work with NULL
- 4 4. Searching Data Containing NULL
- 5 5. NULL and Index Performance
- 6 6. NULL and Sorting
- 7 7. Useful Functions for NULL
- 8 8. Best Practices for Handling NULL
- 9 9. Frequently Asked Questions (FAQ)
- 9.1 Q1: What is the difference between NULL, an empty string (“”), and zero (0)?
- 9.2 Q2: Why doesn’t the result of NULL = NULL evaluate to TRUE?
- 9.3 Q3: What should you watch out for when searching data that includes NULL?
- 9.4 Q4: Are there any considerations regarding NULL and indexes?
- 9.5 Q5: What is the difference between the COALESCE function and the IFNULL function?
- 9.6 Q6: Are there any database design strategies to avoid NULL?
- 9.7 Q7: Can NULL affect the results of aggregate functions?
- 9.8 Q8: Can issues arise when joining data that contains NULL?
- 9.9 Summary
- 10 10. Summary
1. Introduction
MySQL is a database management system used in many applications and systems. Within it, the concept of “NULL” is one of the topics that beginners find difficult to grasp. Accurately understanding what “NULL” is and how to handle it is crucial for making the most of MySQL. This article provides a comprehensive guide covering everything from the basic definition of NULL in MySQL to how to manipulate it, search for it, useful functions related to NULL, and key points to watch out for. It also includes an FAQ section that answers common questions about NULL. This article is intended for the following readers.- Beginners using MySQL for the first time
- Intermediate users who understand the basics of SQL and want to learn more deeply
- Engineers involved in database design or operations
- Correctly understand what NULL is
- Be able to manipulate and query data that includes NULL
- Learn best practices for avoiding issues related to NULL
2. NULL Basics
When working with databases, the concept of “NULL” is extremely important. However, NULL is also one of the elements that is easily misunderstood. In this section, we will explain the basic definition and characteristics of NULL in detail.Definition of NULL
NULL indicates a special state that represents “no value exists” or “an unknown value”. This is different from an empty string (“”) or zero (0). Below are examples showing the differences.- NULL: No value exists (undefined state)
- Empty string (“”): The value exists but its content is empty
- Zero (0): The value exists and its value is “0”
Characteristics of NULL
- Handling of NULL in comparison operations In SQL, NULL is handled with special rules. For example, you need to be careful about the results of comparison operations like the following.
SELECT NULL = NULL; -- Result: NULL
SELECT NULL <> NULL; -- Result: NULL
SELECT NULL IS NULL; -- Result: TRUE
- Even when you compare NULL using regular comparison operators (=, <, >, etc.), the result is NULL.
- To evaluate NULL correctly, you need to use
IS NULL
orIS NOT NULL
.
- NULL in arithmetic operations Arithmetic operations that include NULL always return NULL. Example:
SELECT 10 + NULL; -- Result: NULL
SELECT NULL * 5; -- Result: NULL
- Logical operations with NULL When a condition contains NULL, the result is also NULL. See the examples below.
SELECT NULL AND TRUE; -- Result: NULL
SELECT NULL OR FALSE; -- Result: NULL
NULL and Causes of Trouble
If NULL is not handled correctly, the following problems can occur.- Unexpected query results For example, the following query excludes rows where
age
is NULL.
SELECT * FROM users WHERE age > 20;
As a solution, you need to include NULL in the condition: SELECT * FROM users WHERE age > 20 OR age IS NULL;
- Calculation errors or misinterpretation of blank data Aggregate functions (SUM, AVG, etc.) ignore NULL when calculating. Therefore, datasets with many NULLs can produce unexpected results.
Summary of Basic NULL Rules
- NULL represents a state where no value exists.
- Because regular comparison operators cannot handle it correctly, use
IS NULL
orIS NOT NULL
. - If NULL is involved in arithmetic or logical operations, the result is also NULL.
3. How to Work with NULL
When working with NULL in MySQL, you need to understand the proper handling methods. This section provides a detailed explanation of how to insert, update, and delete NULL values.How to Specify NULL When Inserting Data
When inserting a new record into the database, you can set a column to NULL. Below are concrete examples.- Explicitly Specify NULL
INSERT INTO users (name, age) VALUES ('Taro', NULL);
This query leaves the age
column without a value, inserting NULL.- NULL as Default Value If NULL is set as the default value, omitting the column will automatically insert NULL.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT DEFAULT NULL
);
INSERT INTO users (name) VALUES ('Hanako');
In this example, because no explicit value is provided for the age
column, the default NULL is inserted.How to Set NULL When Updating Data
When updating existing data, you can also set a column’s value to NULL. Below are examples.- Update Value to NULL
UPDATE users SET age = NULL WHERE name = 'Taro';
This query sets the age
column to NULL for records where the name is ‘Taro’.- Conditional Update You can add conditions to set NULL in specific situations.
UPDATE users SET age = NULL WHERE age < 18;
Here, the age
column is set to NULL for all records where the age is less than 18.How to Use NULL as a Condition When Deleting Data
When deleting data that includes NULL, you need to include NULL in the condition. UseIS NULL
instead of a comparison operator.- Delete Using NULL Condition
DELETE FROM users WHERE age IS NULL;
This query deletes records where the age
column is NULL.- Delete with Multiple Conditions Including NULL
DELETE FROM users WHERE age IS NULL AND name = 'Taro';
In this example, only records where age
is NULL and name
is ‘Taro’ are deleted.Precautions When Working with NULL
- Use
IS NULL
Correctly When specifying NULL in a condition, always useIS NULL
orIS NOT NULL
instead of the=
operator.
SELECT * FROM users WHERE age = NULL; -- Incorrect
SELECT * FROM users WHERE age IS NULL; -- Correct
- Design Applications with NULL Handling in Mind When your application manipulates data, being mindful of NULL handling can prevent unintended behavior.
- Leverage Transactions For data operations involving NULL, consider using transactions to avoid unintended data changes.
4. Searching Data Containing NULL
When searching data in MySQL, handling NULL correctly is extremely important. Because NULL differs from regular values, special care is required. This section explains how to efficiently search data that contains NULL.Basic Methods for Searching NULL
When searching for NULL, useIS NULL
or IS NOT NULL
instead of regular comparison operators (=, <, >).- Search for NULL
SELECT * FROM users WHERE age IS NULL;
This query retrieves all records where the age
column is NULL.- Search for non-NULL values
SELECT * FROM users WHERE age IS NOT NULL;
This query retrieves all records where the age
column is not NULL.Searching with Complex Conditions Involving NULL
Because NULL cannot be handled correctly with comparison operators, extra caution is needed when using it in complex conditions.- When including NULL in a condition
SELECT * FROM users WHERE age > 20 OR age IS NULL;
This query retrieves records where age
is greater than 20 or is NULL.- NOT operator with NULL
SELECT * FROM users WHERE NOT (age > 20 OR age IS NULL);
This query retrieves records where age
is 20 or less and not NULL.Using NULL with the LIKE Operator
TheLIKE
operator cannot be used with NULL. Because NULL represents the absence of a value, the following query returns no results:SELECT * FROM users WHERE name LIKE '%a%';
-- Rows with NULL values are not matched by this condition
Instead, you need to add a NULL check:SELECT * FROM users WHERE name LIKE '%a%' OR name IS NULL;
Aggregate Functions and Searching NULL
NULL is ignored by many aggregate functions (SUM, AVG, etc.). Therefore, you need to account for NULL to obtain correct results.- COUNT function
SELECT COUNT(*) AS total_records, COUNT non_null_ages FROM users;
COUNT(*)
: counts all records, including those with NULLCOUNT(column)
: counts records where the column is not NULL- Other aggregate functions
SELECT AVG(age) AS average_age FROM users WHERE age IS NOT NULL;
Calculates the average excluding NULL values.Precautions When Searching for NULL
- Difference between
IS NULL
and=
Because NULL cannot be used with regular comparison operators, you must always useIS NULL
orIS NOT NULL
.
SELECT * FROM users WHERE age = NULL; -- Incorrect
SELECT * FROM users WHERE age IS NULL; -- Correct
- Consideration in multiple conditions When NULL is present, if you don’t explicitly include NULL in the condition, you may get unintended results.
SELECT * FROM users WHERE age > 20; -- NULL rows are excluded
SELECT * FROM users WHERE age > 20 OR age IS NULL; -- includes NULL
- Impact on performance When NULL is part of a condition, index usage may be limited. It’s recommended to verify index effectiveness.
EXPLAIN SELECT * FROM users WHERE age IS NULL;
Summary
Searching for NULL correctly is crucial for obtaining the intended results. When searching data that includes NULL, useIS NULL
and IS NOT NULL
appropriately and consider performance and index implications.5. NULL and Index Performance
Optimizing database performance requires leveraging indexes. However, operations on columns that contain NULL can affect index efficiency. This section explains the relationship between NULL and indexes, the impact on performance, and key optimization points.Setting Indexes on Columns Containing NULL
In MySQL, you can create indexes on columns that contain NULL. For example, you can create an index with SQL like the following.CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
INDEX (age)
);
In this case, the index remains usable even if the age
column contains NULL.Using Indexes with IS NULL and IS NOT NULL
Searches that include NULL in their conditions may or may not use indexes.- When the index is applied
SELECT * FROM users WHERE age IS NULL;
In this query, the index is utilized, enabling efficient search.- When the index is not applied Using more complex conditions, as shown below, can prevent the index from being used.
SELECT * FROM users WHERE age + 1 IS NULL;
Whether the index is used depends on the query conditions.NULL and Composite Indexes
Even with composite indexes, columns that contain NULL receive special treatment.- Example of a composite index
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
department_id INT,
salary INT,
INDEX (department_id, salary)
);
In this case, if department_id
is NULL, part of the department_id, salary
composite index may be unusable.NULL and Its Impact on Performance
- Effectiveness of indexes
- Searches that include NULL often allow indexes to work efficiently. However, when the condition becomes complex, index usage may be limited.
- Considerations for large data volumes
- Adding indexes to columns with many NULLs can increase index size and potentially degrade query performance.
- Design to avoid NULL
- For columns that frequently contain NULL, setting a default value to reduce NULL usage can help improve performance.
Performance Optimization Tips
- Proper use of indexes Use
EXPLAIN
to verify whether an index is being applied.
EXPLAIN SELECT * FROM users WHERE age IS NULL;
- Design to minimize NULL Apply a
NOT NULL
constraint and use default values to design data that avoids NULL:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
age INT NOT NULL DEFAULT 0
);
- Reevaluate indexes Add or drop indexes based on data volume and query patterns to optimize performance.
Summary
NULL can coexist with indexes, but under certain conditions it can affect performance. Clear index design and a well-defined NULL usage policy enable efficient database operations.6. NULL and Sorting
When sorting data in MySQL, it’s important to understand how NULL is handled correctly. Because NULL differs from regular values, knowing the default sort order and how to customize it lets you achieve the intended results. This section explains the basic rules for sorting NULL and advanced manipulation techniques.Default Sort Order for NULL
In MySQL, NULL is handled as follows.- Ascending (ASC): NULL appears at the beginning.
- Descending (DESC): NULL appears at the end.
SELECT * FROM users ORDER BY age ASC;
-- NULL appears first
SELECT * FROM users ORDER BY age DESC;
-- NULL appears last
How to Position NULL in a Specified Order
You can change the default sort order to force NULL to appear at the beginning or the end.- Place NULL at the beginning
SELECT * FROM users ORDER BY age IS NULL DESC, age ASC;
In this query, records where age
is NULL are listed first, followed by records with values sorted in ascending order.- Place NULL at the end
SELECT * FROM users ORDER BY age IS NULL ASC, age ASC;
In this query, non-NULL values appear first, and NULLs are placed at the end.Handling NULL in Multi-Column Sorting
When sorting by multiple columns, you can specify how NULL is handled for each column.- Sorting with multiple conditions
SELECT * FROM users ORDER BY department_id ASC, age IS NULL DESC, age ASC;
This query orders the data as follows.department_id
ascending- records where
age
is NULL - non-NULL
age
records in ascending order
Sorting and NULL Performance
When sorting a column that contains NULL, whether an index is used depends on the query conditions. If the index isn’t applied, sorting can take longer.- Verify Index Usage
EXPLAIN SELECT * FROM users ORDER BY age ASC;
It’s recommended to use EXPLAIN
to check whether the index is being used.Things to Watch When Sorting
- Consider the column’s data type
- If the data type of a column that can contain NULL isn’t appropriate, you may get unexpected results. Pay particular attention to the differences between numeric and string types.
- Clarify sorting conditions
- Use
IS NULL
orIS NOT NULL
to handle NULL explicitly so that query results are clear.
SELECT * FROM users WHERE age IS NULL ORDER BY age DESC;
Summary
By default, NULL appears at the beginning in ascending order and at the end in descending order, but you can customize this in your queries. Specifying the right conditions lets you achieve the desired ordering.7. Useful Functions for NULL
MySQL provides several handy functions for handling NULL efficiently. By using these functions, you can simplify data processing and query writing that take NULL into account. This section explains the most common functions and how to use them.COALESCE Function
COALESCE returns the first non-NULL value from the given arguments. It’s useful for substituting a default value for NULL.- Basic Syntax
COALESCE(value1, value2, ..., valueN)
- Example
SELECT COALESCE(age, 0) AS adjusted_age FROM users;
In this query, if age
is NULL it returns 0
; otherwise it returns the actual value.- Multiple-Argument Example
SELECT COALESCE(NULL, NULL, 'Default Value', 'Other Value') AS result;
The result will be “Default Value”.IFNULL Function
IFNULL returns a specified value instead of NULL. It’s similar to COALESCE but limited to two arguments.- Basic Syntax
IFNULL(expression, alternate_value)
- Example
SELECT IFNULL(age, 0) AS adjusted_age FROM users;
Returns 0
when age
is NULL.- Difference from COALESCE
- IFNULL handles only two arguments, whereas COALESCE can handle multiple arguments.
NULL-Safe Equality Operator (<=>)
<=> is an operator for safely comparing NULL values. Using this operator allows you to compare NULLs with each other.- Example
SELECT * FROM users WHERE age <=> NULL;
This query accurately finds rows where age
is NULL.- Difference from the regular equality operator (=)
- With the
=
operator,NULL = NULL
yields NULL, whereas the<=>
operator yields TRUE.
ISNULL Function
ISNULL determines whether a value is NULL. UsuallyIS NULL
or IS NOT NULL
is sufficient, but this function is useful when you need a functional test.- Basic Syntax
ISNULL(expression)
- Example
SELECT ISNULL(age) AS is_null FROM users;
Returns 1
if age
is NULL, otherwise returns 0
.NULLIF Function
NULLIF returns NULL if its two arguments are equal; otherwise it returns the first argument.- Basic Syntax
NULLIF(expression1, expression2)
- Example
SELECT NULLIF(salary, 0) AS adjusted_salary FROM employees;
If salary
is 0
, it returns NULL; otherwise it returns the salary
value.Choosing a NULL Function
- When you want to set a default value: Use COALESCE or IFNULL
- When you want to compare NULL safely: Use the <=> operator
- When you want to explicitly test for NULL: Use ISNULL or IS NULL
- When you want to return NULL under specific conditions: Use NULLIF
Summary
MySQL offers a rich set of handy functions for working with NULL. By choosing the appropriate function, you can write queries that are simple and efficient. Leverage these functions to optimize your NULL handling.8. Best Practices for Handling NULL
NULL plays an important role in database operations, but its nature can also lead to misunderstandings and problems. By handling NULL correctly, you can maintain data integrity while operating efficiently. This section explains best practices for working with NULL.Handling NULL in Database Design
- Deciding Whether to Allow NULL
- NULL indicates the absence of a value, but not every column needs to allow NULL.
- Example:
- For required fields (e.g., username, email address), set a
NOT NULL
constraint. - For fields that may not have a value (e.g., intermediate score, optional settings), allow NULL.
- For required fields (e.g., username, email address), set a
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
age INT NULL
);
- Setting Default Values
- Setting appropriate default values helps minimize the use of NULL.
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
status VARCHAR(20) NOT NULL DEFAULT 'pending'
);
Managing NULL on the Application Side
- Validation During Data Entry
- When users submit data via input forms, verify that required fields have values.
- Add server‑side validation as well to prevent improper NULL insertion into the database.
- Standardizing NULL Handling
- Maintain consistency in how NULL is handled across the application codebase.
- Example: Provide a helper function that converts NULL to a default value.
def handle_null(value, default):
return value if value is not None else default
Things to Watch for When Writing Queries
- Safe Comparison of NULL
- When comparing NULL, always use
IS NULL
orIS NOT NULL
.
SELECT * FROM users WHERE age IS NULL;
- Handling NULL in Complex Conditions
- In queries with multiple conditions, make the handling of NULL explicit.
SELECT * FROM users WHERE age > 20 OR age IS NULL;
- Considering Aggregates that Include NULL
- Aggregate functions (SUM, AVG, etc.) ignore NULL values during calculation. However, if you need to count NULLs, you must add a separate condition.
SELECT COUNT(*) AS total_records, COUNT(age) AS non_null_records FROM users;
Design for Better Performance and Readability
- Indexes and NULL
- When indexing columns that contain many NULLs, evaluate the index efficiency.
- Rebuild the index as needed.
- Minimizing NULL
- By limiting NULL during the design phase, you improve database readability and performance.
- Instead, use specific default values or flags.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
age INT NOT NULL DEFAULT 0
);
Common Issues in the Field and How to Avoid Them
- Issue: Unintended Query Results Due to NULL
- Solution: Use
IS NULL
andIS NOT NULL
appropriately in queries.
SELECT * FROM users WHERE name = 'Taro' OR name IS NULL;
- Issue: Unexpected Behavior of Aggregate Functions
- Solution: Add a condition to exclude NULL.
SELECT COUNT(age) FROM users WHERE age IS NOT NULL;
- Issue: NULL and Data Integrity
- Solution: Enforce
NOT NULL
constraints at the database level and thorough validation on the application side.
Conclusion
NULL is a very useful concept, but mishandling it can cause problems. By defining clear policies during database design and managing it consistently in the application, you can minimize issues related to NULL.9. Frequently Asked Questions (FAQ)
Operations and characteristics of NULL in MySQL often raise many questions from beginners to intermediate users. This section compiles common questions about NULL and their answers.Q1: What is the difference between NULL, an empty string (“”), and zero (0)?
- A1:
- NULL: Indicates that a value does not exist (undefined).
- Empty string (“”): The value exists but its content is empty.
- Zero (0): The value exists and its numeric value is 0.
- Example:
sql INSERT INTO users (name, age) VALUES ('Taro', NULL); -- age is NULL INSERT INTO users (name, age) VALUES ('Hanako', ''); -- age is an empty string INSERT INTO users (name, age) VALUES ('Jiro', 0); -- age is zero
Q2: Why doesn’t the result of NULL = NULL
evaluate to TRUE?
- A2:
- According to SQL specifications, NULL represents an “unknown value”. Comparing unknown values yields an undefined result (NULL), not TRUE or FALSE.
- When comparing NULL, you must use
IS NULL
orIS NOT NULL
. - Example:
sql SELECT NULL = NULL; -- result: NULL SELECT NULL IS NULL; -- result: TRUE
Q3: What should you watch out for when searching data that includes NULL?
- A3:
- When searching with NULL as a condition, using comparison operators (=, <, >, etc.) will not yield the expected results. Instead, use
IS NULL
orIS NOT NULL
. - Example:
sql SELECT * FROM users WHERE age = NULL; -- incorrect SELECT * FROM users WHERE age IS NULL; -- correct
Q4: Are there any considerations regarding NULL and indexes?
- A4:
- You can index columns that contain NULL, but the efficiency of the index depends on the query conditions.
- Especially with complex conditions (e.g., involving expressions), the index may become ineffective.
- Checking the index:
sql EXPLAIN SELECT * FROM users WHERE age IS NULL;
Q5: What is the difference between the COALESCE function and the IFNULL function?
- A5:
- COALESCE: Takes multiple arguments and returns the first non-NULL value.
- IFNULL: Takes only two arguments and returns the second if the first argument is NULL.
- Example:
sql SELECT COALESCE(NULL, NULL, 'default value', 'other value'); -- result: 'default value' SELECT IFNULL(NULL, 'default'); -- result: 'default'
Q6: Are there any database design strategies to avoid NULL?
- A6:
- NOT NULL constraint: Add a constraint to prevent NULL in required fields.
- Default value setting: Use a default value instead of NULL.
- Example:
sql CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, age INT NOT NULL DEFAULT 0 );
Q7: Can NULL affect the results of aggregate functions?
- A7:
- Aggregate functions (SUM, AVG, COUNT, etc.) ignore NULL when calculating. However, if you want to count the number of NULLs, you need to add a separate condition.
- Example:
sql SELECT COUNT(*) AS total_records, COUNT(age) AS non_null_ages FROM users;
Q8: Can issues arise when joining data that contains NULL?
- A8:
- When joining on columns that contain NULL, NULL is considered not matching any value, which can lead to unexpected results.
- Solution: Write a query that accounts for NULL as a condition, or use the COALESCE function to replace NULL with a default value.
SELECT *
FROM table1 t1
LEFT JOIN table2 t2 ON COALESCE(t1.key, 0) = COALESCE(t2.key, 0);
Summary
NULL is a special value in MySQL database operations. Use the FAQs covered in this section as a reference to understand NULL correctly and learn how to handle it efficiently.10. Summary
Handling NULL in MySQL is one of the essential skills for database design and operation. This article provides a comprehensive overview, covering everything from the basic definition of NULL to manipulation methods, searching, sorting, indexing, as well as useful functions and best practices.Review of Main Points
- Basics and Characteristics of NULL
- NULL means “no value” or “unknown value” and is a special value distinct from an empty string (“”) or zero (0).
- In comparison operations, you must use
IS NULL
orIS NOT NULL
to handle NULL safely.
- Manipulating and Querying Data Containing NULL
- You learned how to correctly handle NULL when inserting, updating, deleting, and querying data.
- In particular, using syntax and functions such as
IS NULL
andCOALESCE
enables flexible and efficient operations.
- NULL and Performance
- We explained how indexing columns that contain NULL affects performance and discussed data design strategies to optimize performance.
- By setting default values when appropriate, you can minimize the use of NULL.
- Convenient Functions for Handling NULL
- Functions such as COALESCE, IFNULL, and NULLIF are useful for solving NULL-related issues.
- For safe comparisons, use the
<=>
operator to prevent unintended behavior.
- Best Practices
- Designing to minimize NULL usage and applying proper validation on the application side helps maintain data integrity.
- Standardizing NULL handling in SQL queries improves code readability and maintainability.
Benefits of Understanding NULL
- Efficient Data Manipulation: Accurately handling NULL prevents unnecessary errors and enables writing efficient queries.
- Improved Data Integrity: Clarifying NULL usage policies during database design leads to higher data consistency.
- Enhanced Application Reliability: Properly handling NULL on the application side prevents unexpected behavior and bugs.
Next Steps
To deepen your understanding of NULL, we recommend the following:- Review how NULL is used in your own projects and identify areas for improvement.
- Use real datasets to experiment with functions and operators such as
IS NULL
,COALESCE
, andIFNULL
. - Perform additional tuning related to indexing and performance.