how to add not null column in existing table and then insert values in that column ?? in sql...
1 Answer
If you want to add a NOT NULL column you must specify a DEFAULT:
ALTER TABLE YourTable
ADD SomeColumn INT NOT NULL
CONSTRAINT DF_YourTable_SomeColumn DEFAULT(0);
Other possibility is to add it with NULL, add your data and alter it to NOT NULL later (see ALTER TABLE)
EDIT: Your comment about "how to insert values"...
This depends very much in your needs. If you want to set all rows to the same value it is:
UPDATE YourTable SET SomeColumn=0;
4 Comments
Rahul Sirohi
then how to insert values in that column ??
Gottfried Lesigang
@RahulSirohi, edited my answer... This is absolute base knowledge actually. So please use one of the millions of tutorials you'll find out there...
Rahul Sirohi
if i want to add different values ?
Gottfried Lesigang
@RahulSirohi, really... How should I answer this without any knowledge about your data table and its content??? If there is a
unique key (might be a primary key) you can set each row separately with UPDATE YourTable SET SomeColumn=aValue WHERE TheKey=123;
DEFAULT...