If the field has DEFAULT it must be NOT NULL?

Asked

Viewed 1,273 times

7

Situation

I was adding a few more columns to a table and falls from this thought. By default here we leave all fields can have value NULL, however if he has a DEFAULT he will insert the DEFAULT in place of NULL.

Question

If the countryside has one DEFAULT it should also be set as NOT NULL? or is indifferent?

  • It also depends on the application, if you can set the value to null it will not insert the default, will insert null.

1 answer

7


Are separate things.

The DEFAULT just makes a auto Insert if the insertion does not have a value for that column, but nothing prevents you from applying a NULL in it after that in another operation, if there is no clause NOT NULL. Including in the INSERT if you explicitly say that you want to place a NULL, the DEFAULT will not be considered and the null and void will be applied if this restriction is not defined.

So if you want to restrict nulls, write this in the modeling, don’t trust a transient operation. The DEFAULT is only a facilitator, not a restrictor.

In fact I would think if it’s worth this idea that everything should be NULL by default. I think it should be the other way around. Only when there are clear cases where the NULL is useful is that it should be allowed. And it should rarely be useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.