Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Explain how to add a NOT NULL column to a table.

user-image
Question added by Emad Mohammed said abdalla , ERP & IT Software, operation general manager . , AL DOHA Company
Date Posted: 2014/12/25
Vinod Jetley
by Vinod Jetley , Assistant General Manager , State Bank of India

As an option you can initially create Null-able column, then update your table column with valid not null values and finally ALTER column to set NOT NULL constraint:

 

ALTER TABLE MY_TABLE ADD STAGE INT NULL

GO

UPDATE MY_TABLE <set valid not null values for your column>

GO

ALTER TABLE MY_TABLE ALTER COLUMN STAGE INT NOT NULL

GO

Another option is to specify correct default value for your column:

 

ALTER TABLE MY_TABLE ADD STAGE INT NOT NULL DEFAULT '0'

Emad Mohammed said abdalla
by Emad Mohammed said abdalla , ERP & IT Software, operation general manager . , AL DOHA Company

First, add the column as NULL. Then use UPDATE to add data to every row. Finally use an ALTER TABLE . . . ALTER COLUMN statement to change the column constraint to NOT NULL.

More Questions Like This