改变字段的自动递增属性

zdx_little_bear 2009-08-11 11:39:34
已有一个表 里面有数据
ID号不是自动递增的切已有数据中没有重复的ID
把ID好变成自动递增的 怎么完成?

...全文
81 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄_瓜 2009-08-11
  • 打赏
  • 举报
回复
--第一步 删除原来的id
alter table ta drop column id
--第二步 增加一个自增的id
alter table ta add id int identity(1,1) primary key
feixianxxx 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ws_hgo 的回复:]
SQL code--(4)identity列与普通列的相互转换
---<1>indetity列转换为普通列createtable #tg
(
idintidentity(1,1),
col1int
)insertinto #tgselect1unionallselect2unionallselect3altertable #tgadd col2intupdate #¡­
[/Quote]

if object_id('ta')is not null
drop table ta
go
create table ta(id int ,nam int)
go
insert ta select
1,3 union all select
3,3 union all select
4,3 union all select
7,3 union all select
8,3 union all select
9,3 union all select
10,3 union all select
11,3 union all select
12,3
go
alter table ta add idd int identity(1,1)
alter table ta drop column id
exec sp_rename 'ta.idd','id'

select * from ta
/*
nam id
----------- -----------
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9

*/

数据借用
华夏小卒 2009-08-11
  • 打赏
  • 举报
回复

if object_id('ta')is not null drop table ta
go
create table ta(id int ,nam int)
go
insert ta select
1,3 union all select
3,3 union all select
4,3 union all select
7,3 union all select
8,3 union all select
9,3 union all select
12,3 union all select
15,3 union all select
16,3
go

declare @n int
set @n=0

alter table ta add nm int --临时增加1列

update ta set @n=1+@n,id=@n

alter table ta drop column nm --删除临时列

select * from ta
id nam
----------- -----------
1 3
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3

(9 行受影响)

ws_hgo 2009-08-11
  • 打赏
  • 举报
回复
http://blog.csdn.net/ws_hgo/archive/2009/02/06/3866850.aspx
ws_hgo 2009-08-11
  • 打赏
  • 举报
回复
--(4)identity列与普通列的相互转换   
---<1>indetity列转换为普通列
create table #tg
(
id int identity(1,1),
col1 int
)
insert into #tg select 1
union all select 2
union all select 3

alter table #tg
add col2 int
update #tg set col2=id
select * from #tg
alter table #tg
drop column id
select * from #tg
---<2>将普通列转换为identity列
create table #TY
(
col1 int,
col2 int
)
insert into #TY select 1,1
union all select 2,2
union all select 13,53
union all select 24,44

--铺助表
create table #TT
(
id int identity(1,1),
col1 int
)

set identity_insert #TT off
insert into #TT (id,col1) select col1,col2 from #TY
select * from #TT
insert into #TT select 46


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/02/06/3866850.aspx
宸路 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dzxccsu 的回复:]
删除ID,重新建立!
[/Quote]
alter table 表
drop column [ID]
alter table 表
add [ID] int identity(1,1)
数据不会丢失
可以先试试
SQL77 2009-08-11
  • 打赏
  • 举报
回复
先将ID列删除,再设置自增列IDENTITY
等待戈多12 2009-08-11
  • 打赏
  • 举报
回复
set identity(100,1)意思是从第100开始,增量为一,表中实际有多少行就从多少行开始。
或者删除ID列再重建,设为自动增长也可以
soft_wsx 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dzxccsu 的回复:]
删除ID,重新建立!
[/Quote]
是的
如果不是主健的话,在企业管理器设置计把ID删除,再增加一列,然后用update更新表即可
yyhust2 2009-08-11
  • 打赏
  • 举报
回复
ID设置主键,用客户端完成自动递增。
dzxccsu 2009-08-11
  • 打赏
  • 举报
回复
删除ID,重新建立!

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧