首页 / 智能数码 / 正文

sql给字段添加默认值

时间:2025-02-07 01:02:05

SQL server中对字段设置默认的方法

在SQL server中有三种方法可以设置默认值:

1. 在建表时设置默认值:create table test_table1( id int, name varchar(10), stamp datetime DEFAULT (getdate()))--建表的时候就设置默认值 select * from test_table1 insert into test_table1 (id, name) values (1, '张三') select * from test_table1

2. 对已有的字段设置默认值:create table test_table2( id int, name varchar(10), stamp datetime) select * from test_table2 --增加约束 ALTER TABLE test_table2 ADD CONSTRAINT test_table2_stamp DEFAULT (getdate()) FOR stamp insert into test_table2 (id, name) values (2, '李四') select * from test_table2

3. 增加字段并设置默认值:create table test_table3( id int, name varchar(10)) select * from test_table3 ALTER TABLE test_table3 ADD stamp datetime DEFAULT getdate() insert into test_table3 (id, name) values (3, '王五') select * from test_table3

《sql给字段添加默认值》不代表本网站观点,如有侵权请联系我们删除

广州她氧信息科技有限公司 她氧网版权所有 粤ICP备2023058637号网站地图 网站地图2