db2表字段修改
1:删除字段非空属性
alter table XXX alter column XXX drop not null
2:添加字段非空属性
alter table XXX alter column XXX set not null
3:添加一个新字段
alter table XXX add column XXXX varchar(100)
4:删除一个字段
alter table XXX drop column XXX
5:增加字段的长度
alter table XXX alter column XXX set data type varchar(100)
注意:
1:不允许修改字段的名称(只能先删除,再添加)。
2:不允许减小字段的长度。
3:不允许修改字段类型(如把 Integer 修改成 varchar)。
4:如果必须修改上三条中的情况,只能重新建表(第一条有简单方法)。
- 1.更改类型(设置为主键的列不能更改类型)
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" SET DATA TYPE VARCHAR(32);
- 2.更改默认值
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" SET DEFAULT 'ABC';
- 系统默认值:
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" SET DEFAULT; --设置默认值
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" DROP DEFAULT; --删除默认值
- 3.更改是否允许空值
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" SET NOT NULL;
- ALTER TABLE "SCHEMA"."TABLENAME" ALTER COLUMN "COL" DROP NOT NULL;
- 更改列类型,是否允许空值后,需要执行REORG TABLE "TABLENAME";
- 更改默认值后,通常需要执行UPDATE "SCHEMA"."TABLENAME" SET "COL" = DEFAULT WHERE "COL" IS NULL;