openGauss SQL语法 —ALTER TABLE(5)

示例

请参考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;

相关链接

CREATE TABLEDROP TABLE

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值