Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a column with a default and a column with a default constraint?

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?

like image 641
JeremyWeir Avatar asked May 24 '11 23:05

JeremyWeir


People also ask

Is default a column constraint?

The ANSI SQL standard defines DEFAULT as being a column attribute whereas Microsoft implemented DEFAULT as a type of constraint.

What is a default 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.

Which constraint provide a default value for a column?

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.

What is a constraint column?

Column constraints are restrictions on the data that can be inserted into a given column.


2 Answers

The constraint in the first example will be assigned a name by SQL Server.

like image 83
ic3b3rg Avatar answered Oct 20 '22 20:10

ic3b3rg


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.

like image 39
nvogel Avatar answered Oct 20 '22 20:10

nvogel