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是主键,当你重新设置种子值后 再次插入新值,会产生主键不唯一错误。
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
先把自增属性去掉.然后 update tb set id = (select count(1) from tb where id < t.id) + 1 from tb t然后再把自增属性加上去.
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 //
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是主键,当你重新设置种子值后
再次插入新值,会产生主键不唯一错误。
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
update tb set id = (select count(1) from tb where id < t.id) + 1 from tb t然后再把自增属性加上去.
我是sql菜鸟 试着运行你的代码但是不知道解决了什么。。
dawugui :
自增属性如何去掉 我id列和其他表有外键约束。。我的表的名字是user 自增列是userid 谁能给我一个一步步做的sql语句 谢谢了
这样做之后确实出现了:再次插入新值,会产生主键不唯一错误。
还有办法吗?
还想问一下 我数据库ldf文件 300多m 我看了一下 就user表userid自增到70多万 其他表数据不多 那么ldf这么是不是一定和这个有关系呢
谢谢 向大家学习 希望了解的赐教
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
//
这样做之后确实出现了:再次插入新值,会产生主键不唯一错误。
还有办法吗?
truncate 原来的表 TB
去掉identity 属性
在用SELECT * INTO tb from #tb
引用 3 楼 innolux_08 的回复:
重新建个表,再把数据导入进去
要不先处理一下数据