SQL数据库问题,高手进~!

来源:百度知道 编辑:UC知道 时间:2024/06/25 01:10:52
ID列会自动增加,因为中间我删除了两行,所以在增加新数据时,ID列的序号就不连续了.我不想它不连续,怎么办?

方法1:用SET IDENTITY_INSERT 设置

方法2:
create table #T(ID int identity,Name nvarchar(10))
if object_id('Tempdb..#') is not null
drop table #
Select Name into # from #T--排除重复记录结果集生成临时表#

truncate table #T--清空表

insert #T select * from # --把临时表#插入到表#T中

要用删除触发器实现.
create table tb_test(id int)
insert into tb_test(id)
select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10

create trigger td_tb_test on tb_test
for delete
as
update tb_test
set id=(select count(*) from tb_test t1 where t1.id<tb_test.id)+1
from tb_test