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
  • Key列和分区分桶列不会继承,建议自行指定,不指定的情况下按默认key列和分桶列建表

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