更新时间:2023-08-18 GMT+08:00
分享

ALTER TABLE修改表结构

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

基本语法

ALTER TABLE [database_name].name [ON CLUSTER ClickHouse集群名] ADD|DROP|CLEAR|COMMENT|MODIFY COLUMN ...

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

示例:

  1. 创建表DB_table1。
    CREATE TABLE DB_table1 ON CLUSTER default_cluster(Year UInt16,Quarter UInt8,Month UInt8,DayofMonth UInt8,DayOfWeek UInt8,FlightDate Date,FlightNum String,Div5WheelsOff String,Div5TailNum String)ENGINE = MergeTree() PARTITION BY toYYYYMM(FlightDate) PRIMARY KEY (intHash32(FlightDate)) ORDER BY (intHash32(FlightDate),FlightNum) SAMPLE BY intHash32(FlightDate) SETTINGS index_granularity= 8192;
  2. 给DB_table1增加列test。
    ALTER TABLE DB_table1 ADD COLUMN test String DEFAULT 'defaultvalue';

    查表。

    desc DB_tables;
  3. 修改表DB_table1列Year类型为UInt8。
    ALTER TABLE DB_table1 MODIFY COLUMN Year UInt8;

    查表结构。

    desc DB_tables;
  4. 删除表DB_table1列test。
    ALTER TABLE DB_table1 DROP COLUMN test;

    查表。

    desc DB_tables;
  5. 修改表DB_table1列Month为Month_test。
    ALTER TABLE DB_table1 RENAME COLUMN Month to Month_test;

    查表。

    desc DB_tables;
分享:

    相关文档

    相关产品