如何将具有默认值的列添加到SQL Server 2000/SQL Server 2005中的现有表中?


当前回答

ALTER TABLE <table name> 
ADD <new column name> <data type> NOT NULL
GO
ALTER TABLE <table name> 
ADD CONSTRAINT <constraint name> DEFAULT <default value> FOR <new column name>
GO

其他回答

ALTER表数据集表名添加列_当前_查找整数默认值0

当要添加的列具有NOT NULL约束,但没有DEFAULT约束(值)时,请注意。在这种情况下,如果表中有任何行,ALTER TABLE语句将失败。解决方案是从新列中删除NOT NULL约束,或为其提供DEFAULT约束。

--Adding Value with Default Value
ALTER TABLE TestTable
ADD ThirdCol INT NOT NULL DEFAULT(0)
GO

SQL Server+更改表+添加列+默认值uniqueidentifier

ALTER TABLE Product 
ADD ReferenceID uniqueidentifier not null 
default (cast(cast(0 as binary) as uniqueidentifier))

例子:

ALTER TABLE [Employees] ADD Seniority int not null default 0 GO