SQL语句如何修改一个表的一个字段为自动增长列

SQL语句如何修改一个表的一个字段为自动增长列... SQL语句如何修改一个表的一个字段为自动增长列 展开
 我来答
Scorpio丶莲
2018-03-31 · TA获得超过7585个赞
知道小有建树答主
回答量:80
采纳率:100%
帮助的人:2.2万
展开全部

如果该字段不是主键,需要先设置该字段为主键:

alter table 表名 add primary key(字段名);

修改字段为自动增长

alter table 表名 change 字段名 字段名 字段类型 auto_increment;

select 自增列=identity(int,1,1),* into #tb from tableName

drop table tabelNameselect * into tableName from #tbdrop table #tb 其实可以直接在数据库中修改表的结构,增加一列(就是内容递增的那列),把这列设为标识列,自动递增1。保存一下就行了。

在sql2000中可以这样,不过感觉不怎么好...如果表中关系多了,不建议这样用if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_setid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[p_setid]

GO

--将表中的某个字段转换成标识字段,并保留原来的值

--注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建

--调用示例

exec p_setid '表名','要转换的字段名'

--*/

CREATE PROC P_SETID

@tbname sysname, --要处理的表名

@fdname sysname --要转换为标识字段的字段名

as

declare @s1 varchar(8000),@s2 varchar(8000),@tmptb sysname

select @s1='',@s2='',@tmptb='[tmp_'+@tbname+'_bak]'

select @s1=@s1+',['+name+']'

+case name when @fdname then '=identity(bigint,1,1)' else '' end

,@s2=@s2+',['+name+']'

from syscolumns where object_id(@tbname)=id

select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)

exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']

set identity_insert '+@tmptb+' on

insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']

set identity_insert '+@tmptb+' off

')

exec('drop table ['+@tbname+']')

exec sp_rename @tmptb,@tbname

go

--使用测试

--创建测试的表

create table 表(编号 bigint,姓名 varchar(10))

insert into 表

select 1,'张三'

union all select 2,'李四'

union all select 4,'王五'

go

--调用存储过程,将编号字段改为标识字段

exec p_setid '表','编号'

go

--显示处理结果

select * from 表

--显示是否修改成功

select name from syscolumns

where object_id('表')=id and status=0x80

go

--删除测试

drop table 表

匿名用户
2013-04-08
展开全部
select 自增列=identity(int,1,1),* into #tb from tableName
drop table tabelNameselect * into tableName from #tbdrop table #tb 你这其实可以直接在数据库中修改表的结构,增加一列(就是内容递增的那列),把这列设为标识列,自动递增1。保存一下就行了在sql2000中可以这样,不过感觉不怎么好...如果表中关系多了,不建议这样用if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_setid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_setid]
GO
--将表中的某个字段转换成标识字段,并保留原来的值
--注意,因为要删除原表,所以,如果表和其他表的关联,这些关联要重新创建
--调用示例
exec p_setid '表名','要转换的字段名'
--*/
CREATE PROC P_SETID
@tbname sysname, --要处理的表名
@fdname sysname --要转换为标识字段的字段名
as
declare @s1 varchar(8000),@s2 varchar(8000),@tmptb sysname
select @s1='',@s2='',@tmptb='[tmp_'+@tbname+'_bak]'
select @s1=@s1+',['+name+']'
+case name when @fdname then '=identity(bigint,1,1)' else '' end
,@s2=@s2+',['+name+']'
from syscolumns where object_id(@tbname)=id
select @s1=substring(@s1,2,8000),@s2=substring(@s2,2,8000)
exec('select top 0 '+@s1+' into '+@tmptb+' from ['+@tbname+']
set identity_insert '+@tmptb+' on
insert into '+@tmptb+'('+@s2+') select '+@s2+' from ['+@tbname+']
set identity_insert '+@tmptb+' off
')
exec('drop table ['+@tbname+']')
exec sp_rename @tmptb,@tbname
go

--使用测试

--创建测试的表
create table 表(编号 bigint,姓名 varchar(10))
insert into 表
select 1,'张三'
union all select 2,'李四'
union all select 4,'王五'
go

--调用存储过程,将编号字段改为标识字段
exec p_setid '表','编号'
go

--显示处理结果
select * from 表

--显示是否修改成功
select name from syscolumns
where object_id('表')=id and status=0x80
go

--删除测试
drop table 表
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友8d47e42
2017-05-15 · TA获得超过142个赞
知道答主
回答量:79
采纳率:63%
帮助的人:14.9万
展开全部
1. 如果该字段不是主键,需要先设置该字段为主键:
alter table 表名 add primary key(字段名);
2. 修改字段为自动增长
alter table 表名 change 字段名 字段名 字段类型 auto_increment;
下面是完整例子:
1.create table student(id int);
2.alter table student add primary key(id);
3.alter table student change id id int auto_increment;
mysql测试过,可以这样做,其他数据库应该也可以。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-04-08
展开全部
改为自增列,应该还没有数据吧?否则应该也没办法改了。我觉得简单些可以删了再加,呵呵alter table aaa drop column b
alter table aaa add b int identity(1,1)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Sylvia4_Ever
2019-10-16 · TA获得超过155个赞
知道答主
回答量:84
采纳率:66%
帮助的人:19.3万
展开全部

用企业管理器,把列属性的标识规范改为‘是’

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式