Question

Is there any merit in creating a policy that requires developers to specify default values for all null columns?

Was it helpful?

Solution

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.

OTHER TIPS

if you require a DEFAULT value for all NULL columns, you can make them all NOT NULL!

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.

It could be that this is the rationale behind the default values policy.

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

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

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.

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!

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top