sql server的小问题
原先在表中删除了的一条数据,现在要重新添加回那张表中。可是主键不是连续的了,怎样通过sql语句来设置主键跟删除之前那样使得主键ID连续呢?先谢过各位啦! SQL?Server
[解决办法]
create table lee
( id int identity(1,1) not null,
name varchar(10)
constraint pk_lee primary key(id)
)
insert into lee(name)
select 'aa' union all
select 'bb' union all
select 'cc' union all
select 'dd' union all
select 'ee'
select id,name from lee
/*
id name
----------- ----------
1 aa
2 bb
3 cc
4 dd
5 ee
(5 row(s) affected)
*/
--原先在表中删除了的一条数据
delete from lee where id=3
--现在要重新添加回那张表中
insert into lee(name) select 'cc'
--可是主键不是连续的了
select id,name from lee
/*
id name
----------- ----------
1 aa
2 bb
4 dd
5 ee
6 cc
(5 row(s) affected)
*/
--主键跟删除之前那样使得主键ID连续
delete from lee where id=6 --> 删除测试记录
set identity_insert lee on
insert into lee(id,name) select 3,'cc'
set identity_insert lee off
select id,name from lee
/*
id name
----------- ----------
1 aa
2 bb
3 cc
4 dd
5 ee
(5 row(s) affected)
*/