patternsqlModerate
Add DEFAULT to existing column
Viewed 0 times
existingdefaultaddcolumn
Problem
In Microsoft SQL server, how can I add/change the default on an existing table column using T-SQL?
Solution
ALTER TABLE yourTable ADD CONSTRAINT constraintName
DEFAULT ('XYZ') FOR [YourColumn]To change the default, drop the constraint and re-add it with the new value:
ALTER TABLE yourTable
DROP CONSTRAINT constraintName
ALTER TABLE yourTable ADD CONSTRAINT constraintName
DEFAULT ('ABC') FOR [YourColumn]Code Snippets
ALTER TABLE yourTable ADD CONSTRAINT constraintName
DEFAULT ('XYZ') FOR [YourColumn]ALTER TABLE yourTable
DROP CONSTRAINT constraintName
ALTER TABLE yourTable ADD CONSTRAINT constraintName
DEFAULT ('ABC') FOR [YourColumn]Context
StackExchange Database Administrators Q#3843, answer score: 17
Revisions (0)
No revisions yet.