Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are NULLs stored in a database?

Tags:

database

null

I'm curious to know how NULLs are stored into a database ?

It surely depends on the database server but I would like to have an general idea about it.


First try:

Suppose that the server put a undefined value (could be anything) into the field for a NULL value.

Could you be very lucky and retrieve the NULL value with

...WHERE field = 'the undefined value (remember, could be anything...)'

Second try:

Does the server have a flag or any meta-data somewhere to indicate this field is NULL ?

Then the server must read this meta data to verify the field.

If the meta-data indicates a NULL value and if the query doesn't have "field IS NULL", then the record is ignored.


It seems too easy...

like image 341
Luc M Avatar asked Oct 31 '08 16:10

Luc M


People also ask

How does SQL store NULL values?

Each row has a null bitmap for columns that allow nulls. If the row in that column is null then a bit in the bitmap is 1 else it's 0. For variable size datatypes the acctual size is 0 bytes.

What are NULLs in a database?

A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).

How are NULLs treated in SQL?

ORDER BY and GROUP BY with NULL SQL considers the NULL values as the UNKNOWN values. Therefore, if we use ORDER By and GROUP by clause with NULL value columns, it treats them equally and sorts, group them. For example, in our customer table, we have NULLs in the MilddleName column.

Is it okay to have NULL values in database?

You can use NULL values for any data type including integers, decimals, strings, or blobs. Even though many database administrators use NULL, they usually demand that NULLs are not used for numeric values. The reason is that NULLs used for numeric values can become confusing when developing code to calculate data.


2 Answers

MySql uses the second method. It stores an array of bits (one per column) with the data for each row to indicate which columns are null and then leaves the data for that field blank. I'm pretty sure this is true for all other databases as well.

The problem with the first method is, are you sure that whatever value you select for your data won't show up as valid data? For some values (like dates, or floating point numbers) this is true. For others (like integers) this is false.

like image 196
Thomas Jones-Low Avatar answered Sep 20 '22 13:09

Thomas Jones-Low


On PostgreSQL, it uses an optional bitmap with one bit per column (0 is null, 1 is not null). If the bitmap is not present, all columns are not null.

This is completely separate from the storage of the data itself, but is on the same page as the row (so both the row and the bitmap are read together).

References:

  • http://www.postgresql.org/docs/8.3/interactive/storage-page-layout.html
like image 27
CesarB Avatar answered Sep 20 '22 13:09

CesarB