Handling null values

We don't want to turn this into a why MariaDB is bad chapter, but I wanted to add a final example here, which I consider to be of high importance:

MariaDB [test]> UPDATE data SET id = NULL WHERE id = 1;
Query OK, 1 row affected, 1 warning (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 1

The id column was explicitly marked as NOT NULL:

MariaDB [test]> SELECT * FROM data; 
+----+------+

| id | data |
+----+------+
| 0 | 9.99 |
+----+------+
1 row in set (0.00 sec)

Obviously, MySQL and MariaDB think that null and zero are the same thing. Let me try to explain the problem here with a simple analogy: if you know your wallet is empty, it isn't the same as not knowing how much you have. As I am writing these lines, I don't know how much money I have with me (null = unknown), but I am 100% sure that it is way more than zero (I know with certainty that there is enough to refuel my beloved car on the way home from the airport, which is hard to do if you have nothing in your pocket).

Here's more scary news:

MariaDB [test]> DESCRIBE data; 
+-------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| data | decimal(3,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

MariaDB does remember that the column is supposed to be NOT NULL; however, it simply modifies your data again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset