将可为空的列添加到 SQL Server,默认值为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35168586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add nullable column to SQL Server with default value of null
提问by Maryam
I want to add a nullable boolean column to an existing table with default value of null.
我想将一个可为 null 的布尔列添加到默认值为 null 的现有表中。
I have used this bit of script but it does not set the default value to null. it sets it to 0 instead.
我已经使用了这段脚本,但它没有将默认值设置为 null。而是将其设置为 0。
ADD newColumnName BIT NULL
CONSTRAINT DF_tableName_newColumnName DEFAULT(null)
回答by Ted Cohen
I just ran your example code snippet on my SQL Server 2008 R2 instance and then inserted a record. It initialized the column to null, as expected. The next step would be to post the alter statement and the insert statement that you used.
我刚刚在我的 SQL Server 2008 R2 实例上运行了您的示例代码片段,然后插入了一条记录。正如预期的那样,它将该列初始化为 null。下一步是发布您使用的alter 语句和insert 语句。
I used:
我用了:
alter table tmp1 Add newColumnName bit null CONSTRAINT DF_tableName_newColumnName DEFAULT(null)
insert into tmp1(emp_id) values(9999)
select * from tmp1
After running the above, I used SQL Server Management Studio "Design" action to examine the properties of the new column. It showed that the "Default Value or Binding" was indeed (Null) as expected.
运行上述程序后,我使用 SQL Server Management Studio 的“设计”操作来检查新列的属性。它表明“默认值或绑定”确实如预期的那样(空)。