NULL vs空处理用户输入 [英] NULL vs Empty when dealing with user input

查看:107
本文介绍了NULL vs空处理用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是,另一个NULL对空字符串问题。

Yes, another NULL vs empty string question.

我同意NULL意味着不设置,而空字符串意味着空值的想法。这里是我的问题:如果列的默认值为NULL,我如何允许用户输入该NULL。

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.

假设在系统上创建了一个新用户。有一个名字和姓氏字段;姓氏是必需的,而名字不是。创建用户时,该用户将看到2个文本输入,一个用于第一个,一个用于最后。人员选择只输入姓氏。技术上没有设置名字。在插入期间,我检查每个字段的长度,将所有空字段设置为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.

因此,我的问题是,如何决定何时在接收用户输入时将字段设置为NULL或空字符串?您如何知道用户希望在未检测到焦点的情况下设置字段,或者如果他们删除了一个值...或...或...?

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...?

相关问题:我应该使用NULL还是空string

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

推荐答案

在参考实际数据时,我几乎从不使用NULL。当用于外键时,我会说NULL是有效的,但它对用户输入的数据几乎是无效的。可能会经常出现的一个例外是不存在的日期,例如具有termination_date字段的员工数据库。在这种情况下,所有当前雇员在该字段中应该具有NULL值。至于让他们实际输入一个空值,对于真正需要空值的值,我会在输入字段旁边放一个复选框,以便用户可以检查它和打开,看到相应的值为null(或在更加用户友好的方式,没有)。当启用复选框将该字段设置为null时,应禁用相应的文本框,如果空值已关联,则它应该从禁用开始,并且只有在用户取消选中空复选框时才会启用。

I almost never use NULL when referring to actual data. When used for foreign keys, I would say that NULL is valid, but it is almost never valid for user entered data. The one exception that would probably come up quite regularly is for dates that don't exist, such as an employee database with a "termination_date" field. In that case, all current employees should have a value of NULL in that field. As for getting them to actually enter a null value, for the values that truly require a null value, I would put a checkbox next to the input field so that the user can check it on and off to see the corresponding value to null (or in a more user friendly manner, none). When enabling the checkbox to set the field to null, the corresponding text box should be disabled, and if a null value is already associated, it should start out as disabled, and only become enabled once the user unchecks the null checkbox.

这篇关于NULL vs空处理用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆