How to add a default constraint on a column already created in SQL Server?

1

In SQL Server, assuming the column already exists, how to create a new DEFAULT constraint? I searched in various references and in all instances, I always had the column to create together. I found the question interesting and would like to integrate it into the SOpt knowledge base.

    
asked by anonymous 21.01.2016 / 20:46

1 answer

1

The command is:

ALTER TABLE [schema].[tabela] 
  ADD  DEFAULT (([valor])) FOR [coluna]

If you prefer to name the constraint:

ALTER TABLE [schema].[tabela]
  ADD CONSTRAINT [nome-da-constraint] DEFAULT [valor-padrao] FOR [coluna] 

He even enriched the article and also indicated how to exclude only the constraint.

alter table [schema]. [table] drop constraint [name-of-constraint]

I found the answer in one of the responses from SOen

    
21.01.2016 / 20:46