1,先添加一个字段,设置为自动增一,比如叫id2 2,select id,id2 into abc from yourtablename--保存关系; 3,先删除这些关联的约束; 4,删除刚才建立的id2,删除id,然后重新建立id自动增一, 5,更新子表:update 子表 set id=t2.id2 from 子表t1, abc t2 where t1.id=t2.id
alter table 主表 drop constraint 外键约名束 go alter table 主表 drop constraint 主键约名束 go alter table 主表 add tmpid identity(1,1) int,not null go update b set b.id=a.tmpid from 主表 a,从表 b where a.id=b.id go alter table 主表 alter column id identity(1,1) int not null go alter table 主表 add constraint pri_id primary key(id) go alter table 从表 add constraint for_id foreign key(id) references 从表(id) go alter table 主表 drop column tmpid go
alter table 从表 drop constraint 外键约名束 go alter table 主表 drop constraint 主键约名束 go alter table 主表 add tmpid identity(1,1) int,not null go update b set b.id=a.tmpid from 主表 a,从表 b where a.id=b.id go alter table 主表 alter column id identity(1,1) int not null go alter table 主表 add constraint pri_id primary key(id) go alter table 从表 add constraint for_id foreign key(id) references 从表(id) go alter table 主表 drop column tmpid go
2,select id,id2 into abc from yourtablename--保存关系;
3,先删除这些关联的约束;
4,删除刚才建立的id2,删除id,然后重新建立id自动增一,
5,更新子表:update 子表 set id=t2.id2 from 子表t1, abc t2 where t1.id=t2.id
有一个自递增列,还能加一个这样的列吗?
drop constraint 外键约名束
go
alter table 主表
drop constraint 主键约名束
go
alter table 主表
add tmpid identity(1,1) int,not null
go
update b set b.id=a.tmpid from 主表 a,从表 b where a.id=b.id
go
alter table 主表
alter column id identity(1,1) int not null
go
alter table 主表
add constraint pri_id primary key(id)
go
alter table 从表
add constraint for_id foreign key(id) references 从表(id)
go
alter table 主表
drop column tmpid
go
drop constraint 外键约名束
go
alter table 主表
drop constraint 主键约名束
go
alter table 主表
add tmpid identity(1,1) int,not null
go
update b set b.id=a.tmpid from 主表 a,从表 b where a.id=b.id
go
alter table 主表
alter column id identity(1,1) int not null
go
alter table 主表
add constraint pri_id primary key(id)
go
alter table 从表
add constraint for_id foreign key(id) references 从表(id)
go
alter table 主表
drop column tmpid
go