create table abcd
(
a char(10) not null,
b char(10) not null primary key(a,b),
c char(10) null,
d char(10) null
)
Declare @Pk varChar(100); -- 声明 局部变量 PK
Select @Pk=Name from sysobjects where Parent_Obj= OBJECT_ID('abcd') and xtype='PK'; -- Parent_Obj 代表父级id为abcd 且 参数是主键约束
print @Pk -- 输出Pk变量的值
if @Pk is not null
begin
exec('Alter table abcd Drop '+ @Pk) --执行动态语句删除原主键
end
alter table abcd
alter column c char(10) not null
alter table abcd add constraint pk_abcd PRIMARY KEY (a, b, c )
SELECT * FROM sysobjects where xtype = 'V' -- 列出视图
本文需要掌握基本SQL语法