日期:2014-05-17 浏览次数:20787 次
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)
*/