删除除最后一行以外的任何行后,如何对自动递增的列值进行重新排序? [英] How to reorder auto increment column values, after deleting any row other than last row?

查看:95
本文介绍了删除除最后一行以外的任何行后,如何对自动递增的列值进行重新排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有 asp.net,c#的SQL查询可以重新排列自动递增的列值.

I want to know is there any SQL query for asp.net,c# that can just re arrange auto increment coloumn values..

例如 删除表中的2:

sno

1

2

3

4

是:

sno

1

3

4

但我想重新安排:

sno

1

2

3

注意:

  1. 不想手动编号
  2. 创建表的查询如下:

CREATE TABLE uid (sno int IDENTITY(1,1) PRIMARY KEY, qpname nvarchar(500), mob int, tm int)

推荐答案

让您的表命名为Parent,将保存备份的表称为Backup.它们应具有相同的列.

Let your table be named Parent and table that will hold the backup is called Backup. They should have identical columns.

INSERT INTO dbo.Backup 
    SELECT * FROM dbo.Parent

现在截断父表

TRUNCATE TABLE dbo.Parent

现在,您只需使用第一个命令就可以将数据插入回去,并且只需反转表名即可.

Now you can just insert the data back using the first command and just reversing the table names.

请记住,这可能并非在所有情况下都有效.您可能启用了On delete cascade,如果是这种情况,则将丢失其他表(也引用父表)中的所有数据.我认为您永远不要使用此表,因为您正在使用此表上的任何Foriegn Key参考.

Remember that this may not work in all cases. You may have On delete cascade on and if that is the case, then you would loose all data from other tables also which are referencing the parent table. I think that you should never use this is you are using any Foriegn Key reference on this table.

这篇关于删除除最后一行以外的任何行后,如何对自动递增的列值进行重新排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆