Solved

SQL Server: default constraint

Posted on 2013-10-25
3
672 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 65

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 26

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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