可以添加没有 DEFAULT 值的非空列吗

我可以添加一个列,我指定为 NOTNULL,我不想指定 DEFAULT 值,但 MS-SQL 2005说:

ALTER TABLE 只允许添加可以包含空值的列,或者指定了 DEFAULT 定义,或者添加的列是标识或时间戳列,或者,如果以前的条件都不满足,则表必须为空,以允许添加此列。列“ test”不能添加到非空表“ shiplist”中,因为它不满足这些条件。

如果是,请告诉我语法,如果不是,请说明原因。

114421 次浏览

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.

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

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.

No.

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

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.

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

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

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

I used below approach it worked for me

Syntax:
ALTER TABLE <YourTable> ADD <NewColumn> <NewColumnType> NOT NULL DEFAULT <DefaultValue>


Example:
ALTER TABLE Tablename ADD ColumnName datetime NOT NULL DEFAULT GETDATE();

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 <a valid not null values for your column>
GO
ALTER TABLE MY_TABLE ALTER COLUMN STAGE INT NOT NULL
GO