db2 表新增欄位及注釋操作

2021-10-16 09:27:06 字數 1050 閱讀 3990

新增字段

alter table [表名] add [欄位名] [column_type]

更改字段型別

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。

去掉字段

alter table [表名] drop column [欄位名]

注意:drop掉字段之後,可能會導致表查詢/插入操作不能執行,需要對錶進行reorg。

為字段新增預設值

alter table [表名] alter column [欄位名] set default [value]

新增帶預設值的字段

alter table [表名] add column [欄位名] [column_type] not null with default [value]

設定字段預設時間為當前時間

alter table [表名] alter column [欄位名] set default current date;

表注釋的新增

comment on table [表名] is '注釋內容';

列注釋的新增

comment on column [表名].[欄位名] is '注釋內容';

例如:comment on column g_toba.my_*** is 』 01-男,02-女』;

說明:列注釋如果修改修改的話,改變注釋內容,重新執行這個語句就行了

db2 表操作語句

1 刪除表中的某一列 alter table t req temporary requirementdrop columntemporary requirement type id 2 在表中增加一列 alter table t req temporary requirement add tempo...

DB2資料庫新增 更改字段

db2 表基本新增字段操作 1.新增字段 alter table table name add column name column type 2.更改字段型別 alter table table name alter column column name setdata type column t...

DB2的字段型別

db2的字段型別如下 字段型別 描述字段長度及其預設值 char size 用於儲存定長 size 位元組的字串資料。每行定長 不足部分補為空格 最大長度為254位元組,預設值為每行1位元組。varchar size 用於儲存變長的字串資料。其中最大位元組長度由 size 指定。每行長度可變,最大長...