数据仓库服务 GaussDB(DWS)-ALTER TABLE:列操作示例

时间:2025-01-26 10:51:42

列操作示例

 1 2 3 4 5 6 7 8 91011
DROP TABLE IF EXISTS warehouse_t;CREATE TABLE warehouse_t(    W_WAREHOUSE_SK            INTEGER                NOT NULL,    W_WAREHOUSE_ID            CHAR(16)               NOT NULL,    W_WAREHOUSE_NAME          VARCHAR(20)   UNIQUE DEFERRABLE,    W_WAREHOUSE_SQ_FT         INTEGER                        ,    W_COUNTY                  VARCHAR(30)                    ,    W_STATE                   CHAR(2)            DEFAULT 'GA',    W_ZIP                     CHAR(10)                       );

向表中增加一个新的字段:

1
ALTER TABLE warehouse_t ADD W_GOODS_CATEGORY int;

修改表中列名信息以及列字段信息:

1
ALTER TABLE warehouse_t CHANGE W_GOODS_CATEGORY W_GOODS_CATEGORY2 DECIMAL NOT NULL COMMENT 'W_GOODS_CATEGORY';

给已创建好的表增加主键:

1
ALTER TABLE warehouse_t ADD PRIMARY KEY(w_warehouse_name);

重命名列:

1
ALTER TABLE warehouse_t RENAME W_ZIP TO new_W_ZIP;

向表中增加多列:

1
ALTER TABLE warehouse_t ADD (W_COMMENT VARCHAR(117) NOT NULL, W_COUNT int);

修改表中已存在字段的数据类型,并将字段约束设置为非空:

1
ALTER TABLE warehouse_t MODIFY W_WAREHOUSE_SQ_FT varchar(20) NOT NULL;

为表的某列添加not null约束:

1
ALTER TABLE warehouse_t ALTER COLUMN W_COUNTY SET NOT NULL;

从表中删除一个字段:

1
ALTER TABLE warehouse_t DROP COLUMN W_STATE;
support.huaweicloud.com/sqlreference-dws/dws_06_0142.html