How to add a default value to an already existing column? How to add a default value to an already existing column? sql-server sql-server

How to add a default value to an already existing column?


Use:

ALTER TABLE dbo.mytableADD CONSTRAINT def_mycolumn DEFAULT GETDATE() FOR mycolumn

For more info, see: Working with Default Constraints


If you want to change the default value of an already existing column. You need to first drop the constraint and then add the constraint all over again as below

ALTER TABLE <TABLE> DROP CONSTRAINT <CONSTRAINT NAME>ALTER TABLE <TABLE> ADD CONSTRAINT <CONSTRAINT NAME> DEFAULT <VALUE> for <COLUMN>

If you are not having the Constraint details of the table, you can use the below query

sp_helpconstraint <TABLE>