求SQL语句删除表格中的一行后序号也自动调整了。在线等!

时间:2022-01-02 10:29:49
我用如下SQL语句创建了zcxIncome表格,并插入数据,
cmd.CommandText = string.Format("create table zcxIncome( Num int identity(1,1) primary key,Mode varchar(50)");
cmd.CommandText = string.Format("insert into zcxIncome (Mode) values('{0}')",Mode);

其中Num列是序号,自动加1,均正常

现在我想用如下SQL语句从该表格中删除一行数据,
cmd.CommandText = string.Format("delete from zcxIncome where Num={0}", DeleteNum);

删除也正常,但Num序号列没有自动调整成“1,2,3.....”,比如说我删掉了序号为3的行,按以上SQL语句,表内的序号没有自动调整,为“1,2,4.....”,我想能够自动调整“1,2,3.....”。

应如何写相关SQL语句?

在线等!

8 个解决方案

#1


identity(1,1)是不能达到您想要的效果的,想实现这种效果可以用触发器或计算列

#2


引用 1 楼 janely318 的回复:
identity(1,1)是不能达到您想要的效果的,想实现这种效果可以用触发器或计算列

洛洛说的没错

#3


如果使用自增列,无法实现你的需求.
可以在查询中来实现排名的效果.
例如:select * , px = (select count(1) from tb where id < t.id) + 1 from tb t
或者2005的话,使用row_number()
select * , px = row_number() over(order by id) from tb

#4


学习洛洛MM

#5


学习洛洛MM

#6


是啊,用identity(1,1),无法更新标识列 'Num',手工修改都不让。

#7


我也想问这个问题,什么触发器,能不能通过 简单的sql语句实现

#8


我的表中没有identity(1,1),,但也不能达到那种效果啊!

#1


identity(1,1)是不能达到您想要的效果的,想实现这种效果可以用触发器或计算列

#2


引用 1 楼 janely318 的回复:
identity(1,1)是不能达到您想要的效果的,想实现这种效果可以用触发器或计算列

洛洛说的没错

#3


如果使用自增列,无法实现你的需求.
可以在查询中来实现排名的效果.
例如:select * , px = (select count(1) from tb where id < t.id) + 1 from tb t
或者2005的话,使用row_number()
select * , px = row_number() over(order by id) from tb

#4


学习洛洛MM

#5


学习洛洛MM

#6


是啊,用identity(1,1),无法更新标识列 'Num',手工修改都不让。

#7


我也想问这个问题,什么触发器,能不能通过 简单的sql语句实现

#8


我的表中没有identity(1,1),,但也不能达到那种效果啊!