Adding NOT NULL column to an exisitng table

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 351
Assume that a table have the data and now you needed to add one more new column to that table with NOT NULL constraint....

ALTER TABLE Employee ADD Salary INT NOT NULL 
GO


This results the below error:

Msg 4901, Level 16, State 1, Line 1
ALTER TABLE only allows columns to be added that can contain nulls,
or have a DEFAULT definition specified , or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'Salary' cannot be added to non-empty table 'Employee' because it does not satisfy these conditions.



To overcome this error, you need to add DEFAULT constraint while adding new column with NOT NULL constaint to an existing table

ALTER TABLE Employee ADD Salary1 INT NOT NULL DEFAULT 100
GO

Comments or Responses

Login to post response