示例
请参考CREATE TABLE的示例。
-- 创建B模式数据库。
openGauss=# create database test_first_after dbcompatibility 'b';
openGauss=# \c test_first_after
-- 创建表t1并插入数据。
openGauss=# drop table if exists t1 cascade;
openGauss=# create table t1(f1 int, f2 varchar(20), f3 timestamp, f4 bit(8), f5 bool);
openGauss=# insert into t1 values(1, 'a', '2022-11-08 19:56:10.158564', x'41', true), (2, 'b', '2022-11-09 19:56:10.158564', x'42', false);
-- 指定位置新增字段
openGauss=# alter table t1 add f6 clob first;
openGauss=# alter table t1 add f7 blob after f2;
openGauss=# alter table t1 add f8 int, add f9 text first, add f10 float after f3;
-- 查询t1表结构
openGauss=# \d+ t1
-- 查询t1表数据
openGauss=# select * from t1;
-- 修改字段到指定位置
openGauss=# alter table t1 modify f3 timestamp first;
openGauss=# alter table t1 modify f1 int after f5;
-- 查询t1表结构
openGauss=# \d+ t1
-- 查询t1表数据
openGauss=# select * from t1;
-- 修改t1表的默认字符集为utf8mb4,默认字符序为utf8mb4_bin
openGauss=# alter table t1 charset utf8mb4 collate utf8mb4_bin;
-- 将t1表中字符类型字段的数据转化为utf8mb4编码,并设置表和字段的字符序为utf8mb4_bin
openGauss=# alter table t1 convert to charset utf8mb4 collate utf8mb4_bin;
-- 为t1表新增字段并设置字段的字符集为utf8mb4,字符序为utf8mb4_bin
openGauss=# alter table t1 add t10 varchar(20) charset utf8mb4 collate utf8mb4_bin;
-- 修改t1表的t10字段的字符集为utf8mb4,字符序为utf8mb4_unicode_ci
openGauss=# alter table t1 modify t10 varchar(20) charset utf8mb4 collate utf8mb4_unicode_ci;
-- 创建INVISIBLE唯一索引
openGauss=# alter table t1 add constraint uniq_a unique (f1) invisible;
-- 修改索引为VISIBLE
openGauss=# alter table t1 alter index uniq_a visible;