自动编号

wolflove23 2008-02-24 11:16:07
有一张表,第一个字段为id 自动编号,主键
由于经常有删除操作,现在的编号断层比较厉害。怎么样才能重新设置,让他从1开始一直连续编下去。
...全文
141 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hui_hui_2007 2008-02-25
  • 打赏
  • 举报
回复
设计有问题,每删除一行,其余所有编号都变,效率太低了。
比如1-10000行,
你删除第2行,则后面99998行都要修改,所以说设计不合理。
也没有意义。
xgcom 2008-02-25
  • 打赏
  • 举报
回复
既然要常常删除,设计时就不要用自动编号。
中国风 2008-02-24
  • 打赏
  • 举报
回复
可以用触发器实现,这样数据量一大,每删除前一个,后面的都重排效率很低
wolflove23 2008-02-24
  • 打赏
  • 举报
回复
我的方法不用程序,把ID 主键删除再重新设置就OK 了
dawugui 2008-02-24
  • 打赏
  • 举报
回复

一个按字段解决断号的处理

我现在要实现这样一个业务,表是这样的:
列名 : Min Max
1 5
6 10
11 15
20 30
40 50
51 60
我要得到最终的结果是:
1 15
20 30
40 60
也就是说,只要中间是连续的,就不显示,而只显示起始和终止.
谢谢了,大家帮个忙!!


if object_id('pubs..tb') is not null
drop table tb
go

create table tb
(
min int,
max int
)
insert into tb(min,max) values(1,5)
insert into tb(min,max) values(6,10)
insert into tb(min,max) values(11,15)
insert into tb(min,max) values(20,30)
insert into tb(min,max) values(40,50)
insert into tb(min,max) values(51,60)
go

if object_id('pubs..test') is not null
drop table test
go
select id=identity(int,1,1),* into test from tb
delete from tb --你可以自己再创建一个和TB同样的表
go

declare @cnt1 as int --min
declare @cnt2 as int --max
declare @cnt as int --循环变量
declare @cntM as int --中间值
declare @cntT as int --记录总数
set @cnt = 1
select @cntT = count(*) from test
while @cnt <= @cntT
begin
if @cnt = 1 --第一条记录
begin
select @cnt1 = min from test where id = @cnt
select @cnt2 = max from test where id = @cnt
end
else
if @cnt = @cntT --最后一条记录
begin
select @cnt2 = max from test where id = @cnt
insert into tb values(@cnt1 , @cnt2)
end
else
begin
select @cntM = min from test where id = @cnt
if @cntM <> @cnt2 + 1
begin
insert into tb values(@cnt1 , @cnt2)
select @cnt1 = min from test where id = @cnt
select @cnt2 = max from test where id = @cnt
end
else
begin
select @cnt2 = max from test where id = @cnt
end
end
set @cnt = @cnt + 1
end
select * from tb
drop table tb
drop table test

min max
----------- -----------
1 15
20 30
40 60

(所影响的行数为 3 行)










create table test
(
[min] int,
[max] int
)

insert into test select 1, 5
insert into test select 6, 10
insert into test select 11, 15
insert into test select 20, 30
insert into test select 40, 50
insert into test select 51, 60


--语句
select [min],[max] = (select min([max]) from test t1
where [max]>t2.[min] and not exists(select 1 from test where [min] = t1.[max]+1)
)
from test t2
where not exists(select 1 from test where [max] = t2.[min]-1)

--结果
1 15
20 30
40 60


中国风 2008-02-24
  • 打赏
  • 举报
回复
实现方法:
http://blog.csdn.net/roy_88/archive/2006/12/01/1424370.aspx
dawugui 2008-02-24
  • 打赏
  • 举报
回复
断号的开始号
set nocount on
declare @t table(a int,b char(6),flag int)
insert @t select 1,'one',NULL
insert @t select 2,'two',NULL
insert @t select 3,'three',NULL
insert @t select 5,'four',NULL
insert @t select 6,'five',NULL
insert @t select 10,'six',NULL
insert @t select 11,'seven',NULL

select *
from @t aa
where (a - (select max(a) from @t where aa.a > a)) > 1
/*

a b flag
----------- ------ -----------
5 four NULL
10 six NULL
*/
dawugui 2008-02-24
  • 打赏
  • 举报
回复
这个,得自己编写了.

每次自己找到断号,自己补充.
victorcai2006 2008-02-24
  • 打赏
  • 举报
回复
MARK

但是觉得这样实现的意义不大啊

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧