Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server: default constraint

Posted on 2013-10-25
3
Medium Priority
?
682 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 1000 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 1000 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

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.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

618 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