Ken Falk - Software Developer

Login
  My Approach     Services     Contact     Experience     Resume     Blog    
 

Add a Default Value to an Existing Field in MS Sql Server

I really like to have default values set in my coluumns. For string data types, I like to set the default value to an empty string. My feeling is that most of the time, a NULL string value does not have a different business significance from an emptry string (although there are exceptions).  It's easier to have to deal only with one type of absent value. I generally like to set defaults in my Business Object classes and do all my inserting through those classes so technically setting defaults on the database is not necessary. However, it's better to be certain that the data is consistent just in case some manual insert/update happens outside of my code.

Sometimes when I create the tables, I forget to put the default values in. I ussually have to save a text copy of my schema changes either because I need to check it into a repository (when working with others) or I just need to simplify the task of rolling development changes out to production, Sure, I know I can get Sql Server to generate the change script for me but sometimes it's just easier to write the code.

...Anyway, here is the syntax for adding a default value to an existing coluumn.

ALTER TABLE TableName ADD DEFAULT (DefaultValue) FOR ColumnName