失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > SQL数据库中getDate()函数 修改/添加约束

SQL数据库中getDate()函数 修改/添加约束

时间:2022-05-01 20:01:02

相关推荐

SQL数据库中getDate()函数 修改/添加约束

SQL数据库中getDate()函数作用是获取系统当前时间。

例如

SELECT GETDATA() AS DataTime --可以查看现在的时间

工作中用到的sql语句

ALTER TABLE [dbo].[cdc_test_str] ADD CONSTRAINT [DF_cdc_test_str_operation_time] DEFAULT (getdate()) FOR [operation_time]

--添加默认值约束

alter table 表名 add constraint 约束名 default 约束值 for 列名;

ALTER TABLE [dbo].[cdc_test_str] ADD CONSTRAINT [DF_cdc_test_str_operation_time] DEFAULT (getdate()) FOR [operation_time]

使用T-SQL脚本修改DEFAULT约束

use 数据库名; --使用数据库

go

if exists(select * from sysobjects where name=约束名) --判断要创建的约束是否已存在

alter table 表名 drop constraint 约束名;

go

alter table 表名 add constraint 约束名 default 约束值 for 列名; --添加默认值约束

go

示例:

--使用数据库

use testss;

go

--判断要创建的约束是否已存在

if exists(select * from sysobjects where name='default1')

alter table test1 drop constraint default1;

go

--添加默认值约束

alter table test1 add constraint default2 default 18 for age;

go

如果觉得《SQL数据库中getDate()函数 修改/添加约束》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。