Solved

SQL Server: default constraint

Posted on 2013-10-25
3
675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 39601328
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
ID: 39601583
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 27

Expert Comment

by:Zberteoc
ID: 39601588
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

695 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