使用SQL語句,可以輕松修改SQL主鍵,下面就將為您介紹這種修改SQL主鍵的方法,供您參考,希望對(duì)您學(xué)習(xí)修改SQL主鍵方面有所啟迪。
- declare @defname varchar(100)
- declare @cmd varchar(500)
- declare @tablename varchar(100)
- declare @keyname varchar(100)
- Set @tablename='Temp1'
- Set @keyname='id' --需要設(shè)置的key,分隔
- select @defname= name
- FROM sysobjects so
- JOIN sysconstraints sc
- ON so.id = sc.constid
- WHERE object_name(so.parent_obj) = @tablename
- and xtype='PK'
- if @defname is not null
- begin
- select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname
- --print @cmd
- exec (@cmd)
- end
- else
- set @defname='PK_'+@keyname
- select @cmd='alter table '+ @tablename+ ' ADD constraint '+ @defname +' PRIMARY KEY CLUSTERED('+@keyname+')'
- exec (@cmd)