<!--设为主键-->
alter table DataTest add primary key (Id);
--删除主键
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'[Test]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
EXEC ('alter table Test drop constraint '+@Constraint_Name )
print(@Constraint_Name)
<!--插入列-->
alter table DataTest add UserPwd nvarchar(20) null;
<!--删除列 -->
ALTER TABLE DataTest drop column UserPwd;
<!--修改列UserPwd改为PassWord-->
ALTER TABLE DataTest ALTER column UserPwd PassWord ;
<!--修改字段类型和范围-->
ALTER TABLE DataTest ALTER COLUMN UserName nvarchar(30);
<!--给表改名字 给一个表重命名:-->
ALTER TABLE DataTest alter Test;
--修改表名
exec sp_rename 'DataTest','Test'