A default value of a column can be added like this
ALTER TABLE [MyTable] ADD DEFAULT ((0)) FOR [MyColumn]
or like this
ALTER TABLE [MyTable] ADD CONSTRAINT [DF_MyTable_MyColumn] DEFAULT ((0)) FOR [MyColumn]
What is the difference between the two?
The ANSI SQL standard defines DEFAULT as being a column attribute whereas Microsoft implemented DEFAULT as a type of constraint.
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new records, if no other value is specified.
The default value provided for a column can be null, a constraint value that is compatible with the data type of the column, or a value that is provided by the database manager.
Column constraints are restrictions on the data that can be inserted into a given column.
The constraint in the first example will be assigned a name by SQL Server.
SQL Server peculiarly classifies defaults as "constraints". The syntax using the CONSTRAINT keyword allows you to specify a name for the default, which is good practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With