SQL更改表字段为自增标识

下面是SQL语句:推荐:http://www.cnblogs.com/roucheng/p/mssqlindex.html

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

下面是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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120459.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号