请问 我数据库是还原的 我删除了一些(n行)无用数据 现在的状态是:id是从70多万开始递增了 我想从1开始 如何做 有人指点一下吗 先谢了

解决方案 »

  1.   

    自增列?用个临时表转过去,truncate 原来的表,再转回来
      

  2.   


    use tempdb
    gocreate table test(id int identity(1,1),col char(1))
    goinsert test select 'a'
    union all select 'b'
    godbcc checkident('test',reseed,0)
    goinsert test select 'c'
    goselect * from test
    go/**
    1 a
    2 b
    1 c
    **/drop table test
    go
    你可以运行一下看效果,不过这样的方法会有些问题,如果你的ID是主键,当你重新设置种子值后
    再次插入新值,会产生主键不唯一错误。
      

  3.   


    use tempdb
    gocreate table test(id int identity(1,1) primary key,col char(1))
    goinsert test select 'a'
    union all select 'b'
    godbcc checkident('test',reseed,0)
    goinsert test select 'c'
    goselect * from test
    go/**Msg 2627, Level 14, State 1, Line 2
    Violation of PRIMARY KEY constraint 'PK__test__03317E3D'. Cannot insert duplicate key in object 'dbo.test'.1 a
    2 b
    **/drop table test
    go
      

  4.   

    先把自增属性去掉.然后
    update tb set id = (select count(1) from tb where id < t.id) + 1 from tb t然后再把自增属性加上去.
      

  5.   

    CN_SQL :
    我是sql菜鸟 试着运行你的代码但是不知道解决了什么。。
    dawugui :
    自增属性如何去掉 我id列和其他表有外键约束。。我的表的名字是user 自增列是userid 谁能给我一个一步步做的sql语句 谢谢了  
      

  6.   

    dbcc checkident('table',reseed,0)
      

  7.   

    dbcc checkident('你的表明',reseed,0)看看联机帮助这个就知道了,就是干这个的
      

  8.   

    dbcc checkident('你的表明',reseed,0) 
    这样做之后确实出现了:再次插入新值,会产生主键不唯一错误。
    还有办法吗?
    还想问一下 我数据库ldf文件 300多m 我看了一下 就user表userid自增到70多万 其他表数据不多 那么ldf这么是不是一定和这个有关系呢
    谢谢 向大家学习 希望了解的赐教
      

  9.   

    还有 我把这个数据库备份成.bak后 再还原 提示媒体集有两个媒体簇但只提供了一个 必须提供所有成员
      

  10.   

    if object_id('aa') is not null
    drop table aa
    go
    create  table  aa (aid int not null primary key, ab char(10))
     insert  aa  select 2,'a' union all
    select 3,'b'
    go
    select * from aa
    go
    create table test (tid int identity(1,1),cc char(10))
    insert test  select ab from aa
    go
    select * from test
    //
      

  11.   

    dbcc checkident('你的表明',reseed,0) 
      

  12.   

    dbcc checkident('你的表明',reseed,0) 
    这样做之后确实出现了:再次插入新值,会产生主键不唯一错误。 
    还有办法吗? 
      

  13.   

    新建个临时表 #tb
    truncate 原来的表 TB
    去掉identity 属性
    在用SELECT * INTO tb from #tb
      

  14.   

    先把主键ID列的主键去掉做好保存,然后再把ID列设为主键做好保存.这就OK了.重新插入数据时就会从1开始了!
      

  15.   

    同意 
    引用 3 楼 innolux_08 的回复:
    重新建个表,再把数据导入进去 
      

  16.   

    dbcc checkident(tablename,reseed,0) 如果上面这个出现主键不唯一错误,再重新将userid设置为主键,自增之类的
    要不先处理一下数据