Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL vs Empty when dealing with user input

Tags:

database

null

Yes, another NULL vs empty string question.

I agree with the idea that NULL means not set, while empty string means "a value that is empty". Here's my problem: If the default value for a column is NULL, how do I allow the user to enter that NULL.

Let's say a new user is created on a system. There is a first and last name field; last name is required while first name is not. When creating the user, the person will see 2 text inputs, one for first and one for last. The person chooses to only enter the last name. The first name is technically not set. During the insert I check the length of each field, setting all fields that are empty to NULL.

When looking at the database, I see that the first name is not set. The question that immediately comes to mind is that maybe they never saw the first name field (ie, because of an error). But this is not the case; they left if blank.

So, my question is, how do you decide when a field should be set to NULL or an empty string when receiving user input? How do you know that the user wants the field to be not set without detecting focus or if they deleted a value...or...or...?

Related Question: Should I use NULL or an empty string to represent no data in table column?

like image 982
Darryl Hein Avatar asked Jan 02 '09 02:01

Darryl Hein


2 Answers

I'll break the pattern, and say that I would always use NULL for zero-length strings, for the following reasons.

  1. If you start fine-slicing the implications of blanks, then you must ensure somehow that every other developer reads and writes it the same way.

  2. How do you alphabetize it?

  3. Can you unambiguously determine when a user omitted entering a value, compared with intentionally leaving it blank?

  4. How would you unambiguously query for the difference? Can a query screen user indicate NULL vs. blank using standard input form syntax?

  5. In practice, I've never been prohibited from reading and writing data using default, unsurprising behavior using this rule. If I've needed to know the difference, I've used a boolean field (which is easier to map to unambiguous UI devices). In one case, I used a trigger to enforce True => null value, but never saw it invoked because the BR layer filtered out the condition effectively.

like image 83
dkretz Avatar answered Oct 19 '22 07:10

dkretz


If the user provides an empty string, I always treat it as null from the database perspective. In addition, I will typically trim my string inputs to remove leading/trailing spaces and then check for empty. It's a small win in the database with varchar() types and it also reduces the cases for search since I only need to check for name is null instead of name is null or name = '' You could also go the other way, converting null to ''. Either way, choose a way and be consistent.

like image 40
tvanfosson Avatar answered Oct 19 '22 09:10

tvanfosson