SQL server主键自增(使用SQL方式),根据时间段查询记录

换了公司,数据库使用SQL server的
记录一下,顺便帮忙

1.设置自增列

#自增列不能直接修改,必须将原有ID列删除,然后重新添加一列具有identity属性的ID字段。
#比如你要修改的字段名为id:

注:这只适用于刚建完表的情况,如果此时主键已经使用过了,表中存在许多数据,不能使用该方法删除主键,会导致数据丢失。(可行的方法,建一张相同的表来存储数据,在修改,插入)。
alter table tb drop column id
alter table tb add id int identity(1,1)

2.设置主键

alter table tb alter column id int not null
alter table tb add constraint pkid primary key (id)

3.根据时间段选择记录

select * from tb where input_date between ‘2019-12-20 00:00:00’ and ‘2019-12-20 23:59:59’

猜你喜欢

转载自blog.csdn.net/pz641/article/details/103660001