Solved

SQL Server: default constraint

Posted on 2013-10-25
3
659 Views
Last Modified: 2013-11-05
Table column has default constraint.
Is better to have it's value in INSERT statement or let DEFAULT work?
Thanks.
0
Comment
Question by:quasar_ee
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
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
 
LVL 1

Assisted Solution

by:JavierVera
JavierVera earned 250 total points
Comment Utility
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
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now