为什么会写这篇文章呢?
主要是因为公司要求对数据库的更改要能提供sql更新脚步,以方便以后查阅。
通过sqlServer的设计界面对字段更改成标识很方便,但是用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
--删除主键字段
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。