Darin Clark


Are there any vices to using default constraints on all columns in your table.

For example an Int that defaults to 0

or a char or varchar that defaults to ''

I know that 0 and Null are not the same thing. But if your programs don't have the concept of NULL then you have to convert the NULL to zero.

So, DEFAULT CONSTRAINTS on every column. Is it good or Bad

Thanks

Darin Clark





Re: Default Constraint on columns Pros and Cons?

richbrownesq


My opinion is use them when its appropriate.

For example, if you had a BIT field such as "OkToSendLotsOfSpamTo" then you may wish to have a default on this to say default(0) as in opt out.

However, for data such as a persons gender char(1) it makes no sense to have a default.

HTH!







Re: Default Constraint on columns Pros and Cons?

Darin Clark

Thanks for your reply.

In the case of gender though wouldn't an empty field or an U for unknown be simpler for programs to process than NULL would

Darin Clark







Re: Default Constraint on columns Pros and Cons?

richbrownesq

I guess that depends. As the value NULL suggests its unknown then, from a DB perspective i see no reason to change it. NULL means unknown across the board- for an INT, a CHAR, a DATETIME etc. If you want to put in your own bespoke logic to cater for unknown values then i guess that is something you can do but in my opinion its added complexity.

HTH!






Re: Default Constraint on columns Pros and Cons?

Arnie Rowland

From a qualitative analysis perspective, there can be major differences between a NULL and zero (0), or 'empty string'.

For example:

A NULL value could indicate that the field has NEVER held a response, and

an 'empty string' could indicate that there has been a response and it was deleted.

Use NULLs where it the NULL value adds meaning to the data, use DEFAULT values in all other fields. NULL values increase code on the development side, with the constant need for NULL validation (dbNULL) before displaying the field value on the screen or report. IF NULL is not required, DEFAULT values ease the 'hassle' factor for the DEV staff.

HOWEVER, remember that DEFAULT zero (0) values may have adverse affects for certain aggregations. Using COUNT(), AVG(), StDEV(), VAR(), as well as some other perhaps desired mathematical capabilites could be substaintly skewed by DEFAULT zero (0) values.

So carefully consider if using a DEFAULT value is a help or a hinderance.






Re: Default Constraint on columns Pros and Cons?

Louis Davidson

No disagreement with anyone here. I will add two more cents worth

I think that you have to be careful in either way. Since NULL generally means unknown, so where at all possible, I try to use it in that way.
If the value is unknown, then use NULL. If the value means I don't have a value, then using 0 or '' makes sense. But, note that those values too often have meaning. '' might mean I don't have a value. 0 is an even worse case. 0 is too often a value that has a specific meaning (I got this for free = 0 dollars, where as NULL would mean you didn't know the price)
NULL does have the nasty problem that it also means to people a missing value. Foreign keys are where this is the worst. I do feel it best to have allow NULL when the value is unknown, but have a key value that means "No related value".
I understand why you want to avoid nulls, and that is a good thing. Normalization can reduce nulls by breaking down tables to they have only a single meaning, and not a lot of not required values. But the problems with NULL values are generally well enough known and everyone has them, so once you understand the problems of them in one context, they are always the same.





Re: Default Constraint on columns Pros and Cons?

Darin Clark

Thanks for everyone's thoughts on the matter.

I am recluctant to mark any one reply as the answer as they all build on each other.






Re: Default Constraint on columns Pros and Cons?

Arnie Rowland

As is the case in 'real' life, usually there is no 'the answer' -but many nuanced shades.






Re: Default Constraint on columns Pros and Cons?

Louis Davidson

True, but we do need to mark as many of the responses as answers as are good, for other people who do searches Smile