如何在SQL Server中将nullable列更改为非null,默认值为0
问题描述:
我有一个整数和可空的列。如何将其更改为非null并将默认值更改为0.
I have a column in which is integer and nullable.How can I change it to not null and default value as 0 .
答
http://stackoverflow.com/questions/3186760/sql-server-change-a-nullable-column-to-not-null-with-default-value [ ^ ]
右键单击表名 - > 修改
现在从列表中选择列,取消选中''允许空值>''
设置默认值,
请参阅列属性默认值或Binding 属性设置为 0
保存...
现在,当用户在插入查询中传递null时,它将使它的值为0
注意:对于已存在于表中的记录不会有影响(不会自动应用0而不是null)
所以,如果你想在现有记录中这样,那么点火更新查询
例如
right click table name -> Modify
now select column from list, uncheck ''Allow Nulls''
for set default value,
see, column properties there is Default value or Binding property set it to 0
Save...
now, when ever user pass null in insert query it will take it''s value 0
Note : for records already exist in table will not be affect (will not apply automatically 0 instead of null)
so, if you want this in existing records then fire update query
e.g.
update tbl1 set col1=0 where col1=Null
快乐编码!
:)
Happy Coding!
:)
在SQL Server中,可以将列的默认值指定为Create Table语句的一部分。如果执行插入但未设置此列的值,则将为指定列设置此默认值。给定示例将此默认值设置为名称列的MyDefaultValue。
示例:
创建表MyTable
(id int not null,
name char(50)default''MyDefaultValue'')
In SQL Server, a column''s default value can be specified as part of the Create Table statement. This default value will be set for the specified column in cases where an insert is performed but this column''s value is not set. The given example sets this default as ''MyDefaultValue'' for name column.
Example:
Create Table MyTable
(id int not null,
name char(50) default ''MyDefaultValue'')