Is there any merit in creating a policy that requires developers to specify default values for all null columns?
创建一个要求开发人员为所有空列指定默认值的策略是否有任何优点?
5 个解决方案
#1
yes, consistency
keeping everyone on the same page is important, and this will ensure your devs don't get unexpected values in production.
是的,保持每个人在同一页面上的一致性非常重要,这将确保您的开发人员不会在生产中获得意外的价值。
I think it's a great idea.
我认为这是一个好主意。
#2
if you require a DEFAULT value for all NULL columns, you can make them all NOT NULL!
如果您需要所有NULL列的DEFAULT值,则可以使它们全部为NOT NULL!
#3
Nulls are often a source of confusion, due to the three value logic they involve. For example, the query...
由于它们涉及三个价值逻辑,空洞通常是混乱的来源。例如,查询...
select * from employee where job != 'MANAGER'
... might be expected to return all employees who are not managers. However, of course, if JOB can be null then this query will not return employees whose JOB is null.
......可能会让所有非经理人员回归。但是,当然,如果JOB可以为null,则此查询不会返回JOB为null的员工。
It could be that this is the rationale behind the default values policy.
可能这是默认值政策背后的基本原理。
#4
Assigning default values only shifts the problem because essentially those default values carry the same "meaning" as the nulls do, so you are then facing a requirement to include special-casing in your code to deal with the "special meaning" of those "special values".
分配默认值只会改变问题,因为基本上这些默认值带有与空值相同的“含义”,因此您需要在代码中包含特殊外壳以处理那些“特殊”的“特殊含义”。值”。
#5
@TonyAndrews :
... might be expected to return all employees who are not managers. However, of course, if JOB can be null then this query will not return employees whose JOB is null. It could be that this is the rationale behind the default values policy....
......可能会让所有非经理人员回归。但是,当然,如果JOB可以为null,则此查询不会返回JOB为null的员工。可能这是默认值政策背后的基本原理....
NULL was invented to handle missing value, so for your query its completely rational to not return rows with null value in job field! You are asking DBMS to return rows which job's are not 'Manager'; it does means not return me those whose jobs are unknown. if you want missing jobs as well, thats different question.
NULL是为了处理缺失值而发明的,因此对于您的查询来说,完全合理的是不在作业字段中返回具有空值的行!您要求DBMS返回作业不是“经理”的行;它确实意味着不能归还那些工作未知的人。如果你想要失踪的工作,那就是不同的问题。
just imagine may be some managers are there which their job fields are NULL(missing, not entered yet) so if database return them, query's result would be absolutely wrong!
想象一下可能是某些管理员在那里他们的工作字段是NULL(缺少,尚未输入)所以如果数据库返回它们,查询的结果将是绝对错误的!
coming back to main question, i think if a field is NULL allowed it should have NULL in the case of missing value, otherwise if the field never has missing value(or always needs default value) just make it NOT NULL with default value. and this is based on business logics in a specific condition.
回到主要问题,我认为如果一个字段为NULL允许它在缺少值的情况下应该为NULL,否则如果该字段永远没有缺失值(或总是需要默认值),只需使用默认值使其为NOT NULL。这是基于特定条件下的业务逻辑。
#1
yes, consistency
keeping everyone on the same page is important, and this will ensure your devs don't get unexpected values in production.
是的,保持每个人在同一页面上的一致性非常重要,这将确保您的开发人员不会在生产中获得意外的价值。
I think it's a great idea.
我认为这是一个好主意。
#2
if you require a DEFAULT value for all NULL columns, you can make them all NOT NULL!
如果您需要所有NULL列的DEFAULT值,则可以使它们全部为NOT NULL!
#3
Nulls are often a source of confusion, due to the three value logic they involve. For example, the query...
由于它们涉及三个价值逻辑,空洞通常是混乱的来源。例如,查询...
select * from employee where job != 'MANAGER'
... might be expected to return all employees who are not managers. However, of course, if JOB can be null then this query will not return employees whose JOB is null.
......可能会让所有非经理人员回归。但是,当然,如果JOB可以为null,则此查询不会返回JOB为null的员工。
It could be that this is the rationale behind the default values policy.
可能这是默认值政策背后的基本原理。
#4
Assigning default values only shifts the problem because essentially those default values carry the same "meaning" as the nulls do, so you are then facing a requirement to include special-casing in your code to deal with the "special meaning" of those "special values".
分配默认值只会改变问题,因为基本上这些默认值带有与空值相同的“含义”,因此您需要在代码中包含特殊外壳以处理那些“特殊”的“特殊含义”。值”。
#5
@TonyAndrews :
... might be expected to return all employees who are not managers. However, of course, if JOB can be null then this query will not return employees whose JOB is null. It could be that this is the rationale behind the default values policy....
......可能会让所有非经理人员回归。但是,当然,如果JOB可以为null,则此查询不会返回JOB为null的员工。可能这是默认值政策背后的基本原理....
NULL was invented to handle missing value, so for your query its completely rational to not return rows with null value in job field! You are asking DBMS to return rows which job's are not 'Manager'; it does means not return me those whose jobs are unknown. if you want missing jobs as well, thats different question.
NULL是为了处理缺失值而发明的,因此对于您的查询来说,完全合理的是不在作业字段中返回具有空值的行!您要求DBMS返回作业不是“经理”的行;它确实意味着不能归还那些工作未知的人。如果你想要失踪的工作,那就是不同的问题。
just imagine may be some managers are there which their job fields are NULL(missing, not entered yet) so if database return them, query's result would be absolutely wrong!
想象一下可能是某些管理员在那里他们的工作字段是NULL(缺少,尚未输入)所以如果数据库返回它们,查询的结果将是绝对错误的!
coming back to main question, i think if a field is NULL allowed it should have NULL in the case of missing value, otherwise if the field never has missing value(or always needs default value) just make it NOT NULL with default value. and this is based on business logics in a specific condition.
回到主要问题,我认为如果一个字段为NULL允许它在缺少值的情况下应该为NULL,否则如果该字段永远没有缺失值(或总是需要默认值),只需使用默认值使其为NOT NULL。这是基于特定条件下的业务逻辑。