views:

51

answers:

6

Hi,

Can I add a column which is I specify as NOT NULL,I don't want to specify the DEFAULT value but MS-SQL 2005 says:

"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 'test' cannot be added to non-empty table 'shiplist' because it does not satisfy these conditions."

If YES, please let me know the syntax, if No please specify the reason.

+3  A: 

No - SQL Server quite reasonably rejects this, because it wouldn't know what value existing rows should have

It's easy to create a DEFAULT at the same time, and then immediately drop it.

Damien_The_Unbeliever
+5  A: 

Add the column to the table, update the existing rows so none of them are null, and then add a "not null" constraint.

Paul Tomblin
This is a better solution than create/drop the default, if the logic for the new values is more complex than a simple constant.
Damien_The_Unbeliever
Why, or how, is it possibly better to update (manually?) + changing to "not null, instead of "updating" automatically through default + changing to "no default" (dropping default)?
vgv8
@vgv8 - what if the new values in each row depend on other columns already in the row? You can't express that via a default constraint.
Damien_The_Unbeliever
@Damien_The_Unbeliever, see my reply http://stackoverflow.com/questions/3997966/can-i-add-a-not-null-column-without-default-value/4003967#4003967 . I had to provide a variant of am answer in order to gain the reason to engage better formatting
vgv8
+1  A: 

No, you can't. Simply because if you could SQL wouldn't know what to put as value in the already existing records. If you don't have any records in the table it would work by the way. The best way to do this is create the column with a default and then remove the default.

ALTER TABLE dbo.MyTable ADD
MyColumn text NOT NULL CONSTRAINT DF_MyTable_MyColumn DEFAULT 'defaultValue'
ALTER TABLE dbo.MyTable
DROP CONSTRAINT DF_MyTable_MyColumn
Gimly
A: 

No.

Just use empty string '' (in case of character type) or 0 (if numeric), etc as DEFAULT value

abatishchev
A: 

No, you can't, as SQL Server, or any other database engines will force this new column to be null for existing rows into your data table. But since you do not allow a NULL, you are required to provide a default value in order to respect your own constraint. This falls under great sense! The DBE will not extrapolate a value for non-null values for the existing rows.

Will Marcouiller
A: 

@Damien_The_Unbeliever's comment , Is it adding computed column? Neither question nor answer implied anything like that. In case of computed column the error states:

"Only UNIQUE or PRIMARY KEY constraints can be created on computed columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require that computed columns be persisted"

OK, if to continue this guessing game, here is my script illustrating the adding of "NOT NULL" column in one "ALTER TABLE" step:

CREATE TABLE TestInsertComputedColumn 
(
    FirstName VARCHAR(100),
    LastName CHAR(50)
);  

insert into TestInsertComputedColumn(FirstName,LastName)
     select 'v', 'gv8';
select * from TestInsertComputedColumn;

ALTER TABLE TestInsertComputedColumn 
      ADD FullName As FirstName + LastName PERSISTED NOT NULL;

select * from TestInsertComputedColumn;
--drop TABLE TestInsertComputedColumn;
vgv8