您的位置:首页 > 数据库

SQL语句修改字段默认值

2009-11-18 16:14 363 查看
一、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:当开始具体创建约束时,要对表中现有的数据进行检查。如果现有数据符合约束的定义,则约束被成功加入到表中,然而,如果有任何数据不能通过约束验证,则不会把约束应用到数据库中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息