db2 表添加字段及注释操作

一、表字段的添加、修改、删除

1.添加字段

alter table [表名] add [字段名] [column_type]

2.更改字段类型

alter table  [表名] alter column [字段名] set data type  [column_type] 

注意: 更改字段类型是有限制的,如将字段改为比之前类型长度大的可以,如果要改小或者修改小数点长度,必须先drop掉原来的column,然后再重新添加.

例如我要将一个Varchar(10)的column改为Varchar(6) 或者将一个DECIMAL(16, 2)的column改为DECIMAL(16, 4)等,均不能使用上述语句修改,另外改为不同的类型,也需要先drop掉column。

3.去掉字段

alter table [表名] drop column [字段名]

 注意:drop掉字段之后,可能会导致表查询/插入操作不能执行,需要对表进行reorg。 

4.为字段添加默认值

alter table [表名] alter column [字段名] set default [value] 

5. 添加带默认值的字段 

alter table [表名] add column [字段名] [column_type] not null with default [value] 

6. 设置字段默认时间为当前时间 

alter table [表名] alter column [字段名] set default  current date; 

二、注释的添加及修改

1.表注释的添加

comment on table [表名]  is  '注释内容';

2.列注释的添加

comment  on  column  [表名].[字段名]   is  '注释内容';

例如:comment on column  g_toba.my_sex is ' 01-男,02-女';

说明:列注释如果修改修改的话,改变注释内容,重新执行这个语句就行了

猜你喜欢

转载自blog.csdn.net/m0_37392721/article/details/82012735