dev-resources.site
for different kinds of informations.
Working with NULL in Databases. Turn Your Frustration Into Delight
NULL values are one of the most frequent frenemies of almost all database administrators. For many, theyâre simply a symbol that no value exists in a column â for others, theyâre a symbol of problems within their database instances. Join us as we explore them and tell you how best to act in specific scenarios.
NULL
values are some of the most popular values within database management systems: thereâs no need to be a database expert to tell that NULL
means nothing, and thatâs exactly what NULL
signifies â itâs a symbol to signify that no values exist in a column. Thatâs not to say that NULL cannot be problematic though.
The Basics of NULL
But thatâs the least of our problems... Keep reading â weâll tell you why in a second.
Problems with NULL
âEmpty result set fetched.â Why? Because something = nothing (NULL) is never true.
To test for null, we need to use the IS [NOT] NULL
clause rather than search for a match â if we need a function, we should probably turn to IFNULL()
instead. In the previous example, weâve successfully confused MySQLâ are we searching for an email with a value of NULL
? Or are we searching for an email without a value, but with a default value of NULL
? The question is not clear â so is the answer.
Technically, 3 NULL
values were inserted, but as the column was set to increment automatically, the values were populated with numbers.
As far as indexes are concerned, columns having NULL
values can easily be indexed and that isnât the cause of much problems, however, bear in mind that NULL values also occupy space within MySQL. Everything works like this:
When MyISAM is being used, a field set to NOT NULL
will take up less space on the disk.
When MyISAM is in use, a field set to NULL
as the default value, will take up more space on the disk.
NULL
does take up storage space, but in order to see any real impact of storing NULL
values within a database, you would literally have to have billions of such values (at that point NULL
values are the least of your problems.) In other words, NULL
values donât require much storage space in and of itself for many storage engines within MySQL, however, if the NDB storage engine (or NDBCluster) is in use, things change a little â it reserves 4 bytes per row for NULL values. However, the impact of this is probably negligible.
However, if we come to partitions, thatâs where NULL
values cause the most problems for many advanced DBAs. We get into them below.
Partitioning and NULL
One more problem with NULL
values that is specific to MySQL and its associates is partitioning. MySQL as an RDBMS doesnât exactly disallow or forbid the NULL value to be used, but it does treat the NULL value as being âless than any other value.â Hereâs how everything works when our data is partitioned:
If HASH or KEY partitioning is being used, NULL
is treated the same as if the expression would return 0 (more information can be found over at the documentation.)
Summary
In this blog, weâve walked you through the concept of NULL
values in relational database management systems, with the examples being based on MySQL. While the concept of NULL
values might seem problematic and perhaps even frightening to some, it doesnât have to be that way â when properly managed, NULL values can become a really good friend of yours.
We hope that youâve enjoyed reading this blog, come back to the DbVisualizer blog in the future to learn even more about databases and how they work, and until next time!
About the author
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
Featured ones: