SQL语句修改字段默认值

一、SQL语句修改字段默认值

alter table 表名 drop constraint 约束名字

说明:删除表的字段的原有约束

alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称

说明:添加一个表的字段的约束并指定默认值

go

例:

alter table T_ping drop constraint DF_T_ping_p_c
alter table T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c
go

alter table with check T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c

alter table with nocheck T_ping add constraint DF_T_ping_p_c DEFAULT ((2)) for p_c

两者的区别是If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. This is not recommended except in rare cases. The new constraint will be evaluated in all future updates.

对于要建立约束的两个表,如果其中的一个已有数据,把“在创建时检查现有数据”选项设置为“是”将告诉SQL SERVER:当开始具体创建约束时,要对表中现有的数据进行检查。如果现有数据符合约束的定义,则约束被成功加入到表中,然而,如果有任何数据不能通过约束验证,则不会把约束应用到数据库中。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/henryhappier/archive/2009/11/18/4829242.aspx

原文地址:https://www.cnblogs.com/henryhappier/p/1656150.html