Let’s say that the record comes from a form to gather name and address information. Line 2 of the address will typically be blank if the user doesn’t live in apartment. An empty string in this case is perfectly valid. I tend to prefer to use NULL to mean that the value is unknown or not given.
I don’t believe the physical storage difference is worth worrying about in practice. As database administrators, we have much bigger fish to fry!
I do not know about MySQL and PostgreSQL, but let me treat this a bit generally.
There is one DBMS namely Oracle which doesn’t allow to choose it’s users between NULL and ”.
This clearly demonstrates that it is not necessary to distinguish between both.
There are some annoying consequences:
You set a varchar2 to an empty string like this:
Update mytable set varchar_col="";
the following leads to the same result
Update mytable set varchar_col = NULL;
But to select the columns where the value is empty or NULL, you have to use
select * from mytable where varchar_col is NULL;
select * from mytable where varchar_col="";
is syntactically correct, but it never returns a row.
On the other side, when concatenating strings in Oracle. NULL varchars are treated as empty strings.
select NULL || 'abc' from DUAL;
yields abc. Other DBMS would return NULL in these cases.
When you want to express explicitly, that a value is assigned, you have to use something like ‘ ‘.
And you have to worry whether trimming not empty results in NULL
select case when ltrim(' ') is null then 'null' else 'not null' end from dual
Now looking at DBMS where ” is not identical to NULL (e.g. SQL-Server)
Working with ” is generally easier and in most case there is no practical need to distinguish between both. One of the exceptions I know, is when your column represents some setting and you have not empty defaults for them. When you can distinguish between ” and NULL you are able to express that your setting is empty and avoid that the default applies.
It depends on the domain you are working on.
NULL means absence of value (i.e. there is no value), while empty string means there is a string value of zero length.
For example, say you have a table to store a person’ data and it contains a
Gender column. You can save the values as ‘Male’ or ‘Female’. If the user is able to choose not to provide the gender data, you should save that as
NULL (i.e. user did not provide the value) and not empty string (since there is no gender with value ”).