NULL vs Empty when dealing with user input

前端 未结 8 2025
一整个雨季
一整个雨季 2021-02-06 11:57

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

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 12:32

    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.

提交回复
热议问题