SQL语句增加字段、修改字段、修改类型、修改默认值

一、修改字段默认值

alter table 表名 drop constraint 约束名字   ------说明:删除表的字段的原有约束

alter table 表名 add constraint 约束名字 DEFAULT 默认值 for 字段名称 -------说明:添加一个表的字段的约束并指定默认值

二、修改字段名:

alter table 表名 rename column A to B

三、修改字段类型:

alter table 表名 alter column UnitPrice decimal(18, 4) not null 

三、修改增加字段:

alter table 表名 ADD 字段 类型 NOT NULL Default 0

 

1.   删除主键:

Declare @Pk varChar(100);

Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('abcd') and xtype='PK';

if @Pk is not null

begin

     exec('Alter table abcd Drop '+ @Pk)  --删除原主键

 end

 

2. 把所有主键设为不能为空

alter table abcd alter column c char(10) not null

ex: alter table BCM300T alter column ZIP_CODE NVARchar(6) not null

3.  重建主键:

ALTER Table abcd ADD CONSTRAINT pk_abcd   PRIMARY KEY (a, b, c )

ex: ALTER TABLE BCM300T ADD CONSTRAINT BCM300T_IDX01 PRIMARY KEY (ZIP_CODE, ZIP_NAME)

posted @ 2018-08-24 12:33  ^ν^向上人生^ν^  阅读(480)  评论(0编辑  收藏  举报