Can I add a not null column without DEFAULT value

2019-01-22 10:40发布

问题:

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.

回答1:

No, you can't.

Because if you could, SQL wouldn't know what to put as value in the already existing records. If you didn't have any records in the table it would work without issues.

The simplest 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

Another alternative would be to add the column without the constraint, fill the values for all cells and add the constraint.



回答2:

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



回答3:

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.



回答4:

I use this approach to insert NOT NULL column without default value

ALTER TABLE [Table] ADD [Column] INT NULL
GO
UPDATE [Table] SET [Column] = <default_value>
ALTER TABLE [Table] ALTER COLUMN [Column] INT NOT NULL


回答5:

No you cannot. But you can consider to specify the default value to ('')



回答6:

No.

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



回答7:

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.



回答8:

@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;