SQL add a Default to an Existing Table Column

When a previously created database table requires the addition of a default value. Can the amendment be made easily? And without recourse to the SQL Configuration Manager.

I had configured a database table. Subsequent testing showed that one of the fields required a default value.

I was testing for a value of less than 0, but there was an occasion where this field may not be given a value.

The use of a default value on the field would ensure a minimum base value for use in comparisons.

For ease I wished to implement the change through the website’s admin SQL page.

SQL add default to existing table column using DNN SQL console

I was using DotNetNuke which supports this. Your website might support something similar, or perhaps write a one-time use piece of code, or action through another SQL interface.

ALTER TABLE fruit ADD default ((-1)) FOR qty

In the example above I am amending the table called fruit and assigning a default value -1 to the qty field.