There appears to be at least two ways to add a default constraint using straight T-SQL. Am I correct that the only difference between the two below is that the second method specifically creates a name for the constraint, and the first method has one generated by SQL Server?
ALTER TABLE [Common].[PropertySetting] ADD DEFAULT ((1)) FOR [Active];
ALTER TABLE [Common].[PropertySetting] ADD CONSTRAINT [DF_PropertySetting_Active) DEFAULT ((1)) FOR [Active];
Pretty much, yes for an
ALTER TABLEYou can add a columnn with default in one step for
CREATEorALTERtoo.As you noted, the system generates a name if one is not supplied.
CONSTRAINT constraint_nameis optional says MSDN. The same applies to any column or table CONSTRAINTIf the column was already created, and you only want to add a (named)
DEFAULTconstraint, then use:To have the system generate the
DEFAULTconstraint name (which will be of the formDF_{TableName}_{Column}_{8RandomChars}, e.g.DF_TableName_FieldName_12345678) then omit theCONSTRAINT <name>part, like so: