[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

SQL Server: default constraint

Table column has default constraint.
Is better to have it's value in INSERT statement or let DEFAULT work?
Thanks.
0
quasar_ee
Asked:
quasar_ee
2 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Like any good consultant I'll say 'it depends'

I'll vote primarily for the default, if for no other reason than it insures that there are no NULL values being inserted into the column, which can be a colossal pain to deal with.

Having it in the INSERT is a good idea as well, just so there's no dependancy on the table schema.
0
 
JavierVeraCommented:
Lets say you're asked to prepare a status field in a table wich has over a year in production envyroment.


If you add a new field, then you should update the fields to the initial status, lets say you put a zero.

In this scenario, you have to think how many different stored procedures are inserting data in this table.

Now, if you find several procedure that need the factoring/modification to this field, your best option is to change the table with a default value for such field.

Sometimes, for some reports you wouldn't like to use  the "isnull(field1,0)".
So it depends wether you want to use a default value or not..

you have to consider the impact on the already developed part of the application.

Maybe it will impact a lot, maybe it wont.
Check for the reports too.
0
 
ZberteocCommented:
Another advantage of using a default in table column definition is that it will insure that there is NO WAY to get passed it no matter how you execute the insert.

Think of this scenario: in one place where insert is coded you will use the default value, then after a while another developer or dba will code/run an insert without any default, because they forgot, didn't care, whatever reason, and now you have an unwanted result.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now