tencent cloud

文档反馈

DDL 语法

最后更新时间:2024-08-07 17:28:29
    说明:
    以下语法说明为 DLC 原生表语法,DLC 原生表默认为 Iceberg 表。如果您使用 Iceberg 外部表,在 DDL 语法上会有细节差异,可参考文档 Iceberg 外部表与原生表语法差异

    CREATE TABLE

    语法

    CREATE TABLE [ IF NOT EXISTS ] table_identifier
    ( col_name[:] col_type [ COMMENT col_comment ], ... )
    [ COMMENT table_comment ]
    [ PARTITIONED BY ( col_name1, transform(col_name2), ... ) ]

    参数

    table_identifier

    table_identifier 支持三段式:catalog.db.name

    Schemas and Data Types

    col_type
    : primitive_type
      | nested_type
    primitive_type
    : boolean
    | int/integer
    | long/bigint
    | float
    | double
    | decimal(p,s),p=最大位数,s=最大小数点位数, s<=p<=38
    | date
    | timestamptimestamp with timezone,不支持time和without timezone
    | string,也可对应Iceberg uuid类型
    | binary,也可对应Iceberg fixed类型
    nested_type
    : struct
    | list
    | map

    Partition Transforms

    transform
    : identity,支持任意类型, DLC不支持该转换
    | bucket[N]hash mod N分桶,支持col_type: int,long, decimal, date, timestamp, string, binary
    | truncate[L],L截取分桶,支持col_type: int,long,decimal,string
    | years,年份,支持col_type: date,timestamp
    | months,月份,支持col_type: date,timestamp
    | days/date,日期,支持col_type: date,timestamp
    | hours/date_hour,小时,支持col_type: timestamp

    示例

    CREATE TABLE dempts(
    id bigint COMMENT 'id number',
    num int,
    eno float,
    dno double,
    cno decimal(9,3),
    flag  boolean,
    data string,
    ts_year timestamp,
    date_month date,
    bno binary,
    point struct<x: double, y: double>,
    points array<struct<x: double, y: double>>,
    pointmaps map<struct<x: int>, struct<a: int>>
    )
    COMMENT 'table documentation'
    PARTITIONED BY (bucket(16,id), years(ts_year), months(date_month), identity(bno), bucket(3,num),truncate(10,data));

    CREATE TABLE AS SELECT

    语法

    CREATE TABLE [ IF NOT EXISTS ] table_identifier
    [ COMMENT table_comment ]
    [ PARTITIONED BY ( col_name1, transform(col_name2), ... ) ]
    [ TBLPROPERTIES ( property_name=property_value, ... ) ]
    AS select_statement

    示例

    CREATE TABLE dempts_copy
    COMMENT 'table create as select'
    PARTITIONED BY (eno, dno)
    AS SELECT * from dempts;

    REPLACE TABLE AS SELECT

    保留表历史 History 的情况下,用 SELECT 查询的结果生成一个快照 Snapshot 来更新表。

    语法

    CREATE [OR REPLACE] TABLE table_identifier
    [ COMMENT table_comment ]
    [ PARTITIONED BY ( col_name1, transform(col_name2), ... ) ]
    AS select_statement

    示例

    CREATE OR REPLACE TABLE dempts_replace
    COMMENT 'table create as replace'
    PARTITIONED BY (eno, dno)
    AS SELECT * from dempts;

    DROP TABLE

    语法

    DROP TABLE [ IF EXISTS ] table_identifier

    ALTER TABLE

    变更表语法

    ALTER TABLE ... RENAME TO

    变更表名称

    语法

    ALTER TABLE table_identifier RENAME TO new_table_identifier

    ALTER TABLE ... SET / UNSET TBLPROPERTIES 

    更新/删除数据表属性

    语法

    -- SET 更新属性配置
    ALTER TABLE table_identifier
    SET TBLPROPERTIES (property_name=property_value, ...)
    
    -- UNSET 删除属性配置
    ALTER TABLE table_identifier
    UNSET TBLPROPERTIES (property_name, ...)

    示例

    -- SET 更新属性配置
    ALTER TABLE dempts SET TBLPROPERTIES ('read.split.target-size'='268435456');
    
    -- UNSET 删除属性配置
    ALTER TABLE dempts UNSET TBLPROPERTIES ('read.split.target-size'='268435456');
    

    ALTER TABLE ... WRITE ORDERED BY

    设置表数据插入时的排序方式

    语法

    ALTER TABLE table_identifier 
    WRITE [LOCALLY] ORDERED BY 
    {col_name [ASC|DESC] [NULLS FIRST|LAST]}[, ...]

    示例

    ALTER TABLE dempts WRITE ORDERED BY category, id;
    
    -- use optional ASC/DEC keyword to specify sort order of each field (default ASC)
    ALTER TABLE dempts WRITE ORDERED BY category ASC, id DESC;
    
    -- use optional NULLS FIRST/NULLS LAST keyword to specify null order of each field (default FIRST)
    ALTER TABLE dempts WRITE ORDERED BY category ASC NULLS LAST, id DESC NULLS FIRST;
    
    -- To order within each task, not across tasks
    ALTER TABLE dempts WRITE LOCALLY ORDERED BY category, id;

    ALTER TABLE ... WRITE DISTRIBUTED BY PARTITION

    修改分区表的数据分配策略

    语法

    ALTER TABLE table_identifier 
    WRITE DISTRIBUTED BY PARTITION 
    [ LOCALLY ORDERED BY 
    {col_name [ASC|DESC] [NULLS FIRST|LAST]}[, ...]] 

    示例

    ALTER TABLE dempts WRITE DISTRIBUTED BY PARTITION;
    ALTER TABLE dempts WRITE DISTRIBUTED BY PARTITION LOCALLY ORDERED BY id;

    ALTER TABLE COLUMNS

    变更字段语法

    ALTER TABLE ... ADD COLUMNS

    新增多个字段

    语法

    -- 新增多个字段
    ALTER TABLE table_identifier 
    ADD COLUMNS (col_name col_type [COMMENT col_comment], ...)
    
    -- 新增单个字段
    ALTER TABLE table_identifier 
    ADD COLUMN col_name col_type [COMMENT col_comment] 
    [FIRST | AFTER target_col_name]

    示例

    -- 新增多个字段
    ALTER TABLE dempts
    ADD COLUMNS (
    new_column_1 string comment 'new_column_1 docs',
    new_column_2 int comment 'new_column_2 docs'
    );
    
    -- 新增单个字段
    ALTER TABLE dempts 
    ADD COLUMN new_column_3 string comment 'new_column docs';

    ALTER TABLE ... RENAME COLUMN 

    变更字段名称

    语法

    ALTER TABLE table_identifier 
    RENAME COLUMN old_column_name TO new_column_name

    ALTER TABLE ... ALTER COLUMN

    变更字段类型/备注信息

    语法

    ALTER TABLE table_identifier 
    ALTER COLUMN col_name 
    {TYPE new_col_type | COMMENT col_comment}
    目前Iceberg TYPE 变更,仅支持字段类型安全扩展:
    int/integer -> long/bigint
    float -> double
    decimal(P,S) -> decimal(P2,S),其中P2 > P,即精准度提升

    示例

    ALTER TABLE dempts ALTER COLUMN new_column_2 TYPE bigint;
    ALTER TABLE dempts ALTER COLUMN new_column_2 comment 'alter docs';

    ALTER TABLE ... DROP COLUMN

    删除表字段

    语法

    ALTER TABLE table_identifier DROP COLUMN column_name

    ALTER TABLE PARTITIONS

    ALTER TABLE ... ADD PARTITION FIELD

    新增单个分区字段

    语法

    ALTER TABLE table_identifier 
    ADD PARTITION FIELD col_name|transform (col_name) [AS alias]
    transform 参考 CREATE TABLE 说明。

    示例

    ALTER TABLE dempts ADD PARTITION FIELD new_column_1;
    ALTER TABLE dempts ADD PARTITION FIELD bucket(3,new_column_2);

    ALTER TABLE ... REPLACE PARTITION FIELD

    替换单个分区字段

    语法

    ALTER TABLE table_identifier REPLACE PARTITION FIELD col_name|transform (col_name) [AS alias]

    ALTER TABLE ... DROP PARTITION FIELD

    删除单个分区字段

    语法

    ALTER TABLE table_identifier 
    DROP PARTITION FIELD col_name|transform (col_name);
    transform 参考 CREATE TABLE 说明

    示例

    ALTER TABLE dempts DROP PARTITION FIELD new_column_1;
    ALTER TABLE dempts DROP PARTITION FIELD bucket(3,new_column_2);

    ALTER TABLE ... SET IDENTIFIER FIELDS 

    添加 identifier fields 属性

    语法

    ALTER TABLE dempts SET IDENTIFIER FIELDS empno, name

    ALTER TABLE .. DROP IDENTIFIER FIELDS

    添加 identifier fields 属性

    语法

    
    ALTER TABLE dempts DROP IDENTIFIER FIELDS empno, name
    
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持