MySQL NULL Values
MySQL NULL Values
In MySQL, a NULL value represents missing, unknown, or undefined data. It is not the same as zero (0) or an empty string (”). NULL simply means no value has been stored.
π Key Facts About NULL
| Value Type | Meaning |
|---|---|
NULL |
No value / unknown |
0 |
Number zero (valid value) |
'' (empty string) |
Text with zero characters |
' ' (space) |
One character text |
So:
β NULL β 0
β NULL β ''
π§ͺ Example Table
| id | name | age | |
|---|---|---|---|
| 1 | Rahul | 25 | rahul@gmail.com |
| 2 | Priya | NULL | priya@example.com |
| 3 | Aman | 30 | NULL |
Here, Priyaβs age is unknown and Amanβs email is missing.
π Checking NULL Values (IS NULL)
To find rows where a column contains NULL:
π Checking Non-NULL Values (IS NOT NULL)
β Why Not Use = NULL?
This is incorrect:
Because NULL is not a value β it represents absence of a value β so equality comparison doesnβt work.
π§ Updating a Field to NULL
π Inserting NULL Values
π§ Using IFNULL() or COALESCE() to Handle NULL
Sometimes, you want to display a default value when NULL is found.
π§ NULL in Comparisons with Operators
| Expression | Result |
|---|---|
NULL = NULL |
FALSE |
NULL <> NULL |
FALSE |
NULL + 10 |
NULL |
COUNT(column) |
Ignores NULL values |
π§Ύ Summary
| Feature | Example |
|---|---|
| Check for NULL | IS NULL |
| Check for not NULL | IS NOT NULL |
| Insert NULL | VALUES (NULL) |
| Update to NULL | SET column = NULL |
| Display alternate value | IFNULL() or COALESCE() |
