sql中怎么设置默认值

sql中怎么设置默认值,第1张

1、首先新建一个学生表:student,需求:字段password的默认值是1213142。

2、如果正漏神常插入值,需要手动添加密码,但是如果有大量的字段可以使用默认值,那么语句会显得有点臃肿。

3、现在我们设置字段password默认值,如图所示,实际上是创建了一个约束,所以图中的 df_pwd_stu 表示的是约束的名称,前搜岁这时我们自慧睁定义的。

4、再次插入,这时可以忽略我们已经设置默认值字段的值了,因为插入的时候回数据库会帮我们插入默认值。完成效果图。

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:当开始具体创建约束时,要对表中现有的数据进行检查。如果现有数据符合约束的定义,则约束被成功加入到表中源码天空


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11981612.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存