CREATE-TABLE-AS-SELECT

Name

CREATE TABLE AS SELECT

Description

该语句通过 Select 语句返回结果创建表结构,同时导入数据

语法:

  1. CREATE TABLE table_name [( column_name_list )]
  2. opt_engine:engineName
  3. opt_keys:keys
  4. opt_comment:tableComment
  5. opt_partition:partition
  6. opt_distribution:distribution
  7. opt_rollup:index
  8. opt_properties:tblProperties
  9. opt_ext_properties:extProperties
  10. KW_AS query_stmt:query_def

说明:

  • 用户需要拥有来源表的SELECT权限和目标库的CREATE权限
  • 创建表成功后,会进行数据导入,如果导入失败,将会删除表
  • 可以自行指定 key type,默认为Duplicate Key

SinceVersion 1.2

  • 所有字符串类型的列(varchar/var/string) 都会被创建为 string 类型。
  • 如果创建的来源为外部表,并且第一列为 String 类型,则会自动将第一列设置为 VARCHAR(65533)。因为 Doris 内部表,不允许 String 列作为第一列。

Example

  1. 使用 select 语句中的字段名

    1. create table `test`.`select_varchar`
    2. PROPERTIES(\"replication_num\" = \"1\")
    3. as select * from `test`.`varchar_table`
  2. 自定义字段名(需要与返回结果字段数量一致)

    1. create table `test`.`select_name`(user, testname, userstatus)
    2. PROPERTIES(\"replication_num\" = \"1\")
    3. as select vt.userId, vt.username, jt.status
    4. from `test`.`varchar_table` vt join
    5. `test`.`join_table` jt on vt.userId=jt.userId

Keywords

  1. CREATE, TABLE, AS, SELECT

Best Practice