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_cgoalter
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:当开始具体创建约束时,要对表中现有的数据进行检查。如果现有数据符合约束的定义,则约束被成功加入到表中源码天空
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-02-21
2楼是ORACLE的语法
sqlserver的是
alter
table
students
add
constraint
c_students_sex
default
'男'
for
Ssex
给该列添加约束之前先去掉该列上的约束
相似回答