用SQL修改主键约束

--修改主键约束用SQL
--获取主键约束名字
declare @csname   varchar(100)  
set @csname=''  
   
select @csname=name
FROM sysobjects
WHERE xtype='PK' AND parent_obj=object_id('表名') 
   
--删除约束
exec('alter   table   表名  drop   constraint ' + @csname)  
   
--禁用约束(不校验)
exec('alter   table   表名  nocheck   constraint '   + @csname)  
   
--启用约束(校验)
exec('alter   table   表名  check   constraint '   + @csname)  
--添加约束
alter   table   表名  add   constraint   主键约束名  primary   key   (列名)
例: if not exists ( SELECT *
                from syscolumns
                where   id = object_id('accPF_RefFAcctID')
                        and name = 'id')
        ALTER TABLE accPF_RefFAcctID
        ADD  id INT IDENTITY(1,1) CONSTRAINT PK_accPF_RefFAcctID PRIMARY KEY (id)

posted on 2009-07-27 16:22  夏日之花  阅读(3587)  评论(0)    收藏  举报

导航