SQL更改表字段为自增标识


下面是SQL语句:

--删除主键约束
DECLARE @Constraint_Name varchar (200) 
select @Constraint_Name = Name from dbo.sysobjects

where Xtype = 'PK' and Parent_Obj =

(select [ID] from dbo.sysobjects

where id = object_id(N'[表名称]')

and OBJECTPROPERTY(id, N'IsUserTable') = 1) 
if @Constraint_Name <> ''
begin
alter table 表名称 drop constraint @Constraint_Name
end
--删除主键字段
-- 何问起 hovertree.com 

declare @count int

select @count = count(*) from sysobjects a,syscolumns b

where a.id = b.id and b.name = '主键字段名' and a.Name = '表名称'
if @count > 0
begin
alter table 表名称 drop column 主键字段名 
end

--创建字段,主键,并自增
alter table 表名称 add 主键字段名 int identity(1,1) PRIMARY KEY

推荐:http://www.cnblogs.com/roucheng/p/mssqlindex.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM