且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除除最后一行以外的任何行后,如何对自动递增的列值进行重新排序?

更新时间:2023-08-28 16:19:34

让您的表命名为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.