Thursday 12 July 2018

A Comprehensive Look at MySQL IS NULL Operator

A Comprehensive Look at MySQL IS NULL Operator?

Summary: in this tutorial, you will learn how to use the MySQL IS NULL operator to test whether a value is NULL or not.

Introduction to MySQL IS NULL operator

To test whether a value is NULL or not, you use the  IS NULL operator. The following show syntax of the IS NULL operator:
If the value is NULL, the expression returns true. Otherwise, it returns false.
Note that MySQL does not have the built-in BOOLEAN type. It uses the TINYINT(1) to represent the BOOLEAN values i.e.,  true means 1 and false means 0.
Because the IS NULL is a comparison operator, you can use it anywhere that  an operator can be used e.g., in the SELECT or WHERE clause. See the follow example:
To check if a value is not NULL, you use IS NOT NULL operator as follows:
This expression returns true (1) if the value is not NULL. Otherwise, it returns false (0). Consider the following example:

MySQL IS NULL examples

We will use the customers table in the sample database for the demonstration.

To get customers who do not have sales representative, you use the IS NULL operator as follows:

To get the customers who have a sales representative, you use the IS NOT NULL operator:

MySQL IS NULL’s specialized features

To be compatible with ODBC programs, MySQL supports some specialized features of the IS NULLoperator.
1) If the DATE or DATETIME column that has NOT NULL constraint and contains the special date '0000-00-00', you can use the IS NULL operator to find such rows.
In this example, we created a new table named projects and insert some data into the table. The last query used IS NULL to get the rows whose values in the complete_date value is '0000-00-00'.
2) If the variable @@sql_auto_is_null is set to 1, you can get the value of a generated column after executing an INSERT statement by using the IS NULL operator. Note that by default the variable @@sql_auto_is_null is 0. See the following example.
First, set the variable @@sql_auto_is_null to 1.
Second, insert a new row into the projects table:
Third, use the IS NULL operator to get the generated value of the id column:
MySQL IS NULL sql_auto_is_null example

MySQL IS NULL Optimization

MySQL performs the same optimization for the IS NULL operator in the same way that it does for the equal (=) operator.
For example, MySQL uses the index when it searches for NULL with the IS NULL operator as shown in the following query:
See the EXPLAIN of the query:

MySQL can also optimize for the combination col = value OR col IS NULL. See the following example:

In this example, the EXPLAIN shows ref_or_null when the optimization is applied.
If you have a key that is a combination of columns, MySQL can perform optimization for any key part. Suppose there is an index on columns k1 and k2 of the table t1, the following query is optimized:
In this tutorial, you have learned how to use MySQL IS NULL operator to test whether a value is NULL or not.

0 comments:

Post a Comment