Answered by:
Adding a new not null column to an existing table?

Question
-
Wednesday, February 11, 2015 12:06 AM
Answers
-
Fahma,
You may add the field as a not null column with a default constraint for the existing rows. Can be done as follows:
alter table table_name add col_name varchar(255) not null constraint DF_table_name_col_name default ('ABCDE')
After doing this, the table is altered with the new column and updated with the new column values. Then if you choose, you may also drop the constraint as follows:
alter table table_name drop constraint DF_table_name_col_name
Thanks,
Jay
<If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>- Proposed as answer by Qiuyun YuMicrosoft contingent staff Wednesday, February 11, 2015 12:27 PM
- Marked as answer by Lydia ZhangMicrosoft contingent staff Tuesday, February 17, 2015 9:11 AM
Wednesday, February 11, 2015 5:58 AM -
Hi,
You will either have to specify a DEFAULT, or add the column with NULLs allowed, update all the values, and then change the column to NOT NULL.
ALTER TABLE [YourTable] ADD [NewColumn] [NewColumnType] NOT NULL DEFAULT [DefaultValue]
Hope this helps
Thanks
Bhanu
- Proposed as answer by Martin.CairneyMVP, Editor Wednesday, February 11, 2015 1:32 AM
- Marked as answer by Lydia ZhangMicrosoft contingent staff Tuesday, February 17, 2015 9:11 AM
Wednesday, February 11, 2015 12:34 AM
All replies
-
Hi,
You will either have to specify a DEFAULT, or add the column with NULLs allowed, update all the values, and then change the column to NOT NULL.
ALTER TABLE [YourTable] ADD [NewColumn] [NewColumnType] NOT NULL DEFAULT [DefaultValue]
Hope this helps
Thanks
Bhanu
- Proposed as answer by Martin.CairneyMVP, Editor Wednesday, February 11, 2015 1:32 AM
- Marked as answer by Lydia ZhangMicrosoft contingent staff Tuesday, February 17, 2015 9:11 AM
Wednesday, February 11, 2015 12:34 AM -
Fahma,
You may add the field as a not null column with a default constraint for the existing rows. Can be done as follows:
alter table table_name add col_name varchar(255) not null constraint DF_table_name_col_name default ('ABCDE')
After doing this, the table is altered with the new column and updated with the new column values. Then if you choose, you may also drop the constraint as follows:
alter table table_name drop constraint DF_table_name_col_name
Thanks,
Jay
<If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>- Proposed as answer by Qiuyun YuMicrosoft contingent staff Wednesday, February 11, 2015 12:27 PM
- Marked as answer by Lydia ZhangMicrosoft contingent staff Tuesday, February 17, 2015 9:11 AM
Wednesday, February 11, 2015 5:58 AM -
>>>>After doing this, the table is altered with the new column and updated with the new column values.
This has been changed in SQL Server 2012... A operation is very fast,
Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
SQL Server Integration Services: Business Intelligence
Wednesday, February 11, 2015 6:46 AMAnswerer -
Thanks that was helpful.Friday, February 13, 2015 12:00 AM