代码改变世界

更改一个主键的列的类型的步骤

2011-08-04 11:58  鹏雕  阅读(205)  评论(0编辑  收藏  举报

--------------更改一个主键的列的类型的步骤------------------------------------

 例如:修改表GBTerminal.F97列xh类型为smallint --删除默认值
alter table GBTerminal.F97 drop constraint DF_F97_xh
go
--删除check
alter table GBTerminal.F97 drop constraint [CK_f97_xh]
go
--删除主键
alter table GBTerminal.F97 drop constraint PK_F97
go
--更改xh列类型为smallint
alter table GBTerminal.F97 alter column xh int NOT NULL
--xh列加默认值
alter table  GBTerminal.F97 add constraint DF_F97_xh default(0) for xh
--xh列加check约束
alter table [GBTerminal].[F97] add  constraint [CK_f97_xh] CHECK (([xh]<=(1024)))
--添加主键PK_F97
alter table  GBTerminal.F97 add constraint PK_F97 primary key(tid,xh)