null or emtpy string?

  • in a nullable varchar field which is better to store in case of null value, to store a null or an empty string?

     

    10x


    If something's hard to do, then it's not worth doing.

  • From the storage point of view there is not much difference. Both of these are stored together with the row data. Whether a column is null is just a flag, and uses a single bit (even if the column is not nullable). The null varchar column would need 2 bytes extra if it is the last variable length column in a row, so the difference is negligible.

    From the point of view of queries, it depends on if you like using IS NULL and IS NOT NULL statements, or prefer = ''

    From the application's point of view if the column is nullable, you will have to prepare the application to handle null values anyway.

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • That depends on many things, and it also depends on whom you ask :-). There are people who consider NULL values as a flaw in SQL and are of the opinion that one should never use NULLs. Other people would tell you, that NULL is better.

    Generally, NULLs require some additional work - you have to be careful when using a condition on column with NULL values. For example, condition "WHERE column 'ABC' " will not display rows, where column holds NULL. So, from this point of view, empty string would be easier to manage.

    On the other hand, NULL can have different meaning than empty string - for example, if you want to differentiate between "unknown" (NULL = we have not asked the person whether it has middle name) and "not existing" (empty string = we know that this person does not have middle name).

    Well, anyway, if the column already is defined as nullable, there is no reason why not use NULL. All your queries have to take into account that there just might be some NULL in the column, no matter whether you use it or not. If you don't want to use NULLs, be sure the column does not accept NULLs!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply