更新时间:2022-09-30 GMT+08:00
分享

ALTER TABLE修改表结构

本章节主要介绍ClickHouse修改表结构的SQL基本语法和使用说明。

基本语法

ALTER TABLE [database_name].name [ON CLUSTER cluster] ADD|DROP|CLEAR|COMMENT|MODIFY COLUMN ...

ALTER仅支持 *MergeTree ,Merge以及Distributed等引擎表。

使用示例

--给表t1增加列test01 
ALTER TABLE t1 ADD COLUMN test01 String DEFAULT 'defaultvalue';
--查询修改后的表t1
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│  id          │ UInt8  │                │                     │           │                    │                  │  
│  name        │ String │                │                     │           │                    │                  │ 
│  address     │ String │                │                     │           │                    │                  │
│  test01      │ String │  DEFAULT       │  'defaultvalue'     │           │                    │                  │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘
--修改表t1列name类型为UInt8
ALTER TABLE t1 MODIFY COLUMN name UInt8;
--查询修改后的表t1
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│  id          │ UInt8  │                │                     │           │                    │                  │  
│  name        │ UInt8  │                │                     │           │                    │                  │ 
│  address     │ String │                │                     │           │                    │                  │
│  test01      │ String │  DEFAULT       │  'defaultvalue'     │           │                    │                  │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘
--删除表t1的列test01
ALTER TABLE t1 DROP COLUMN test01;
--查询修改后的表t1
desc t1
┌─name────┬─type─┬─default_type─┬─default_expression ┬─comment─┬─codec_expression─┬─ttl_expression─┐
│  id          │ UInt8  │                │                     │           │                    │                  │  
│  name        │ UInt8  │                │                     │           │                    │                  │ 
│  address     │ String │                │                     │           │                    │                  │
└───────┴────┴────────┴────────── ┴───── ┴──────────┴─────────┘
分享:

    相关文档

    相关产品