Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Emad Mohammed said abdalla , ERP & IT Software, operation general manager . , AL DOHA Company
Date de publication: 2014/12/25
Vinod Jetley
par 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
par 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