码迷,mamicode.com
首页 > 数据库 > 详细

SQL更改表字段为自增标识

时间:2018-05-13 01:11:15      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:sql   自增   ble   select   .com   har   varchar   prim   主键   

--删除主键约束
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

SQL更改表字段为自增标识

标签:sql   自增   ble   select   .com   har   varchar   prim   主键   

原文地址:https://www.cnblogs.com/denny-x/p/9030681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!